#! /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


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

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

# 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 Bas_info memset libc.so.6 strcpy printf __register_frame pthread_create sigemptyset pthread_getspecific strerror __ctype_b fgets pthread_key_delete sysinfo __frame_state_for __register_frame_table rename fprintf strcat pthread_once pthread_key_create stdin unlink sigaction strcmp __deregister_frame fclose stderr __errno_location exit fopen _IO_stdin_used __libc_start_main __ctype_tolower pthread_setspecific GLIBC_2.0 GLIBC_2.1                                                            *       
ii     f                
ii     p   
ii     f     -       -       -  $     -  %     *       +        +       +  	     +$  
     +0       +<  
     +H       +T       +`       +l       +x       +       +       +       +       +       +       +       +        +  !     +  "     +  #     ,  &     ,  '     ,   )     ,,  .    㿘@    @  :   @        #X\  B  B  I#<!Ț @ HP       㿘  b    	    `    H 
" $"?@    "  `    H!     @ ǐ" $b  㿘  㿘  H        "@ da  㿘  㻀!  	 @ H # @ G#  ''' ' ''  *' +  I)  I'  I%  I @ Gْa耢     '@ Gђ!  2  '@ Gʒ  2   '@ G   	   ?  ߢ`  ,`  I@ Gݐ"@ G @	
 ?  	
     I  ! ' 
@ GbX   '@#  I@ Gb`@ G    @ G @ GȐ @@ G   2    I@ G"@ G @ G  I@ Gb*! @ G     !  I@ G"@ G{   @ G @ G@ G      '耢 #   	@ G    L a( 	, L* ` 2, M `    *` 	  9       I"@ Gh    	! L  * ` 2 ?   ,     ?$@ ''         `       ''`    a   I@ Gc(  ' `  '         !@ G'     I#@@ F      !L      : * ` " $  '!  '`         Ma* + " *M ,    
*"@	*
,  L  a* `  
 ! L  * ` 2 L   :      L  a* `  	! L  * ` 2 L    " .  @ Fْ $@	  	c@ F   2$M   C$@ ''              %! -'     a  I@ Fhc(  ` '  '            I@ F|#