#!/bin/bash
##########################################################################
#
#  tellerstats.sh     --  tellerstats.sh for the local variation
#                         of tellerstats
#
#  $Id: tellerstats.sh,v 1.1.0.12 2004/09/14 03:59:16 floyd Exp floyd $
#
#  See file ChangeLog for the RCS history.
#
##########################################################################
#
#  Copyright 2004 by Floyd Davidson, floyd@barrow.com
#  File created:  Mon Sep  6 03:57:49 2004
#  Last updated:  Thu Sep  9 11:37:34 2004
#
##########################################################################
#
#    tellerstats.sh                  3
#	generate graphs from the data
#
#    Copyright 2001 The lm_sensors group
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

# generic tellerstats init BEGIN

PATH=/usr/local/sbin:/usr/X11R6/bin:/bin:/bin/sbin:/usr/bin/:/usr/sbin/:/usr/local/bin

DEBUG=ON

# get config information from /etc/tellerstats.conf or whereever we are pointed

if [ -z "$TELLERSTATS_CONF" ]
then
   TELLERSTATS_CONF=/etc/tellerstats.conf
fi   

export TELLERSTATS_CONF

if [ ! -r $TELLERSTATS_CONF ]
then
   echo "$0: Could not find config file $TELLERSTATS_CONF"
   exit 1
fi   

. $TELLERSTATS_CONF


if [ ! -d $DBPATH ]
then
   echo "$0: data directory $DBPATH does not exist"
   exit 1
fi

if [ ! -d $SENSORPATH1 ]
then
   echo "$0: sensor information directory $SENSORPATH1 does not exist."
   exit 1
fi

if [ ! -d $SENSORPATH2 ]
then
   echo "$0: sensor information directory $SENSORPATH2 does not exist."
   exit 1
fi


if [ ! -d $HTMLROOT ]
then
   echo "$0: The root of your webserver - $HTMLROOT - does not exist..bailing out"
   exit 1
fi

if [ ! -d $HTMLPATH ]
then
   echo "$0: The place where we keep HTML files and pictures - $HTMLPATH - does not exist..bailing out"
   exit 1
fi

if [ ! -r $GNUPLOTSCRIPT_TMPL ]
then
   echo "$0: The gnuplot script template $GNUPLOTSCRIPT_TMPL does not exist..bailing out"
   exit 1
fi

export DBPATH SENSORPATH1 SENSORPATH2 TEMPPATH HTMLROOT HTMLPATH GNUPLOTSCRIPT_TMPL

if [ -n "$DEBUG" ]
then
   echo "DBPATH = $DBPATH"
   echo "SENSORPATH1 = $SENSORPATH1"
   echo "SENSORPATH2 = $SENSORPATH2"
   echo "TEMPPATH = $TEMPPATH"
   echo "HTMLROOT = $HTMLROOT"
   echo "HTMLPATH = $HTMLPATH"
   echo "GNUPLOTSCRIPT_TMPL = $GNUPLOTSCRIPT_TMPL"
fi

# generic tellerstats init END

if [ -z "$LINEWIDTH" ]
then
   LINEWIDTH=10
fi
export LINEWIDTH   

if [ -z "$PLOTFORMAT" ]
then
   PLOTFORMAT=ps
fi
export PLOTFORMAT

if [ -z "$PLOTTERMINAL" ]
then
   PLOTTERMINAL="postscript eps enhanced color \"Helvetica\" 26"
fi
export PLOTTERMINAL

if [ ! -n "$IMAGE_TSIZE" ] 
then
    IMAGE_TSIZE="214x160"
fi
export IMAGE_TSIZE

if [ ! -n "$IMAGE_HSIZE" ] 
then
    IMAGE_HSIZE="800x600"
fi
export IMAGE_HSIZE

if [ ! -n "$IMAGE_LSIZE" ] 
then
    IMAGE_LSIZE="1024x768"
fi
export IMAGE_LSIZE


if [ -n "$DEBUG" ]
then
   echo "LINEWIDTH = $LINEWIDTH"
   echo "PLOTFORMAT = $PLOTFORMAT"
   echo "PLOTTERMINAL = $PLOTTERMINAL"
   echo "IMAGE_TSIZE = $IMAGE_TSIZE   (thumbnail size)"
   echo "IMAGE_LSIZE = $IMAGE_LSIZE   (low resolutions size)"
   echo "IMAGE_HSIZE = $IMAGE_HSIZE   (high resolutions size)"
fi


# Trim files to 48 hour window

cd $DBPATH
files="`echo *`"

for this in $files
do
   tail $this -n1450 > ${this}.tmp
   mv ${this}.tmp $this
done

###############################################

rm -rf $TEMPPATH
mkdir -p $TEMPPATH

cd $TEMPPATH

# Update primary plots
export DOLLAR2=\$2
GNUPLOTSCRIPT="$TEMPPATH/gnuplotscript"
cat $GNUPLOTSCRIPT_TMPL | perl -p -e's/\$(\w+)/$ENV{$1}/g' > $GNUPLOTSCRIPT
gnuplot < $GNUPLOTSCRIPT
rm $GNUPLOTSCRIPT

files="`echo *`"

CONVERT_OPTS_T="-interlace none -scale    ${IMAGE_TSIZE} -quality 100"
CONVERT_OPTS_H="-interlace none -geometry ${IMAGE_HSIZE}! -quality 100"
CONVERT_OPTS_L="-interlace none -geometry ${IMAGE_LSIZE}! -quality 100"

for this in $files
do
   prefix=`echo $this|perl -p -e's/\.\w+$//'`
   convert $CONVERT_OPTS_T $TEMPPATH/$this $HTMLPATH/${prefix}T.png
   convert $CONVERT_OPTS_H $TEMPPATH/$this $HTMLPATH/${prefix}H.png
   convert $CONVERT_OPTS_L $TEMPPATH/$this $HTMLPATH/${prefix}L.png

   touch $HTMLPATH/${prefix}T.png $HTMLPATH/${prefix}H.png $HTMLPATH/${prefix}L.png

done

# Generate html index files

DATE_STAMP=$(date)

# index.html (1024x768 resolution)
sed -e "s/%%DATE%%/${DATE_STAMP}/" \
    -e "s/%%PNG_RES%%/H.png/g" \
    -e "s/%%H_RES%%/486/g" \
    -e "s/%%V_RES%%/365/g" \
    -e "s/%%OTHER_RES%%/SMALLER/g" \
    -e "s/%%OTHER_INDEX%%/index1.html/g" \
    -e "s/%%THIS_INDEX%%/index.html/g" \
  < ${HTMLPATH}/index.tmpl \
  > ${HTMLPATH}/index.html

# index1.html (800x600 resolution)
sed -e "s/%%DATE%%/${DATE_STAMP}/" \
    -e "s/%%PNG_RES%%/L.png/g" \
    -e "s/%%H_RES%%/376/g" \
    -e "s/%%V_RES%%/282/g" \
    -e "s/%%OTHER_RES%%/LARGER/g" \
    -e "s/%%OTHER_INDEX%%/index.html/g" \
    -e "s/%%THIS_INDEX%%/index1.html/g" \
  < ${HTMLPATH}/index.tmpl \
  > ${HTMLPATH}/index1.html

# if this was called as a cgi script, it should redirect to the index.html file
if [ -n "$REMOTE_HOST" ]
then
   REL_HTML=${HTMLPATH#$HTMLROOT}
   echo "Location: $REL_HTML/index.html"
   echo
fi

if [ -z "$DEBUG" ]
then
   rm -rf $TEMPPATH
fi   

exit 0