#!/bin/sh
set -u
if [ "${1:-}" = "-x" ] ; then set -xv; shift; fi

# VOLUME_QUOTAS=${1:-/diska/tape-inventory/VOLUME_QUOTAS}
VOLUME_QUOTAS=`enstore config --show inventory | grep inventory_rcp_dir | cut -d':' -f 3 | cut -d"'" -f 1`/VOLUME_QUOTAS
QUOTA_DIR=`dirname $VOLUME_QUOTAS`/quotas
if [ ! -d $QUOTA_DIR ]; then mkdir -p $QUOTA_DIR;fi

if [ ! -r $VOLUME_QUOTAS ];then
   echo `date` ERROR can not read \"$VOLUME_QUOTAS\"
   exit 1
fi

now=`date +"%Y-%m-%d:%H:%M:%S"`
gen=`head -n1 $VOLUME_QUOTAS | sed -e 's/.*generated: [A-Za-z][A-Za-z][A-Za-z] //'`
generated=`date -d "$gen" +"%d-%b-%y:%H:%M"`
last_quarter=`date --date "3 months ago"  +"%b-%y"`
next_month=`date --date "34 days"  +"%b-%y"`

start=0
cat $VOLUME_QUOTAS | sed -e 's/none: emergency/emergency/' | \
  while read n lib sg req auth quota allocated blank written deleted used rest; do
    start=`expr $start + 1`
    if [ $start -le 2 ]; then continue; fi
    if [ -z "${n:-}" ];then continue; fi
    if [ -z "${lib:-}" ]; then break; fi
    data=$QUOTA_DIR/$lib.$sg
    echo $generated $blank $written >>$data
    ps=$QUOTA_DIR/$lib.$sg.ps
    jpg=$QUOTA_DIR/$lib.$sg.jpg
    stamp=$QUOTA_DIR/$lib.${sg}_stamp.jpg
    plt=$QUOTA_DIR/$lib.$sg.plt
    rm -f $plt
    alarm=""
    if [ $sg != "emergency" ]; then
      if   [ $blank -eq 0 ]; then alarm="NO BLANKS LEFT"
      elif [ $blank -eq 1 ]; then alarm="1 BLANK LEFT"; 
      elif [ $blank -eq 2 ]; then alarm="2 BLANKS LEFT"; 
      elif [ $written -eq 0 ]; then alarm="NOTHING WRITTEN";
      elif [ `expr $blank \* 100 / $written` -le 5 ]; then alarm="LOW BLANKS";
      fi
    fi
    echo `date` $sg $lib $generated $blank $written $alarm

    echo set terminal postscript color solid >>$plt
    echo set output \"$ps\" >>$plt
    echo set title \"$lib $sg     Authorized=$auth Quota=$quota Allocated=$allocated Written=$written Bytes=$used\" >>$plt
    echo set ylabel \"Blank Tapes Left\" >>$plt
    echo set xdata time >>$plt
    echo set timefmt \"%d-%b-%y:%H:%M\" >>$plt
    echo set format x \"%m-%d-%y\" >>$plt
    echo set grid >>$plt
    echo \#set key left >>$plt
    echo set nokey >>$plt
    echo set label \"Plotted $now\" at graph 1.01,0 rotate font \"Helvetica,10\" >>$plt
    echo set label \"$blank blanks left\" at graph .1,.85 font  \"Helvetica,20\" >>$plt
    if [ -n "${alarm:-}" ]; then 
       echo set label \"$alarm\" at graph .2,.5 font \"Helvetica,50\" >>$plt
    fi
    echo set xrange[\"01-$last_quarter:00:00\":\"01-$next_month:00:00\"] >>$plt
    echo set yrange [0:] >>$plt
    echo \#plot \"$data\" using 1:2 t\"Blanks Left\" pt 5 ps 3, \"$data\" using 1:3 t\"Vols Written\" pt 5 ps 3 >>$plt
    echo \#plot \"$data\" using 1:2 t\"Blanks Left\" with impulses lw 10 >>$plt
    echo plot \"$data\" using 1:2 t\"Blanks Left\" with impulses >>$plt

    /usr/bin/gnuplot $plt
    /usr/bin/convert -flatten -background lightgray -rotate 90 $ps $jpg
    /usr/bin/convert -flatten -background lightgray -rotate 90 -geometry 120x120  -modulate 80 $ps $stamp
    rm -f $plt
done
