#!/bin/csh -f
# $Id: newcmlog,v 1.3 1991/10/01 05:02:25 sean Rel $
#****************************************************************************
# 	  Copyright (c) 1988 Thinking Machines Corporation, Inc.,			    *
#		of Cambridge, Mass.   All rights reserved.						    *
#																		  	*
#  This notice is intended as a precaution against inadvertent publication  *
#  and does not constitute an admission or acknowledgement that publication *
#  has occurred or constitute a waiver of confidentiality.		     		*
#									     									*
#  Connection Machine software is the proprietary and confidential property *
#  of Thinking Machines Corporation.					     				*
#***************************************************************************/
#

# Get script name, local host name, logger-pid file name
set my_name=`basename $0`
set hostname=`hostname`
set loggerpidfile="${hostname}.cm-logger.pid"
set acctpidfile="${hostname}.cm-acctd.pid"

# Set defaults before looking for optional arguments
# default directory to find log files in
set log_dir="/usr/spool/cm"

# default name of central accounting log file (in log_dir)
set central_acct_file="central.acct_log"

# default name of central error log file (in log_dir)
set central_error_file="central.error_log"

# default name of cm-acctd accounting file
set cm_acctd_file="cmacct"

# Now parse any command-line arguments
while ($#argv > 0)
	switch ($1)
		case -a*:		# name of central accounting log file
			set central_acct_file=$2
			shift
			breaksw

		case -d*:		# directory to find log files in
			set log_dir=$2
			shift
			breaksw

		case -e*:		# name of central error log file
			set central_error_file=$2
			shift
			breaksw

		case -h*:		# help!
			set print_help=true
			breaksw

		case -p*:		# pid file name
			set acctpidfile=$2
			shift
			breaksw

		case -*:		# unrecognized option
			echo "ERROR in ${my_name}: Unrecognized option '$1'"
			set print_help=true
			breaksw

		default:
			echo "ERROR in ${my_name}: Unrecognized argument '$1'"
			set print_help=true
			breaksw
	endsw
	shift
end

# If the command line was bogus or the user asked for help...
if ($?print_help) then
	echo "Usage: ${my_name} [-d log_directory] [-help]"
	echo "                [-a central_accounting_log_file] "\
		 "[-e central_error_log_file]"\
		 "[-p cm-acctd-pid-file]"
	exit(-1)
endif

# Get into the directory that contains log files; give up if there is none
# Since errors on cd cause the script to abort, check for errors in advance
if (! -d $log_dir) then
    echo "ERROR in ${my_name}: cannot find log directory $log_dir"
	exit(-2)
else if (! -x $log_dir) then
	echo "ERROR in ${my_name}: can't cd into log directory $log_dir"
	echo "                   because it does not have execute permission."
	exit(-3)
else
	cd $log_dir
endif

# Suppress "No match" msg in case there are no log files to match the pattern
set nonomatch

# Make sure new log files get created with mode "666"
umask 0

# The following four sections roll the four different types of log files:
# 1. local acccounting log file
# 2. local error log file
# 3. central accounting log file (if it's present)
# 4. central error log file (if it's present)
# The basic actions for each type of log file are the same, but the
# sections are broken out to make it easier for sites to CUSTOMIZE the
# actions for each specific type of log file.  For example, a site might
# wish to mail out only the central error log file.

# Roll the local accounting log file.
# This rotation keeps 8 old local accounting log files.
# CUSTOMIZE this portion if you want to keep a different number of local
# accounting log files.
set log=$hostname.acct_log
rm -f ${log}.8
# use the -e test because mv -f, unlike rm -f, complains if the
# file to mv doesn't exist
if (-e ${log}.7) mv -f ${log}.7 ${log}.8
if (-e ${log}.6) mv -f ${log}.6 ${log}.7
if (-e ${log}.5) mv -f ${log}.5 ${log}.6
if (-e ${log}.4) mv -f ${log}.4 ${log}.5
if (-e ${log}.3) mv -f ${log}.3 ${log}.4
if (-e ${log}.2) mv -f ${log}.2 ${log}.3
if (-e ${log}.1) mv -f ${log}.1 ${log}.2
if (-e ${log}) mv -f ${log} ${log}.1
cp /dev/null ${log}		# create empty new log file
# CUSTOMIZE this line if you want to mail out completed local acct log file
# cat ${log}.1 | mail -s "log file $log.1" dv-site-logs@think.com

# Roll the local error log file.
# This rotation keeps 8 old local error log files.
# CUSTOMIZE this portion if you want to keep a different number of local
# error log files.
set log=$hostname.error_log
rm -f ${log}.8
# use the -e test because mv -f, unlike rm -f, complains if the
# file to mv doesn't exist
if (-e ${log}.7) mv -f ${log}.7 ${log}.8
if (-e ${log}.6) mv -f ${log}.6 ${log}.7
if (-e ${log}.5) mv -f ${log}.5 ${log}.6
if (-e ${log}.4) mv -f ${log}.4 ${log}.5
if (-e ${log}.3) mv -f ${log}.3 ${log}.4
if (-e ${log}.2) mv -f ${log}.2 ${log}.3
if (-e ${log}.1) mv -f ${log}.1 ${log}.2
if (-e ${log}) mv -f ${log} ${log}.1
cp /dev/null ${log}		# create empty new log file
# CUSTOMIZE this line if you want to mail out completed local error log file
# cat ${log}.1 | mail -s "log file $log.1" dv-site-logs@think.com

# Roll the central accounting log file, if it's present.
# This rotation keeps 8 old central accounting log files.
# CUSTOMIZE this portion if you want to keep a different number of central
# accounting log files.
set log=$central_acct_file
rm -f ${log}.8
# use the -e test because mv -f, unlike rm -f, complains if the
# file to mv doesn't exist
if (-e ${log}.7) mv -f ${log}.7 ${log}.8
if (-e ${log}.6) mv -f ${log}.6 ${log}.7
if (-e ${log}.5) mv -f ${log}.5 ${log}.6
if (-e ${log}.4) mv -f ${log}.4 ${log}.5
if (-e ${log}.3) mv -f ${log}.3 ${log}.4
if (-e ${log}.2) mv -f ${log}.2 ${log}.3
if (-e ${log}.1) mv -f ${log}.1 ${log}.2
if (-e ${log}) mv -f ${log} ${log}.1
# create empty new log file, but only if old log file existed
if (-e ${log}.1) cp /dev/null ${log}
# CUSTOMIZE this line if you want to mail out completed central acct log file
# cat ${log}.1 | mail -s "log file $log.1" dv-site-logs@think.com

# Roll the central error log file, if it's present.
# This rotation keeps 8 old central error log files.
# CUSTOMIZE this portion if you want to keep a different number of central
# error log files.
set log=$central_error_file
rm -f ${log}.8
# use the -e test because mv -f, unlike rm -f, complains if the
# file to mv doesn't exist
if (-e ${log}.7) mv -f ${log}.7 ${log}.8
if (-e ${log}.6) mv -f ${log}.6 ${log}.7
if (-e ${log}.5) mv -f ${log}.5 ${log}.6
if (-e ${log}.4) mv -f ${log}.4 ${log}.5
if (-e ${log}.3) mv -f ${log}.3 ${log}.4
if (-e ${log}.2) mv -f ${log}.2 ${log}.3
if (-e ${log}.1) mv -f ${log}.1 ${log}.2
if (-e ${log}) mv -f ${log} ${log}.1
# create empty new log file, but only if old log file existed
if (-e ${log}.1) cp /dev/null ${log}
# CUSTOMIZE this line if you want to mail out completed central error log file
# cat ${log}.1 | mail -s "log file $log.1" dv-site-logs@think.com

# Roll the log files created by cm-acctd, if present.
# This rotation keeps 8 old cm-acctd log files.
# CUSTOMIZE this portion if you want to keep a different number of cm-acctd
# log files.
set log=$cm_acctd_file
rm -f ${log}.8
# use the -e test because mv -f, unlike rm -f, complains if the
# file to mv doesn't exist
if (-e ${log}.7) mv -f ${log}.7 ${log}.8
if (-e ${log}.6) mv -f ${log}.6 ${log}.7
if (-e ${log}.5) mv -f ${log}.5 ${log}.6
if (-e ${log}.4) mv -f ${log}.4 ${log}.5
if (-e ${log}.3) mv -f ${log}.3 ${log}.4
if (-e ${log}.2) mv -f ${log}.2 ${log}.3
if (-e ${log}.1) mv -f ${log}.1 ${log}.2
if (-e ${log}) mv -f ${log} ${log}.1
# create empty new log file, but only if old log file existed
if (-e ${log}.1) cp /dev/null ${log}
# mailing files created by cm-acctd isn't recommended, since the files are
# binary.  They can be mailed if piped through uuencode first, thusly:
# cat ${log}.1 | uuencode | mail -s "cmacct file $log.1" cm-sys-admin@your-site

#
# If a daemon pid file exists, tell the daemon that's (presumably) associated
# with it to close and reopen log files, so it will see the rotation.
# This will print the following error if the daemon was ungracefully killed:
# <pid_number>: No such process
#
if (-r $loggerpidfile) then
	kill -HUP `cat $loggerpidfile`
else
	echo "WARNING in ${my_name}: file ${log_dir}/$loggerpidfile doesn't exist."
	echo "This probably means that no cm-logger daemon is running."
endif

if (-r $acctpidfile) then
	kill -HUP `cat $acctpidfile`
else
	echo "WARNING in ${my_name}: file ${acctpidfile} doesn't exist."
	echo "This probably means that no cm-acctd daemon is running."
endif
exit(0)

# Tell gmacs to use narrow tab width, for easier reading
### Local Variables: ***
### tab-width:4 ***
### End: ***
