#!/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 - Revised by Jake Oct. 97
#
#==============================================================================
#=======================================================
#
# 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=/home/A8F/jake/stat
#nova_bin=/project/C420/s/npac_http/tools/stats   NOT USED HERE!
#docroot=/servers-admin/npac_http/html
docroot=/home/A8F/jake/stat/html
#logroot=/e/p/C067
#logroot=/e/s/log
logroot=/project/C067
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:
#
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
wwwstat=$nova_bin/wwwstat

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

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

\rm -f $tmp/*

wwwtmp=$tmp/wwwstats.$$
latest_logs=`\ls $logroot/log_archive/access_log.* | tail -28`
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/log1.$$
nice -9 grep -v "$thisday/$thismonth/19$thisyear" $logfile >> $tmp/log1.$$

echo '['`date`'] wwwstat.sh: searched log files'

\ls $logroot/log_archive/access_log.* | tail -28
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
echo '['`date`'] wwwstat.sh: built 28 day log'
nice -9 $wwwstat -U $tmp/log.$$ > $wwwtmp
echo '['`date`'] wwwstat.sh: built this months log'

#
# 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`'] wwwstat.sh: Finished'
exit
