#!/bin/sh
#
# logrot - SH(1) shell script for periodical Apache HTTP Server Log Rotation
# for updates check http://go.to/ninuzzo/unix/logrot/
# version 1.0
#
# Copyright (c) 2003  Antonio Bonifati
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

# use
#   sh -x logrot.sh
# for debugging or uncomment this
#set -x

#
# default configuration follows
#
accesslogname=access.log
errorlogname=error.log
vhostslogdir=/var/log/vhosts

# use gzip to compress the old logs
# to save space if available
# add -9 at the end for best compression (slowest)
# you can also use bzip2 (replace gzip with bzip2)
# comment out if you don't want to compress the
# old logs as default
zip=gzip

# how many seconds to wait
# while Apache finishes serving old requests
# must be an integer
sec=1200

# how many archive files to keep
# besides the log file itself
count=12

# permission mode to use for new log files
mode=0640
#
# end of default configuration
#

#
# script begins
#
usage ( )
{ cat <<%
usage: `basename $0` [-h] [-n] [-a file] [-e file] [-d dir] [-m mode]
                 [-z prg] [-s seconds] [-c int] dir ...

%
  exit
}

# a shell function to rotate any number of log files
rotate ( )
{ for prefix
  do
     # trick to count the number of dots in prefix plus one
     dots=$((`echo $prefix | sed 's/\\./\\
/g' | wc -l`))

     for logfile in `ls $prefix.[0-9]* 2>/dev/null | sort -t. +${dots}nr`
     do
        ext=`echo $logfile | sed "s/^$prefix\.[0-9]*\(.*\)/\1/"`
        num=$((`echo $logfile | sed "s/^$prefix\.\([0-9]*\).*/\1/"`+1))
        if test $num -ge $count
        then rm $logfile
        else
             mv $logfile $prefix.$num$ext
        fi
     done
     mv $prefix $prefix.0
  done
}

# user can override default configuration with command line switches
test $1 || usage
while test $1
do case $1 in
   -a)  shift; accesslogname=$1 ;;
   -e)  shift; errorlogname=$1 ;;
   -d)  shift; vhostslogdir=$1 ;;
   -m)  shift; mode=$1 ;;
   -z)  shift; zip=$1 ;;
   -n)  zip= ;;
   -s)  shift; sec=$1 ;;
   -c)  shift; count=$1 ;;
   -h|--help)  usage ;;
   -*)  echo \'unknown flag $1\' ;;
   *) break ;;
   esac
   shift
done

for vhost
do
   # change to the vhost log directory
   cd $vhostslogdir/$vhost

   # rotate logs for this vhost
   rotate $accesslogname $errorlogname
done

# restart Apache
# this must be done BEFORE doing any processing on the log files
apachectl graceful
# wait for some time while Apache finishes serving old requests
sleep $sec
for vhost
do
   # change to the vhost log directory
   cd $vhostslogdir/$vhost

   # modifies the newly opened log file modes
   chmod $mode $accesslogname $errorlogname
done

# from here you can do whatever you want with
# the log files with a .0 extension

test $zip && for vhost
do
   # change to the vhost log directory
   cd $vhostslogdir/$vhost

   # old log compression
   $zip $accesslogname.0 $errorlogname.0
done
#
# script ends
#

