#!/bin/sh

# This program is called by cron jobs and in webserver's pre-rotate
# script to update the statistics of AWStats.

[ -x /usr/lib/cgi-bin/awstats.pl ] || exit

USAGE="Usage: awstats-update [ -v | --verbose | -q | --quiet ]"
GETOPT=`getopt -o vq --long 'verbose,quiet' -n awstats-update -- "$@"`
if [ $? != 0 ] ; then
    echo >&2 "$USAGE" ; exit 1
fi
eval set -- "$GETOPT"
VERBOSE=false
while true ; do
    case "$1" in
	-v|--verbose) VERBOSE=true; shift ;;
	-q|--quiet) VERBOSE=false; shift ;;
	--) shift ; break ;;
	*) echo >&2 "Internal error!" ; exit 1 ;;
    esac
done

for config in /etc/awstats/awstats.*.conf; do
    config=${config#/etc/awstats/awstats.} # remove prefix
    config=${config%.conf}                 # remove suffix
    if [ "$config" = "model" ]; then
	continue
    fi
    # Run awstats.  Filter the output for error messages.
    if [ "$VERBOSE" = "true" ]; then
	echo
	echo "/usr/lib/cgi-bin/awstats.pl -config=$config -update"
	/usr/lib/cgi-bin/awstats.pl -config=$config -update
    else
	/usr/lib/cgi-bin/awstats.pl -config=$config -update | fgrep Error:
    fi
done
