#! /bin/bash
prefix=/usr
exec_prefix=${prefix}
bindir=${exec_prefix}/bin
datarootdir=${prefix}/share
pkgdatadir=${datarootdir}/gds
alarmdata=$pkgdatadir/Alarms

#######################################
#        Initialize the stuff         #
#######################################

# Set right environment for the correct LIGO site

WebDir=${GDSAPACHE}/Monitors/Alarms

TpltDir=$alarmdata/Templates
SnipDir=$alarmdata/Snippets
ScrhDir="/tmp/Alarms"
verbose=0
[ "$1" = "-v" ] && verbose=1

case $LIGOSITE in
  LHO)
    WebURL="https://marble.ligo-wa.caltech.edu/dmt/Monitors/Alarms"
    BaseURL="https://marble.ligo-wa.caltech.edu/dmt"
  ;;

  LLO)
    WebURL="https://llodmt.ligo-la.caltech.edu/dmt/Monitors/Alarms"
    BaseURL="https://llodmt.ligo-la.caltech.edu/dmt"
  ;;

  *)
    echo "Cannot identify the LIGO site. Please set the LIGOSITE variable"
    echo "correctly. It is set to $LIGOSITE now! " 
    echo "Exiting..."
    exit
esac

# Make sure that the required directories exist and all files are at the right place

if [ -e $ScrhDir ]; then
  rm -f $ScrhDir/* >/dev/null 2>&1
else
  mkdir -p $ScrhDir
fi

if [ -e $WebDir ]; then
  rm -r $WebDir/*   >/dev/null 2>&1
else
  mkdir -p $WebDir
fi

mkdir -p $WebDir/pix

# Web pages
cp $SnipDir/*.htm* $WebDir/    >/dev/null 2>&1
# Sounds
cp $SnipDir/*.mid  $WebDir/    >/dev/null 2>&1
cp $SnipDir/*.wa*  $WebDir/    >/dev/null 2>&1
# Pictures
cp $SnipDir/*.gif  $WebDir/pix >/dev/null 2>&1
cp $SnipDir/*.tif  $WebDir/pix >/dev/null 2>&1
cp $SnipDir/*.jpg  $WebDir/pix >/dev/null 2>&1
cp $SnipDir/*.png  $WebDir/pix >/dev/null 2>&1

chmod ugo+rw $WebDir/*
chmod ugo+rw $WebDir/*/*

if [ "$1" = "-v" ];then
    echo "Run environment:"
    echo "DMTNAMESERVER: " $DMTNAMESERVER
    echo "WebDir:        " $WebDir
    echo "WebURL:        " $WebURL 
    echo "BaseURL:       " $BaseURL 
    echo "ScrhDir:       " $ScrhDir
    echo "BinDir:        " $binbir
    echo "TpltDir:       " $TpltDir
    echo "TpltDir:       " $SnipDir
    echo "...................................................... Web directory:"
    ls $WebDir
    echo "...................................................... Pix dir for the Web:"
    ls $WebDir/pix
    echo "......................................................"
fi

#######################################
#        Prepare The web page         #
#######################################

while [ 1 = 1 ]; do
  rm $ScrhDir/*  >& /dev/null
  outf=">> $ScrhDir/Alarms.html"

  Alarm allactive | gawk 'BEGIN{c=0}{if($1=="Monitor:") {close(a); c++; a=sprintf("%s/%s.%d.alarm",D,$2,c);}; print $0 >> a}' D=$ScrhDir

  cat $TpltDir/AlarmHeader.html | gawk '{gsub("LIGOSITE",LIS); print $0}' LIS=$LIGOSITE > $outf

  echo '<table BORDER COLS=2 WIDTH="100%">'                          >> $outf
  echo '<tr><td>Created at</td>'                                     >> $outf
  echo '<td><b><font color="#FF0000">' `tconvert now` '</td>'        >> $outf
  echo '<td><b><font color="#FF00DD">' `date` '</td>'                >> $outf
  echo '<td><b><font color="#0000FF">' `date -u` '</td></tr>'        >> $outf
  echo '</table><br>'                                                >> $outf

  echo '<table BORDER COLS=2 WIDTH="100%">'                          >> $ouf
  echo '<tr><td><b><font color="#005500">Monitor</font></b></td>'    >> $outf
  echo     '<td><b><font color="#005500">Error  </font></b></td>'    >> $outf
  echo     '<td><b><font color="#005500">Seconds</font></b></td>'    >> $outf
echo     '<td><b><font color="#005500">Links  </font></b></td>'      >> $outf
  echo     '<td><b><font color="#005500">Description</font></b></td></tr>' >> $outf

  SN="NO"

  for ALS in `/bin/ls $ScrhDir/*.alarm`; do

    M=`cat $ALS | grep "Monitor: "    | cut -d ' ' -f 2`
    A=`cat $ALS | grep "Alarm: "      | cut -d ' ' -f 2`
    L=`cat $ALS | grep "Severity: "   | gawk 'BEGIN{a=0}  {a=$2; if(a>7) a=7;} END{print a}'`
    S=`cat $ALS | grep "Start time: " | gawk '{gsub(":"," ");print $3}'`
    N=`when 0 %s`
    R=`cat $ALS | grep "Duration: "   | gawk '{r=s+$2-n; printf("%d", r)}' s=$S n=$N`
    W=`cat $ALS | grep "Web file: "   | cut -d ' ' -f 3`
    E=`echo $W  | grep http | gawk '{f=$1;} END{print f}' FS=":"`
    [ "$E" = "http" ] || W=$BaseURL/$W
    SO={$M}_{$A}_{$L}

    [ "$SN" != "NO" ] || \
      SN=`cat {$TpltDir}/Sound_Needed.list | gawk 'BEGIN {a="NO"}{if($1==S){a="YES"}}END{print a}' S=$SO`

    LL=`cat {$TpltDir}/Sound_Needed.list | gawk '{if($1==S){lev=8;}}END{print lev}' S=$SO lev=$L`

    #echo $SO $SN

    cat $ALS \
      | grep Description \
      | sed -e 's/Description: //g' \
      | gawk '{printf("\#\!/bin/tcsh -f\necho \"%s\"\n", $0)}' \
      > $ScrhDir/D.tmp
    chmod u+x $ScrhDir/D.tmp
    cat $ALS \
      | grep Parameters \
      | sed -e 's/Parameters://g' \
	    -e 's/ +/ /g' \
	    -e 's/^ */"/g' \
	    -e 's/ /" "/g' \
	    -e 's/$/"/' \
      | gawk '{printf("\#\!/bin/tcsh -f\n%s/%s %s\n",SD,"D.tmp",$0)}' SD=$ScrhDir \
      > $ScrhDir/P.tmp
    chmod u+x $ScrhDir/P.tmp
    $ScrhDir/P.tmp > $ScrhDir/Desc.txt

    #rm $ScrhDir/D.tmp
    #rm $ScrhDir/P.tmp

    if [ $verbose = 1 ]; then
      echo "Data Extracted from " $ALS " :"
      echo "Monitor:      " $M
      echo "Alarm:        " $A
      echo "Severty:      " $L
      echo "Start time:   " $S
      echo "Time to live: " $R
      echo "Web file:     " $W
      echo "Description:  " `cat $ScrhDir/Desc.txt`
    fi

    if [ "$R" -gt 0 ]; then
      echo '<tr><td><A href="'$W'" target="middle">' >> $outf
      [ "$LL" = "8" ] || \
        echo '<img SRC="pix/speaker.gif" BORDER=0 align=CENTER>' >> $outf
      echo '<img SRC="pix/'$L'.gif" alt="' >> $outf
      echo "`cat $ScrhDir/Desc.txt`" >> $outf
      echo '" title="' >> $outf
      echo "`cat $ScrhDir/Desc.txt`" >> $outf
      echo '" BORDER=0 align=CENTER></A>' >> $outf
      echo '<b><font color="#FF0000">'$M'</font></td><td></b><A target="middle" href=" '$W' " > '$A'</a></td>' >> $outf
      echo '<td><i><font color="#009900">'$R' </font></i></td>' >> $outf
      echo '<td></td>' >> $ScrhDir/Alarms.html
      echo '<td><font color="#FF0000"><font size=-2>' >> $outf
      echo "`cat $ScrhDir/Desc.txt`" >> $outf
      echo '</font></font></td></tr>' >> $outf
    fi
  done

  echo '</table>' >> $ScrhDir/Alarms.html

  if [ "$SN" = "YES" ]; then
    cat $TpltDir/sound.html >> $outf
  fi

  cat $TpltDir/AlarmTrailer.html >> $outf

  mv $outf $WebDir/leftT.html >/dev/null 2>&1
  mv $WebDir/leftT.html  $WebDir/left.html >/dev/null 2>&1

  sleep 10
  rm $ScrhDir/*

done
