#!/bin/sh

PATH=$PATH:/sbin:/usr/sbin

#Number of columns to display the output into.
NUMCOLUMNS=3

#Make sure to set a timeout.
timeout='--timeout 10 --retries 3'

is_host()
{
   host_to_check=$1

   #Get the IPs of the host the script is running on.
   ips_to_check=`nslookup $host_to_check | grep -i Address | sed '1d' | cut -f 2- -d " " | tr ',' "\n" | sed 's/ //g'`
   ip_to_check=`echo "$ips_to_check" | head -n 1`

   #Get the ip addresses on the current machine.  This was been tested on
   # Linux, Solaris and MacOS X.
   ip_addrs=`/sbin/ifconfig -a | egrep "inet " | awk '{print $2}' | sed 's/addr://'`

   #Take the IP of the host we are testing, and compare it with every IP
   # defined on the current node.
   for ip in $ip_addrs; do
      if [ "$ip_to_check" = "$ip" ]; then
         #echo 1
         return 1
      fi
   done

   #echo 0
   return 0
}

#Make sure we are
if [ `id -u` -ne 0 ]; then
   echo "Must be user root." 2>&1
   exit 1
fi

#Verify that we can find pnfs.
if [ ! -f /usr/etc/pnfsSetup ]; then
   echo "Unable to find /usr/etc/pnfsSetup file." 1>&2
   exit 1
fi
pnfs_location=`egrep "^pnfs=" /usr/etc/pnfsSetup | cut -f 2 -d "="`
if [ -z "$pnfs_location" ]; then
   echo "Unable to find pnfs installation." 1>&2
   exit 1
fi
pmount=$pnfs_location/tools/pmount
mdb=$pnfs_location/tools/mdb
if [ ! -f $pmount ]; then
   echo "Unable to find pmount command." 1>&2
   exit 1
fi

#Obtain the list of hosts allowed to mount pnfs.
hostlist=`$pmount show hosts`
if [ $? -ne 0 ]; then
   echo "Failed to run pmount." 1>&2
   exit 1
fi
#Obtain the list of databases.
showall=`$mdb showall`
if [ $? -ne 0 ]; then
   echo "Failed to run mdb." 1>&2
   exit 1
fi

#Determine the node the web server is running on.
web_node=`enstore conf $timeout --show crons web_node`
if [ -z "$web_node" ]; then
   echo "Unable to determine web server node." 1>&2
   exit 1
fi
#Determine the directory on the web server to copy the output to.
html_dir=`enstore conf $timeout --show crons html_dir`
if [ -z "$html_dir" ]; then
   echo "Unable to determine html directory." 1>&2
   exit 1
fi

#Get the web alias name if available.
www_alias=`enstore conf $timeout --show crons www_alias` #optional in config
if [ $? -ne 0 ]; then
   #If the alias wasn't available, go with the hostname.
   www_alias=`hostname | cut -f 1 -d "."`
fi

#Get the pnfs alias name if available.
pnfs_alias=`enstore conf $timeout --show crons pnfs_alias` #optional in config
alias_rtn=$?
is_host $pnfs_alias
is_host_rtn=$?
if [ $alias_rtn -ne 0 -o $is_host_rtn -eq 0 ]; then
   #If the alias wasn't available or belonged to a different pnfs server
   # than this one, go with the hostname.
   pnfs_alias=`hostname | cut -f 1 -d "."`
fi
filename=/tmp/pnfsExports.html

#Send the output for making the web page to the temporary file.
(
echo '<html> <head> <title>PNFS Exports Page</title> </head> <body>'
echo '<body bgcolor="#ffffff" text=#a0a0ff">'
echo '<h1><center>PNFS Exports Page </center><h1><hr>'
echo "<h1><center>PNFS Information: `date`</center><h1>"

echo '<pre>'
echo "$showall"
echo '</pre><hr>'

echo "<h1><center>PNFS ExportList Fetch Begin: `date`</center><h1><hr>"
echo '<table bgcolor="#dfdff0" nosave>'
echo -e "<tr>\n"

col=0
for host in `echo $hostlist`; do
   if [ $col -eq $NUMCOLUMNS ]; then
      echo -e "</tr>\n <tr>"
      col=0
   fi
   col=`expr $col + 1`
   echo '<td> <pre>'
   
   show_host_info=`$pmount show host $host`
   echo "$show_host_info" 
   
   echo '</pre> </td>'
done

echo '</table>'
echo "<h1><center>PNFS ExportList Fetch Done: `date`</center><h1><hr>"
echo '</html>'
) > $filename

#This copy should still work if the pnfs and web servers are on the same node.
chown enstore.enstore $filename
chmod 755 $filename
enrcp $filename $web_node:$html_dir


