#!/bin/sh 

#==============================================================================
#
#   Shell script to do monthly wwwstats on log file and produce an HTML file
#   containing the NPAC site statistics
#
#   Ade Miller 8th Jan 96
#
#==============================================================================
#=======================================================
#
# These four definitions specify the binary paths for
# nova and clyde and the writable document and log root
# for the web server.
#
#=======================================================
#
clyde_bin=/srvtools/bin/stats
#nova_bin=/project/C420/s/npac_http/tools/stats      NOT USED HERE!
docroot=/servers-admin/npac_http/html
#logroot=/e/p/C067
logroot=/e/s/log
tmp=$logroot/tmp

#
#   Name of log file to use:
#
logfile=$logroot/logs/access_log
#
#   Name of file to write this month's wwwstat results to:
#
wwwhtml=$docroot/stats/archive
#
#   Names of executable wwwstat program:
#
wwwstat=$clyde_bin/wwwstat

#==============================================================================
echo '['`date`'] wwwstat_month.sh: Starting' 

latest_logs=`\ls $logroot/log_archive/access_log.* | tail -31`
thisday=`date +%d`
thismonth=`date +%h`
thisyear=`date +%y`

lastyear=$thisyear

case "$thismonth" in
  Jan) lastmonth="Dec"; lastyear=`expr $thisyear - 1` ;;
  Feb) lastmonth="Jan" ;;
  Mar) lastmonth="Feb" ;;
  Apr) lastmonth="Mar" ;;
  May) lastmonth="Apr" ;;
  Jun) lastmonth="May" ;;
  Jul) lastmonth="Jun" ;;
  Aug) lastmonth="Jul" ;;
  Sep) lastmonth="Aug" ;;
  Oct) lastmonth="Sep" ;;
  Nov) lastmonth="Oct" ;;
  Dec) lastmonth="Nov" ;;
esac
  
echo '['`date`'] wwwstat_month.sh: stats for '$lastmonth' '$lastyear

#
# Remove all but last month's hits.  Remove any hits for this month.
#

touch $tmp/log.$$
nice -9 \gunzip -c $latest_logs | \
 nice -9 grep "[0-9]/$lastmonth/19$lastyear" >> $tmp/log.$$

#
# Create new uptodate wwwstats.html files by running wwwstat.
#
# 1: Build a stats file for this month only, [Month].[Year].wwwstats.html
# 2: Built a stats for this year, [Year].wwwstats.html, by merging this
#    month with existing yearly file.
#
nice -9 $wwwstat -U $tmp/log.$$ > $wwwhtml/$lastmonth.$lastyear.wwwstats.html
if (test $lastmonth = "Jan") then
  cp $wwwhtml/$lastmonth.$lastyear.wwwstats.html $wwwhtml/$lastyear.wwwstats.html  
else
  nice -9 $wwwstat -U -i $wwwhtml/$thisyear.wwwstats.html $tmp/log.$$ > $wwwhtml/$thisyear.a.wwwstats.html
  mv $wwwhtml/$thisyear.wwwstats.html $wwwhtml/$thisyear.wwwstats.old.html
  mv $wwwhtml/$thisyear.a.wwwstats.html $wwwhtml/$thisyear.wwwstats.html
fi
#
# Remove temporary files and copy result to HTML archive.
#
\rm -f $tmp/log.$$
chmod 664 $wwwhtml/$thisyear.wwwstats.html
chmod 664 $wwwhtml/$lastmonth.$lastyear.wwwstats.html

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