#! /bin/sh

# Copyright (c) 1997 University of Cambridge.
# See the file NOTICE for conditions of use and distribution.


# Shell script for cycling exim main and reject log files. Each time it is run,
# the files get "shuffled down" by one, the current one (e.g. mainlog) becoming
# mainlog.1, the previous mainlog.1 becoming mainlog.2, and so on, up to the
# limit configured here. The same happens to the reject logs. All those with
# numbers greater than 1 are compressed.

# This script should be called regularly (e.g. daily) by a root crontab
# entry of the form

# 1 0 * * *   /opt/exim/bin/exicyclog

# The following lines are generated from Exim's configuration file when
# this source is built into a script, but you can subsequently edit them
# without rebuilding things, as long are you are careful not to overwrite
# the script in the next Exim rebuild/install. "Keep" is the number of old log
# files that are required to be kept. "Compress" and "suffix" define your
# chosen compression method. The others are provided because the location
# of certain commands varies from OS to OS. Sigh.

keep=10
compress=/bin/gzip
suffix=gz

chown=/bin/chown
chgrp=/bin/chgrp
mv=/bin/mv
rm=/bin/rm

# End of editable lines


# Determine if the log file path is set, and where the spool directory is. The
# strings /etc/exim.conf and /usr/sbin below are replaced by their compile-
# time settings when this source is build into a script. Search for an
# exim_path setting in the configure file; otherwise use the bin directory.
# Call that version of Exim to find the spool directory and log file path.

exim_path=`grep '^[	 ]*exim_path' /etc/exim.conf | sed 's/.*=[	 ]*//'`
if test "$exim_path" = ""; then exim_path=/usr/sbin/exim; fi

spool_directory=`$exim_path -C /etc/exim.conf -bP spool_directory | sed 's/.*=[  ]*//'`
log_file_path=`$exim_path -C /etc/exim.conf -bP log_file_path | sed 's/.*=[  ]*//'`

# If log_file_path is empty, then the logs we are interested in are called
# "mainlog" and "rejectlog" in the directory called "log" in the spool
# directory. Otherwise we fish out the directory from the given path, and
# also the names of the logs.

if [ "$log_file_path" = "" ]; then
  logdir=$spool_directory/log
  mainlog=mainlog
  rejectlog=rejectlog
else
  logdir=`echo $log_file_path | sed 's?/[^/]*$??'`
  logbase=`echo $log_file_path | sed 's?^.*/??'`
  mainlog=`echo $logbase | sed 's/%s/main/'`
  rejectlog=`echo $logbase | sed 's/%s/reject/'`
fi

# Get into the log directory to do the business.

cd $logdir

# If there is no main log file, do nothing.

if [ ! -f $mainlog ]; then exit; fi

# Find out the owner and group of the main log file so that we can re-instate
# this on moved and compressed files, since some operating systems may change
# things. This is a tedious bit of code, but it should work both in operating
# systems where the -l option of ls gives the user and group, and those in which
# you need -lg. The condition is that, if the fifth field of the output from
# ls consists entirely of digits, then the third and fourth fields are the user
# and group.

a=`ls -lg $mainlog`
b=`ls -l  $mainlog`

user=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $3; }'`
group=`echo "$a\n$b\n" | awk 'BEGIN { OFS=""} { if ($5 ~ /^[0-9]+$/) print $4; }'`

# Now do the job.

if [ -f $mainlog.$keep ]; then $rm $mainlog.$keep; fi;
if [ -f $mainlog.$keep.$suffix ]; then $rm $mainlog.$keep.$suffix; fi;

if [ -f $rejectlog.$keep ]; then $rm $rejectlog.$keep; fi;
if [ -f $rejectlog.$keep.$suffix ]; then $rm $rejectlog.$keep.$suffix; fi;

while [ $keep -gt 1 ]; do
  old=`expr $keep - 1`
  if [ -f $mainlog.$old ]; then
    $mv $mainlog.$old $mainlog.$keep
    $compress $mainlog.$keep
    $chown $user $mainlog.$keep.$suffix
    $chgrp $group $mainlog.$keep.$suffix
  elif [ -f $mainlog.$old.$suffix ]; then
    $mv $mainlog.$old.$suffix $mainlog.$keep.$suffix
    $chown $user $mainlog.$keep.$suffix
    $chgrp $group $mainlog.$keep.$suffix
  fi
  if [ -f $rejectlog.$old ]; then
    $mv $rejectlog.$old $rejectlog.$keep
    $compress $rejectlog.$keep
    $chown $user $rejectlog.$keep.$suffix
    $chgrp $group $rejectlog.$keep.$suffix
  elif [ -f $rejectlog.$old.$suffix ]; then
    $mv $rejectlog.$old.$suffix $rejectlog.$keep.$suffix
    $chown $user $rejectlog.$keep.$suffix
    $chgrp $group $rejectlog.$keep.$suffix
  fi
  keep=$old
done

if [ -f $mainlog ]; then
  $mv $mainlog $mainlog.1
  $chown $user $mainlog.1
  $chgrp $group $mainlog.1
fi

if [ -f $rejectlog ]; then
  $mv $rejectlog $rejectlog.1
  $chown $user $rejectlog.1
  $chgrp $group $rejectlog.1
fi

# End of exicyclog
