#! /bin/sh

set -e

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

# Date: 1998-10-07

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

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

CALAMARISOPTIONS=-a
ME=/etc/cron.daily/calamaris
WEEKFILES=daily.1:daily.2:daily.3:daily.4:daily.5:daily.6:daily.0

SQUIDLOGDIR=/var/log/squid
cd $SQUIDLOGDIR || exit 1
if [ ! -r access.log ]; then
    exit 0
fi

LOGDIR=/var/log/calamaris
cd $LOGDIR || exit 1

# today
DAYOFWEEK=`date +"%w"`

# read configuration file: /etc/calamaris.conf
# is there a more elegant way to do this?
DAYMAIL=`awk -F: '(!/#/) && ($1 == "daily") { print $2; }' $CONFFILE`
DAYWEB=`awk -F: '(!/#/) && ($1 == "daily") { print $3; }' $CONFFILE`
DAYDO=`awk -F: '(!/#/) && ($1 == "daily") { print $4; }' $CONFFILE`
DAYTITLE=`awk -F: '(!/#/) && ($1 == "daily") { print $5; }' $CONFFILE`

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

# perhaps sometimes I will do this, 
# but as weekends and ends of months don't meet always...

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

# if we need monthly or weekly reports save a summary
if [ $WEEKDO != "nothing" ]; then
    CALAMARISOPTIONSOLD="$CALAMARISOPTIONS"
    CALAMARISOPTIONS="$CALAMARISOPTIONS -o daily.$DAYOFWEEK"
fi

# do the daily report
case $DAYDO in
    nothing) if [ $WEEKDO != "nothing" ]; then
	cat $SQUIDLOGDIR/access.log | \
	    nice -39 $CALAMARIS $CALAMARISOPTIONS > /dev/null
	fi
    ;;
    mail) (
	    echo "To: $DAYMAIL"
	    cat $SQUIDLOGDIR/access.log | \
		nice -39 $CALAMARIS $CALAMARISOPTIONS -mH "$DAYTITLE"
	) | /usr/lib/sendmail -t
    ;;
    web) cat $SQUIDLOGDIR/access.log | \
	    nice -39 $CALAMARIS $CALAMARISOPTIONS -wH "$DAYTITLE" > $DAYWEB
    ;;
    both) cat $SQUIDLOGDIR/access.log | \
	    nice -39 $CALAMARIS $CALAMARISOPTIONS -wH "$DAYTITLE" > $DAYWEB
	(
	    echo "To: $DAYMAIL"
	    cat $SQUIDLOGDIR/access.log | \
		nice -39 $CALAMARIS $CALAMARISOPTIONS -mH "$DAYTITLE"
	) | /usr/lib/sendmail -t
    ;;
    *) 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

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


if [ $DAYOFWEEK = "0" ]; then
    case $WEEKDO in
	nothing) 
	;;
	mail) (
		echo "To: $WEEKMAIL"
		nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES \
		    -zmH "$WEEKTITLE"
	    ) | /usr/lib/sendmail -t
	;;
	web) nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES \
		-zwH "$WEEKTITLE" > $WEEKWEB
	;;
	both) nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES \
		-zwH "$WEEKTITLE" > $WEEKWEB
	    (
		echo "To: $WEEKMAIL"
		nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES \
		    -zmH "$WEEKTITLE"
	    ) | /usr/lib/sendmail -t
	;;
	*) 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
#    if [ $MONTHDP != "nothing" ]; then
#	nice -39 $CALAMARIS $CALAMARISOPTIONS -i $WEEKFILES -z -o weekly.$WEEKWHAT > /dev/null
#    fi
fi

# do the monthly report and rotate the monthly logs: nothing to do :-)

exit 0
