#!/bin/csh -f

#==============================================================================
#
#	Script to roll over the logs on NPAC web server.  This is designed to
#	run as root on clyde.  It must run as root because it has to kill off
#	and restart the HTTPd, bound to port 80.
#
#==============================================================================
#
#=======================================================
#
# These five definitions specify the binary paths for
# nova and clyde and the writable document root for the
# web server, log files and HTTPd binary.
#
#=======================================================
#
set clyde_bin=/srvtools/bin/stats
#set nova_bin=/project/C420/s/npac_http/tools/stats  NOT USED HERE!
set docroot=/servers-admin/npac_http/html
#set logroot=/e/p/C067
set logroot=/e/s/log
set serverroot = /srvtools/bin
set tmp=$logroot/tmp

#
#	Paths to files and directories
#
echo '['`date`'] rollover.sh: Starting'
set TEST = 0

if ($TEST ) set logroot = /tmp/log_test                      # Test rollover.
set log_file = access_log
set ref_file = referer_log
set agt_file = agent_log
set logarch = $logroot/log_archive
set loghtml = $docroot/stats/archive
if ($TEST ) set loghtml = /tmp/log_test/html                 # Test rollover.
set wwwstat = $clyde_bin/wwwstat

#==============================================================================
#	You should not have to change anything below here.
#==============================================================================
#
#       Work out this year and this month
#	=================================
#
set thisyear = `date +%y`
set thismonth = `date +%b`  
set thisday = `date +%d`  
set thisdate = "$thisday/$thismonth/19$thisyear"
set thisdtg = `date +%y%m%d`

cd $logroot/logs

#
# Copy log files to tmp.  This means we have a backup if all else fails.
#
\cp *_log tmp

#
#	Check to see if archive directory is there!
#	===========================================
#
if (! -d $logarch ) then
  echo '['`date`'] rollover.sh: No directory '$logarch
  exit
endif

#
#	Stop HTTPd
#	==========
#
if ( $TEST == 0) then 
  echo "STOPPING HTTPD: `date`"
  $serverroot/http_stop80
endif

#
#	Loop over each log file and archive it
#	======================================
#
foreach log ( $log_file $ref_file $agt_file )
#
#	Make sure access log only contains this week.
#	  Remove todays hits from archive and save it.
#	  Remove today's hits from archive and put them back in the log file.
#	
#
  echo $log
  mv $log $tmp/log.$$
  grep -v "$thisdate" $tmp/log.$$ > $logarch/$log.$thisdtg
  grep "$thisdate" $tmp/log.$$ > $log
end

#
#	Keep last 1000 lines of error and counter log and delete the rest.
#
foreach log ( error_log counter_log )
  mv $log $tmp/$log.$$
  head -1000 $tmp/$log.$$ > $log
  \rm -f $tmp/$log.$$
end
#
#	Start the server
#	================
#
if ( $TEST == 0) then 
  echo "STARTING HTTPD: `date`"
  /$serverroot/http_start80
endif

#
#	Update the list of unique agents
#	================================
#
cut -d']' -f,2, $logarch/agent_log.$thisdtg | \
   sed -e 's/ //' -e 's/via.*//' | sort -u > $tmp/agents.$$
if ( -f $loghtml/agents.txt ) then
  cat $loghtml/agents.txt >> $tmp/agents.$$
endif
sort -u $tmp/agents.$$ >! $loghtml/agents.txt
\rm -f $tmp/agents.$$
chmod 644 $loghtml/agents.txt
#
#	Compress log files
#	==================
#
foreach log (  $log_file $ref_file $agt_file )
  nice -9 gzip -9 $logarch/$log.$thisdtg
end

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