#!/bin/sh -e

#=======================================================
#
# These four definitions specify the binary paths for
# nova and clyde and the writable document and log root
# for the web server.
# 
#=======================================================
#
#clyde_bin=/usr/local/servers/npac_http/tools/stats  NOT USED HERE!
nova_bin=/project/npac_srvtools/bin/stats
docroot=/usr/local/archives/public/html
#logroot=/project/C067
logroot=/project/www_npac_log
tmp=$logroot/tmp

statsdir=$docroot/stats


thisyear=`date +%y`
thismonth=`date +%h`
lastyear=`expr $thisyear - 1`

#
# THESE ARE REQUIRED BECAUSE NOVA DOES NOT HAVE A /usr/local/servers
# MOUNT POINT for /project/C420/s/
#
bindir=$nova_bin

wwwresult=$statsdir/wwwstats_28.html
wwwmonth=$statsdir/wwwstats.html
wwwyear=$statsdir/archive/$thisyear.wwwstats.html
#wwwyear=$statsdir/archive/96.wwwstats.html

gwstat="$bindir/gwstat"
combine="$bindir/combine"

#
# generate gwstat *.gifs
# 
# For gwstat to work the executables must be in the path!
# The easiest way to achieve this is to cd to the $bindir
#
echo '['`date`'] Starting: gwstat.sh'
cd $bindir
LD_LIBRARY_PATH=/usr/dt/lib:/usr/X11R6/lib:/usr/ucblib:/usr/openwin/lib
export LD_LIBRARY_PATH

$gwstat -i $wwwmonth -C country -A archive
$gwstat -i $wwwresult -R recent -b blue -f yellow
$gwstat -i $wwwresult -D daily -b RosyBrown -f brown -o RosyBrown
$gwstat -i $wwwresult -H hourly -b gray -f brown -o brown
$gwstat -i $wwwresult -W weekly -b turquoise -f DarkViolet
$gwstat -i $wwwyear -M monthly -b gray  -f blue
$gwstat -i $wwwmonth -S $statsdir/archive.conf -T selected
$gwstat -i $wwwyear -M yearly -b gray -f blue

cd $statsdir
#
#  Built new base background gif if one doesn't exist.
#
#  $bindir/montage \
# -tile 3 -geometry 100x100 -title "Clickable Traffic graph" \
# -label "by archive" $statsdir/NA.gif \
# -label "by subject" $statsdir/NA.gif \
# -label "by domain" $statsdir/NA.gif \
# -label "by hour" $statsdir/NA.gif \
# -label "by day" $statsdir/NA.gif \
# -label "by week" $statsdir/NA.gif \
# -label "last year" $statsdir/NA.gif \
# -label "by month" $statsdir/NA.gif \
# -label "last 7 days" $statsdir/NA.gif \
# base.gif

### compose the chip files into stats.gif
all=$tmp/gif.$$

if ( test -f archive-chip.gif ) then
  $combine +display -geometry +32+97 -size 100x100 \
    -compose replace archive-chip.gif $statsdir/base.gif $all
else
  cp $statsdir/base.gif $all
fi
if (test $thismonth = "Jan") then
cp $statsdir/archive/$lastyear.monthly-chip.gif $statsdir/monthly-chip.gif
cp $statsdir/archive/$lastyear.monthly.gif $statsdir/monthly.gif
else 
cp $statsdir/archive/$thisyear.monthly-chip.gif $statsdir/monthly-chip.gif
cp $statsdir/archive/$thisyear.monthly.gif $statsdir/monthly.gif
fi
$combine +display -geometry +196+97 -size 100x100 \
-compose replace selected-chip.gif $all $all

$combine +display -geometry +360+97 -size 100x100 \
-compose replace country-chip.gif $all $all

$combine +display -geometry +32+281 -size 100x100  \
-compose replace hourly-chip.gif  $all $all

$combine +display -geometry +196+281 -size 100x100  \
-compose replace daily-chip.gif  $all $all

$combine +display -geometry +360+281 -size 100x100  \
-compose replace weekly-chip.gif $all $all

$combine +display -geometry +32+465 -size 100x100  \
-compose replace monthly-chip.gif $all $all

$combine +display -geometry +196+465 -size 100x100  \
-compose replace recent-chip.gif $all $all

mv -f $all stats.gif
chmod 664 stats.gif archive*.gif selected*.gif country*.gif hourly*.gif \
          daily*.gif weekly*.gif yearly*.gif monthly*.gif recent*.gif
cp $statsdir/archive/$thisyear.monthly-chip.gif $statsdir/monthly-chip.gif
cp $statsdir/archive/$thisyear.monthly.gif $statsdir/monthly.gif

echo '['`date`'] Finished: gwstat.sh'
exit

