#!/bin/sh

#==============================================================================
#
#   Shell script to do daily 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=/usr/local/servers/npac_http/tools/stats
nova_bin=/project/C420/s/npac_http/tools/stats
docroot=/e/p/C420/p/html
logroot=/e/p/C067

#
#   Name of log file to use:
#
logfile=$logroot/logs/access_log
#
#   Name of file to write this month's wwwstat results to:
#
wwwmonth=$docroot/stats/wwwstats.html
#
#   Name of file to write longer, last 28 day wwwstat results to:
#
wwwresult=$docroot/stats/wwwstats_28.html
#
#   Names of executable wwwstat program:
#
wwwstat=$clyde_bin/wwwstat

#==============================================================================

echo '['`date`'] Starting: wwwstat.sh'

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

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

touch /tmp/log.$$
nice -9 \gunzip -c $latest_logs | \
   nice -9 grep "[0-9]/$thismonth/19$thisyear" >> /tmp/log.$$
if (test $thisday = "01") then
  echo "  1st of the $thismonth 19$thisyear"
  touch /tmp/log.$$
  nice -9 grep "[0-9]/$thismonth/19$thisyear" $logfile >> /tmp/log.$$ 
else
  echo "  Normal day $thisday $thismonth 19$thisyear"
  nice -9 grep "[0-9]/$thismonth/19$thisyear" $logfile | \
    nice -9 grep -v "$thisday/$thismonth/19$thisyear" >> /tmp/log.$$
fi

touch /tmp/log.$$
nice -9 grep -v "$thisday/$thismonth/19$thisyear" $logfile >> /tmp/log1.$$

wc -l /tmp/log.$$

#
# Create new uptodate wwwstats.html file by running another script.
#
# Build a stats file for this month only $wwwtmp.
# Build a stats file for last four weeks $wwwresult.
#
nice -9 $wwwstat -U /tmp/log1.$$ $latest_logs > $wwwresult
nice -9 $wwwstat -U /tmp/log.$$ > $wwwtmp

#
# Remove temporary files and copy result to HTML archive.
#
\rm -f /tmp/log.$$
\rm -f /tmp/log1.$$
\mv -f $wwwtmp $wwwmonth
chmod 664 $wwwresult
chmod 664 $wwwmonth

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