[Buzilla 376] Preliminary DKIM support
[exim.git] / src / src / exicyclog.src
index 092b682c6586bc15a125f3d3d9c3d96e026a41f8..3fdcf4ffdcb4bf8738065d73c6570ba6abd5e52d 100644 (file)
@@ -1,9 +1,13 @@
 #! /bin/sh
-# $Cambridge: exim/src/src/exicyclog.src,v 1.2 2004/12/21 16:26:31 ph10 Exp $
+# $Cambridge: exim/src/src/exicyclog.src,v 1.5 2006/03/16 11:51:10 ph10 Exp $
 
-# Copyright (c) 2004 University of Cambridge.
+# Copyright (c) 2006 University of Cambridge.
 # See the file NOTICE for conditions of use and distribution.
 
+# This script takes the following command line arguments:
+# -l dir       Log file directory
+# -k days      Number of days to keep the log files
+
 # Except when they appear in comments, the following placeholders in this
 # source are replaced when it is turned into a runnable script:
 #
@@ -24,9 +28,9 @@
 # This is a 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. When the number to keep is 
-# greater than 99 (not common, but some people do it), three digits are used 
-# (e.g. mainlog.001). The same shuffling happens to the reject logs. All 
+# and so on, up to the limit configured here. When the number to keep is
+# greater than 99 (not common, but some people do it), three digits are used
+# (e.g. mainlog.001). The same shuffling happens to the reject logs. All
 # renamed files with numbers greater than 1 are compressed.
 
 # This script should be called regularly (e.g. daily) by a root crontab
 # 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.
+# files that are required to be kept. Its value can be overridden by the -k
+# command line option. "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=EXICYCLOG_MAX
 compress=COMPRESS_COMMAND
@@ -54,6 +59,23 @@ rm=RM_COMMAND
 # End of editable lines
 #########################################################################
 
+# Sort out command line options.
+
+while [ $# -gt 0 ] ; do
+  case "$1" in
+  -l) log_file_path=$2
+      shift
+      ;;
+  -k) keep=$2
+      shift
+      ;;
+   *) echo "** exicyclog: unknown option $1"
+      exit 1
+      ;;
+   esac
+   shift
+done
+
 # Some operating systems have different versions in which the commands live
 # in different places. We have a fudge that will search the usual suspects if
 # requested.
@@ -112,16 +134,20 @@ done
 # Determine if the log file path is set, and where the spool directory is.
 # 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. BEWARE: a tab character is needed in the command below. It has had a
-# nasty tendency to get lost in the past. Use a variable to hold a space and a
-# tab to keep the tab in one place.
+# path, unless log_file_path was set above by a command line option. BEWARE: a
+# tab character is needed in the command below. It has had a nasty tendency to
+# get lost in the past. Use a variable to hold a space and a tab to keep the
+# tab in one place.
 
 st='    '
 exim_path=`grep "^[$st]*exim_path" $config | sed "s/.*=[$st]*//"`
 if test "$exim_path" = ""; then exim_path=BIN_DIRECTORY/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" = "" ] ; then
+  log_file_path=`$exim_path -C $config -bP log_file_path | sed 's/.*=[  ]*//'`
+fi
 
 # If log_file_path contains only "syslog" then no Exim log files are in use.
 # We can't cycle anything. Complain and give up.
@@ -139,10 +165,19 @@ fi
 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 is empty, try and get the compiled in default by using
+# /dev/null as the configuration file.
+
+if [ "$log_file_path" = "" ]; then
+  log_file_path=`$exim_path -C /dev/null -bP log_file_path | sed 's/.*=[  ]*//'`
+  log_file_path=`echo "$log_file_path" | \
+    sed 's/^ *:\{0,1\} *syslog *:\{0,1\} *//;s/: *syslog *:/:/;s/: *syslog *$//'`
+fi
+
+# If log_file_path is still empty, the logs we are interested in are probably
+# 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
@@ -157,7 +192,7 @@ fi
 
 # Get into the log directory to do the business.
 
-cd $logdir
+cd $logdir || exit 1
 
 # If there is no main log file, do nothing.
 
@@ -209,13 +244,13 @@ if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
 while [ $count -gt 1 ]; do
   old=`expr $count - 1`
   if [ $keep -gt 99 ]; then
-    if   [ $old -lt 10 ]; then oldt=00$old 
+    if   [ $old -lt 10 ]; then oldt=00$old
     elif [ $old -lt 100 ]; then oldt=0$old
     else oldt=$old
-    fi   
-  else 
+    fi
+  else
     if [ $old -lt 10 ]; then oldt=0$old; else oldt=$old; fi;
-  fi   
+  fi
   if [ -f $mainlog.$oldt ]; then
     $mv $mainlog.$oldt $mainlog.$countt
   elif [ -f $mainlog.$oldt.$suffix ]; then
@@ -254,10 +289,10 @@ while [ $count -le $keep ]; do
     if   [ $count -lt 10 ]; then countt=00$count
     elif [ $count -lt 100 ]; then countt=0$count
     else countt=$count
-    fi    
-  else 
+    fi
+  else
     if [ $count -lt 10 ]; then countt=0$count; else countt=$count; fi
-  fi   
+  fi
   if [ -f $mainlog.$countt ]; then $compress $mainlog.$countt; fi
   if [ -f $mainlog.$countt.$suffix ]; then
     $chown $user:$group $mainlog.$countt.$suffix