#!/bin/sh
set -u  # force better programming and ability to use check for not set
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

checkModTime () {
    dataFile = $1
    today=`date +'%b %e'`
    mtime=`ls -l ${dataFile}*`|grep "$today"
    return $?
}

node=`uname -n| sed -e 's/\([^\.]\)\..*/\1/'`
ttt=`which gang >/dev/null 2>&1`
if [ $? -eq 0 ];
then
    gang=`gang`
else
    gang="NOGANG"
fi

# the first parameter is the base.  if not entered, use the default
if [ $# -ge 1 ] ; 
then
    base=$1;
    shift;
else
    base=/tmp/doesnot/exist
fi

#convert=/usr/bin/X11/convert
convert=/usr/bin/convert
if [ ! -e $convert ];
then
    cvrt=`which convert 2> /dev/null`
    if [ $? -eq 0 ];
    then
	convert=$cvrt
    fi
fi

if [ `uname -n | grep -c ${gang}srv2` -ne 0 ]; then
  base=/root/CRONS
fi

if [ ! -d $base ]; then
  echo $base does not exist
  mkdir -p $base
fi

cd $base

last1week=`date -d '1 week ago' +'%Y-%m-%d:00:01'`
last6months=`date -d '6 months ago' +'%Y-%m-%d:00:01'`
lastmonth=`date -d '1 month ago' +'%Y-%m-%d:00:01'`
tomorrow=`date -d '1 day' +'%Y-%m-%d:00:01'`
lastmonday=`date -d 'last monday' +'%Y-%m-%d:00:01'`

blankH="/root/blankHISTOGRAM"

for i in *; do
  echo $i ..............................................
  cd $base/$i
  rm -f *HISTOGRAM  >/dev/null 2>&1
  rm -f *.pbm  >/dev/null 2>&1
  enrcp $i:/root/CRON/*HISTOGRAM . >/dev/null 2>&1
  enrcp $i:/home/enstore/CRON/*HISTOGRAM .  >/dev/null 2>&1
  #enrcp $i:/diska/pnfs/db/info/scheduler/*HISTOGRAM . >/dev/null 2>&1
  for j in *HISTOGRAM; do
     if [ "$j" = "*HISTOGRAM" ]; then break; fi
     f=`basename $j |sed -e 's/HISTOGRAM//'`
     # for the log-stash plot, use 6 months time scale not 2 weeks
     if [ "$f" = "log-stash" ]; 
     then 
	lastweek=$last6months; 
     elif [ "$f" = "backupSystem2Tape" ];
     then
        lastweek=$lastmonth;
     else
	lastweek=$last1week; 
     fi
     echo generating plot for $i/$f
     rtn=`checkModTime $i/$f.ps`
     if [ "$rtn" = "1" ];
     then
	# the date of the files was earlier than today, they are out of date
	j=$blankH
     fi
     file=`mktemp /tmp/cronplot.XXXXXXXX`
     file2=`mktemp /tmp/cronplot2.XXXXXXXX`
     pt=`python $ENSTORE_DIR/src/get_cron_title.py $f`
     title="$pt - $i/$f"
     echo "
set term postscript enhanced color solid 'Helvetica' 10
set output '$f.ps'
set xlabel 'Date'
set timefmt '%Y-%m-%d:%H:%M:'
set xdata time
set xrange ['$lastweek' :'$tomorrow']
set yrange [-5:15]
set key box
set grid
set label ' DIS ' at graph 0,.2 right
set label ' ACT ' at graph 0,.15 right
set label ' DWN ' at graph 0,.1 right
set label ' OOB ' at graph 0,.6 right
#set format x '%m-%d'
plot '$j' using 1:2 t '$title' with points 13, 0 t '' with lines 2, 10 t '' with lines 2, '$file2' using 1:2 t 'Last Monday' with lines lt 3 lw 4
#plot '$j' using 1:2 t '$title' with points, 0 t '' with lines 2, 10 t '' with lines 2, '$file2' using 1:2 t 'Last Monday' with lines 3
" >> $file
     echo "
$lastmonday -3
$lastmonday -5
" >> $file2
     gnuplot $file
     rm $file
     rm $file2
     $convert -flatten -background lightgray -rotate 90 $f.ps $f.jpg
     $convert -flatten -background lightgray -rotate 90 -geometry 120x120  -modulate 80 $f.ps ${f}_stamp.jpg
#     $convert -flatten -background lightgray -rotate 90 -geometry 120x120  -modulate -20 $f.ps ${f}_stamp.jpg
  done
  rm -f *HISTOGRAM  >/dev/null 2>&1
done
