#!/bin/sh
#
# ========================================================================
#
# *
# * This file is part of MARS, the MAGIC Analysis and Reconstruction
# * Software. It is distributed to you in the hope that it can be a useful
# * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
# * It is distributed WITHOUT ANY WARRANTY.
# *
# * Permission to use, copy, modify and distribute this software and its
# * documentation for any purpose is hereby granted without fee,
# * provided that the above copyright notice appear in all copies and
# * that both that copyright notice and this permission notice appear
# * in supporting documentation. It is provided "as is" without express
# * or implied warranty.
# *
#
#
#   Author(s): Daniela Dorner  08/2005 <mailto:dorner@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2006
#
#
# ========================================================================
#
# This script produces the plots from all root-files in the web directory
# 
# After checking, if the script is already running, the plots are produced:
# With the programm showplot a ps-file is written, from which the png 
# files are produced. 
#

source `dirname $0`/sourcefile
printprocesslog "INFO starting $0"
program=dowebplots

set -C

scriptlog=$runlogpath/$program-$datetime.log
date >> $scriptlog 2>&1

while getopts p: opts
do
   case $opts in
     p)  type=$OPTARG
         echo "got programname: $type"  >> $scriptlog 2>&1
         ;;
     ?)  echo "usage: $(basename $0) -p programname"  >> $scriptlog 2>&1
         ;;
   esac
done

if [ "$type" = "" ]
then 
   echo "no program name given -> exit"  >> $scriptlog 2>&1
   echo "usage: $(basename $0) -p programname"   >> $scriptlog 2>&1
   date  >> $scriptlog 2>&1
   printprocesslog "INFO finished $0"
   exit
fi

# check if script is already running
lockfile=$lockpath/lock-$program-$type.txt
checklock  >> $scriptlog 2>&1

#finding all rootfiles in the data directory, that were modified in the last 3 days
#this are all statusdisplays 
rootfiles=`find $datapath/$type/ -maxdepth 10 -name '*.root' -mtime -3 | grep -v '_I_' | grep -v '_Y_' | grep -v 'summary'`

#exit if no rootfiles are found
if [ "$rootfiles" = "" ]
then 
   echo "nothing to do -> exit"  >> $scriptlog 2>&1
   finish >> $scriptlog 2>&1
fi


cd $mars

#produce plots for each rootfile
printprocesslog "INFO procduing plots for each $type rootfile"
for rootfile in ${rootfiles[@]}
do 
   date >> $scriptlog 2>&1
   #get names of the psfile and the 
   #pngfiles (1 per tab in the statusdiplay)
   psfile=`echo $rootfile | sed -e 's/.root$/.ps/g'`
   tabfile=`echo $rootfile | sed -e 's/.root$/-tab/g'`
   echo "rootfile: $rootfile" >> $scriptlog 2>&1
   echo "psfile:   $psfile"   >> $scriptlog 2>&1
   echo "tabfile:  $tabfile"  >> $scriptlog 2>&1
   
   #get date of root- and psfile
   daterootfile=`date +%Y%m%d -r $rootfile` >> $scriptlog 2>&1
   datepsfile=`date +%Y%m%d -r $psfile` >> $scriptlog 2>&1
   if [ "$datepsfile" = "" ]
   then
      echo "date of psfile is empty, i.e. the file $psfile doesn't exist" >> $scriptlog 2>&1
      echo " -> setting date to 0 and producing psfile..." >> $scriptlog 2>&1
      datepsfile=0
   fi

   echo "checking date..."  >> $scriptlog 2>&1
   echo "date of rootfile: $daterootfile" >> $scriptlog 2>&1
   echo "date of psfile:   $datepsfile"   >> $scriptlog 2>&1
   #if the psfile is newer than the rootfile
   #no plots have to be done -> continue
   if [ "$datepsfile" -gt "$daterootfile" ] >> $scriptlog 2>&1
   then
      echo "psfile is newer than rootfile -> continue " >> $scriptlog 2>&1
      continue
   fi

   printprocesslog "INFO procduing plots for $rootfile"
   echo "producing psfile..." >> $scriptlog 2>&1
   ./showplot -b --save-as-ps=$psfile $rootfile >> $scriptlog 2>&1

   echo "creating temporary dir for pstoimg..." >> $scriptlog 2>&1
   temppath=`dirname $rootfile`
   tempwebplotspath=$temppath/tempwebplots
   makedir $tempwebplotspath >> $scriptlog 2>&1

   echo "converting plots to png..." >> $scriptlog 2>&1
   pstoimg -antialias -flip r270 -density 100 -tmp $tempwebplotspath -type png -multipage -out=$tabfile $psfile >> $scriptlog 2>&1
   
   echo "removing temporary dir..." >> $scriptlog 2>&1
   rmdir -v $tempwebplotspath >> $scriptlog 2>&1
done

finish >> $scriptlog 2>&1

