#! /bin/sh

# Copyright (c) 1999 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.01, the previous mainlog.01 becoming mainlog.02, 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
#########################################################################


# See if this installation is using the esoteric "USE_NODE" feature of Exim,
# in which it uses the host's name as a suffix for the configuration file name.
# Set $hostsuffix if a suffixed file is found.

configure_file_use_node=
if [ "$configure_file_use_node" = "yes" ]; then
  host=`uname -n`
  if [ ! "$host" != "" ]; then
      if [ -f /etc/exim/exim.conf.$host ]; then
          hostsuffix=.$host
      fi
  fi
fi

# Set the configuration file name

config=/etc/exim/exim.conf$hostsuffix

# Determine if the log file path is set, and where the spool directory is. The
# strings /etc/exim/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' $config | sed 's/.*=[	  ]*//'`
if test "$exim_path" = ""; then exim_path=/usr/sbin/exim; fi

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

# If log_file_path contains only "syslog" then no Exim log files are in use.
# We can't cycle anything. Complain and give up.

if [ "$log_file_path" = "syslog" ] ; then
  echo "*** Exim is logging to syslog - no log files to cycle ***"
  exit 1
fi

# Otherwise, remove ":syslog" or "syslog:" (some spaces allowed) and inspect
# what remains. The simplistic regex originally used failed when a filename
# contained "syslog", so we have to use three less general ones, because sed
# doesn't have much power in its regexs.

log_file_path=`echo "$log_file_path" | \
  sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'`

# 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`

# These statements work fine in the Bourne or Korn shells, but not in Bash.
# So for the benefit of systems whose /bin/sh is really Bash, they have been
# changed to a messier form.

# 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; }'`

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

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

# Now do the job. Originally the files were numbered 1, 2, etc., but it was
# pointed out that using 01, 02, etc. would be better. So that things continue
# to work, we first of all rename any files we find that have old names. This
# code was written in February 1998. It can be removed at some point in the01.01             01.01             01.01             01.01             01.01             01.01             01.01    .shstrtab .interp .note.ABI-tag .hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .text .fini .rodata .PARISC.unwind .data .eh_frame .dynamic .ctors .dtors .got .sbss .bss .comment .note                                                          
                                                 !       $  $                  '                          /           b                  7o    	6  	6                   Do    	  	   @                S       	  	   <                ]       
4  
4                 g            H                  m       
  
  Al                  s       Np  Np   (                  y       N  N  	                         XH  XH                         \  \                            \  \                            \  \                          ]  ]                            ]  ]                     b       ]  ]                          _h  _h                           _p  _p                             _p  _p  p                               _p                                  `6                                  `                                                                  ./usr/sbin/exinext                                                                                  0100755 0000000 0000000 00000011205 10170544443 013071  0                                                                                                    ustar   root                            root                                                                                                                                                                                                                   #! /bin/sh

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


# A shell+perl script to fish out the next retry time for a given domain;
# it fi