#!/bin/bash

# Directory where the executables will be installed
BIN_TARGET_DIRECTORY=$HOME/operations/who_is_using_the_cluster
# Directory where the generated HTML pages will be written
# Must be accessible from each node and from apache
# On the IPP cluster, it's a symbolic link located in the ippm
# user home directory (hence nfs accessible) to /var/www/localhost/htdocs
# on ippmonitor.ipp.ifa.hawaii.edu
HTML_TARGET_DIRECTORY=$HOME/htdocs/clusterMonitor2
# File containing the nodes names (one nore per line)
TARGET_NODENAMES_FILENAME=$HOME/common/share/ipp/AllNodesNames

# No need to change anything from there unless you know what you're doing
echo "$BIN_TARGET_DIRECTORY"

MKDIR="/bin/mkdir -p"
CP="/bin/cp -f"
CAT=/bin/cat
SED=/bin/sed
CHMOD=/bin/chmod

echo "Getting SVN revision"
SVNREVISION=`/usr/bin/svn up . | cut -f3 -d' '`

echo "Copying executables"
$MKDIR $BIN_TARGET_DIRECTORY
$CP update_all_nodes.py $BIN_TARGET_DIRECTORY/
$CHMOD 755 $BIN_TARGET_DIRECTORY/update_all_nodes.py
$CP get_processes_information.py $BIN_TARGET_DIRECTORY/
$CHMOD 755 $BIN_TARGET_DIRECTORY/get_processes_information.py
$CP documentation/README.deployed $BIN_TARGET_DIRECTORY/README
$CHMOD 644 $BIN_TARGET_DIRECTORY/README

echo "Copying html and php files"
$MKDIR $HTML_TARGET_DIRECTORY/
$CAT html/help.html | $SED "s/__SVN_REVISION__/$SVNREVISION/g" > $HTML_TARGET_DIRECTORY/help.html
$CP html/index.php $HTML_TARGET_DIRECTORY/
$CP html/header.html $HTML_TARGET_DIRECTORY/
$CP documentation/README $HTML_TARGET_DIRECTORY/
$CHMOD 644 $HTML_TARGET_DIRECTORY/README

echo "Creating html files for each node"
$MKDIR $HTML_TARGET_DIRECTORY/nodes
for node in `cat $TARGET_NODENAMES_FILENAME`; do
  echo "$node not updated yet" > $HTML_TARGET_DIRECTORY/nodes/$node.html
  $CHMOD 644 $HTML_TARGET_DIRECTORY/nodes/$node.html
done

echo "Now you can add something like:"
echo "--"
echo "MAILTO=schastel@ifa.hawaii.edu"
echo "0-55/5 * * * * $HTML_TARGET_DIRECTORY/update_all_nodes.py >> /tmp/who_is_using_the_cluster.log 2>&1"
echo "--"
echo "to the crontab of a relevant user for a relevant node"

