#!/bin/sh
#
# apache	Rotate the apache logsfiles daily.
#
set -x 

# How many days of logs to keep.
DAYS=35

[ -f /etc/apache/httpd.conf ] || exit 0
umask 022

# This looks for lines in the conf files like: FooLog /some/where
LOGS=$(egrep -h "^[:space:]*[A-Za-z]*Log /" /etc/apache/*.conf \
    | sed s/^\ *//g | cut -f 2 -d " ")

if [ "$LOGS" = "" ]
then
    LOGS="/var/log/apache/access.log /var/log/apache/error.log"
    [ -d /var/log/apache ] || exit 0
fi

SERVERROOT=$(egrep -h "^[:space:]*ServerRoot " /etc/apache/httpd.conf | \
    cut -f 2 -d " ")
if [ "$SERVERROOT" != "" ]
then
    cd $SERVERROOT
fi

USR=$(egrep -h "^User [a-z]" httpd.conf | cut -f 2 -d " ")
if [ "$USR" = "" ]
then
    USR="root"
fi

GRP=$(egrep -h "^Group [a-z]" httpd.conf | cut -f 2 -d " ")
if [ "$GRP" = "" ]
then
    GRP="www-data"
fi

for LOG in $LOGS
do
    if [ -f $LOG ]
    then
	savelog -c $DAYS -m 664 -u $USR -g $GRP $LOG > /dev/null
    fi
done

# Send a reload signal to the apache server.
trap "" 1
killall -HUP /usr/sbin/apache 2> /dev/null

exit 0
