#! /bin/sh

set -e

# calamaris: daily cron script.
# This script should be run before the one for squid or oops. According to the
# man page of run-parts this is okay: squid and oops come after calamaris in
# the alphabet.

# Date: 1998-10-07

# DEBUG=1

CONFFILE=/etc/calamaris/cron.conf
CALAMARIS=/usr/bin/calamaris
CALAMARIS_CONF_FILE=/etc/calamaris/calamaris.conf

if [ ! -x /usr/bin/calamaris ]; then
    exit 0
fi

CALAMARISOPTIONS="-a --config-file ${CALAMARIS_CONF_FILE}"
HTMLOPTIONS="-F html"
ME=/etc/cron.daily/calamaris
WEEKFILES=forweekly.1:forweekly.2:forweekly.3:forweekly.4:forweekly.5:forweekly.6:forweekly.0

# today
DAYOFWEEK=`date +"%w"`
DAYOFMONTH=`date +"%d" | bc -l`
# WEEKOFYEAR=`date +"%W"`
# MONTH=`date +"%B"`

# read configuration file: /etc/calamaris/cron.conf
# daily
eval `awk -F: \
'(!/#/) && ($1 == "daily") {
        print "DAYMAIL=" $2; print "DAYWEB=" $3;
        print "DAYDO=" $4; print "DAYTITLE=" $5;
}' $CONFFILE`
DAYWEBPATH=`dirname $DAYWEB`
DAYWEBFILE=`basename $DAYWEB`

# weekly
eval `awk -F: \
'(!/#/) && ($1 == "weekly") {
        print "WEEKMAIL=" $2; print "WEEKWEB=" $3;
        print "WEEKDO=" $4; print "WEEKTITLE=" $5;
}' $CONFFILE`
WEEKWEBPATH=`dirname $WEEKWEB`
WEEKWEBFILE=`basename $WEEKWEB`

# monthly
eval `awk -F: \
'(!/#/) && ($1 == "monthly") {
        print "MONTHMAIL=" $2; print "MONTHWEB=" $3;
        print "MONTHDO=" $4; print "MONTHTITLE=" $5;
}' $CONFFILE`
MONTHWEBPATH=`dirname $MONTHWEB`
MONTHWEBFILE=`basename $MONTHWEB`

# squid or oops?
CACHE=auto
eval `awk -F= \
'(!/#/) && ($1 == "cache") {
        print "CACHE=" $2;
}' $CONFFILE`

# cache file
CACHEFILE=access.log
eval `awk -F= \
'(!/#/) && ($1 == "cachefile") {
        print "CACHEFILE=" $2;
}' $CONFFILE`

# look for cache log files
if [ "$CACHE" = "auto" ]; then
    if [ -r /var/log/squid3/$CACHEFILE ]; then
	CACHE=squid
	CACHELOGDIR=/var/log/squid3
    fi
    if [ -r /var/log/squid/$CACHEFILE ]; then
	CACHE=squid
	CACHELOGDIR=/var/log/squid
    fi
    if [ -r /var/log/oops/$CACHEFILE ]; then
	CACHE=oops
	CACHELOGDIR=/var/log/oops
    fi
    if [ "$CACHE" = "auto" ]; then
	echo "/etc/cron.daily/calamaris: no cache log files found, exiting cleanly"
	exit 0
    fi
else
    CACHELOGDIR=/var/log/$CACHE
    if [ ! -r $CACHELOGDIR/$CACHEFILE ]; then
	echo "/etc/cron.daily/calamaris: no cache log files found in $CACHELOGDIR, exiting cleanly"
	exit 0
    fi
fi

# change to working dir
LOGDIR=/var/log/calamaris
cd $LOGDIR || exit 1

# if we need monthly or weekly reports save a summary
if [ "$WEEKDO" != "nothing" ]; then
    CALAMARISOPTIONSOLD="$CALAMARISOPTIONS"
    CALAMARISOPTIONS="$CALAMARISOPTIONS -o forweekly.$DAYOFWEEK"
    # Ensure that this file exists and is empty; if there are no entries in the
    # squid access.log, calamaris won't create the summary file, which causes
    # problems with the weekly and monthly summaries.
    :> forweekly.$DAYOFWEEK
else
    if [ "$MONTHDO" != "nothing" ]; then
	CALAMARISOPTIONSOLD="$CALAMARISOPTIONS"
	CALAMARISOPTIONS="$CALAMARISOPTIONS -o formonthly.$DAYOFMONTH"
    fi
fi

# do the daily report
case "$DAYDO" in
    nothing) if [ "$WEEKDO" != "nothing" ]; then
	cat $CACHELOGDIR/$CACHEFILE | \
	    nice -39 $CALAMARIS $CALAMARISOPTIONS > /dev/null
	fi
    ;;
    mail) if [ -x /usr/sbin/sendmail ]; then (
	    echo "To: $DAYMAIL"
            echo "From: Calamaris <root>"
	    cat $CACHELOGDIR/$CACHEFILE | \
		nice -39 $CALAMARIS $CALAMARISOPTIONS -F mail -H "$DAYTITLE"
	) | /usr/sbin/sendmail -t
	fi
    ;;
    web) cat $CACHELOGDIR/$CACHEFILE | \
	    nice -39 $CALAMARIS $CALAMARISOPTIONS $HTMLOPTIONS -H "$DAYTITLE" \
		--output-path $DAYWEBPATH --output-file $DAYWEBFILE
    ;;
    both) cat $CACHELOGDIR/$CACHEFILE | \
	    nice -39 $CALAMARIS $CALAMARISOPTIONS $HTMLOPTIONS -H "$DAYTITLE" \
		--output-path $DAYWEBPATH --output-file $DAYWEBFILE
	if [ -x /usr/sbin/sendmail ]; then (
	    echo "To: $DAYMAIL"
            echo "From: Calamaris <root>"
	    cat $CACHELOGDIR/$CACHEFILE | \
		nice -39 $CALAMARIS $CALAMARISOPTIONS -F mail -H "$DAYTITLE"
	) | /usr/sbin/sendmail -t
	fi
    ;;
    *) echo "the 'todo' for the daily Squid report in $CONFFILE"
	echo -n "is '$DAYDO' instead of one out of "
	echo "(nothing, mail, web, both)." >&2
	exit 1
    ;;
esac

if [ "$MONTHDO" != "nothing" -a "$WEEKDO" != "nothing" ]; then
    cp forweekly.$DAYOFWEEK formonthly.$DAYOFMONTH
fi

# do the weekly report on Sunday <=> $DAYOFWEEK==0
if [ -n "$CALAMARISOPTIONSOLD" ]; then
    CALAMARISOPTIONS="$CALAMARISOPTIONSOLD"
fi

if [ "$DAYOFWEEK" = "0" ]; then
    # make sure, all forweekly files exist, ie. touch them
    touch `echo $WEEKFILES | sed "s/:/ /g"`
    case "$WEEKDO" in
	nothing) 
	;;
	mail) if [ -x /usr/sbin/sendmail ]; then (
		echo "To: $WEEKMAIL"
                echo "From: Calamaris <root>"
		nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES \
		    -z -F mail -H "$WEEKTITLE"
	    ) | /usr/sbin/sendmail -t
            fi
	;;
	web) nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES \
		-z $HTMLOPTIONS -H "$WEEKTITLE" \
		--output-path $WEEKWEBPATH --output-file $WEEKWEBFILE
	;;
	both) if [ "$DEBUG" = "1" ]; then
		echo "Weekly stats."
		echo "Doing web stats: nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES -zwH \"$WEEKTITLE\" into $WEEKWEB"
	    fi
	    nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES \
		-z $HTMLOPTIONS -H "$WEEKTITLE" \
		--output-path $WEEKWEBPATH --output-file $WEEKWEBFILE
	    if [ "$DEBUG" = "1" ]; then
		echo "Doing mail stats: nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES -zmH \"$WEEKTITLE\""
	    fi
	    if [ -x /usr/sbin/sendmail ]; then (
		echo "To: $WEEKMAIL"
                echo "From: Calamaris <root>"
		nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES \
		    -z -F mail -H "$WEEKTITLE"
	    ) | /usr/sbin/sendmail -t
	    fi
	    if [ "$DEBUG" = "1" ]; then echo "Finished weekly stats."; fi
	;;
	*) echo "the 'todo' for the weekly Squid report in $CONFFILE"
	    echo -n "is '$WEEKDO' instead of one out of "
	    echo "(nothing, mail, web, both)." >&2
	    exit 1
	;;
    esac
    # remove all forweekly files (could disturb next week's statistics if
    # computer is switched of one day)
    rm -f `echo $WEEKFILES | sed "s/:/ /g"`
fi

# do the monthly report
if [ "$DAYOFMONTH" = "1" ]; then
    i=2
    MONTHFILES=formonthly.1
    yesterday=`date +"%d" -d "yesterday"`
    end=`echo 1+$yesterday | bc -l`
    while [ "$i" != $end ]; do
	MONTHFILES=$MONTHFILES:formonthly.$i
	i=`echo 1+$i | bc -l`
    done
    # make sure, all formonthly files exist, ie. touch them
    touch `echo $MONTHFILES | sed "s/:/ /g"`
    case "$MONTHDO" in
	nothing)
	;;
	mail) if [ -x /usr/sbin/sendmail ]; then (
		echo "To: $MONTHMAIL"
                echo "From: Calamaris <root>"
		nice -39 $CALAMARIS $CALAMARISOPTIONS -i $MONTHFILES \
		    -z -F mail -H "$MONTHTITLE"
	      ) | /usr/sbin/sendmail -t
              fi
        ;;
	web) nice -39 $CALAMARIS $CALAMARISOPTIONS -i $MONTHFILES \
	        -z $HTMLOPTIONS -H "$MONTHTITLE" \
		--output-path $MONTHWEBPATH --output-file $MONTHWEBFILE
        ;;
	both) nice -39 $CALAMARIS $CALAMARISOPTIONS -i $MONTHFILES \
	        -z $HTMLOPTIONS -H "$MONTHTITLE" \
		--output-path $MONTHWEBPATH --output-file $MONTHWEBFILE
	      if [ -x /usr/sbin/sendmail ]; then (
		echo "To: $MONTHMAIL"
                echo "From: Calamaris <root>"
		nice -39 $CALAMARIS $CALAMARISOPTIONS -i $MONTHFILES \
		    -z -F mail -H "$MONTHTITLE"
	      ) | /usr/sbin/sendmail -t
	      fi
	;;
	*) echo "the 'todo' for the weekly Squid report in $CONFFILE"
	    echo -n "is '$MONTHDO' instead of one out of "
	    echo "(nothing, mail, web, both)." >&2
	    exit 1
	;;
    esac
    # remove all formonthly files (could disturb next month's statistics if
    # computer is switched of one day)
    rm -f formonthly.*
fi

exit 0
