#!/bin/bash

VERSION='0.2'

CONFFILE='/etc/tz-brasil.conf'
VARDIR='/var/lib/tz-brasil'
STAMPFILE='success-stamp'
RETRYFILE='failure-stamp'
ZIC='/var/lib/tz-brasil/info'

# this is the default configuration
VERBOSE='1'
SERVER='http://people.debian.org/~pzn/tz-brasil/'
FILE='tz-brasil-sp.zic'
TIME_ZONE='America/Sao_Paulo'
WGETOPTS=''
FETCH_INTERVAL='10000' # aprox 7 days
RETRY_INTERVAL='250'   # aprox 4 hours

# read the config file
if [ ! -r "$CONFFILE" ]; then
    echo "could not read configuration file: $CONFFILE" >&2
    exit 32
fi
. $CONFFILE

if [ "`cat /etc/timezone`" != "$TIME_ZONE" ]; then
    echo "Current timezone is `cat /etc/timezone`"  >&2
    echo "Please change it to $TIME_ZONE" >&2
    echo "Use the command 'tzconfig' to do it" >&2
    exit 33
fi

# sorry for my dumb command line argument parser :-)
case "$1" in
    --test)
	echo 'verbose mode set to 2'
	VERBOSE='2'
	;;
    --force)
	echo 'verbose mode set to 2'
	VERBOSE='2'
	echo 'removing old timestamp. forcing a new fetch'
	rm -fv $VARDIR/$STAMPFILE || exit 34
	rm -fv $VARDIR/$RETRYFILE || exit 34
	;;
    --version)
	echo "tz-brasil version $VERSION"
	echo '(c) 2002-2004 Pedro Zorzenon Neto <pzn@debian.org>'
	echo 'For licence details read copyright file. On Debian systems, it can be found at /usr/share/doc/tz-brasil/copyright.'
	exit 0
	;;
    --help)
	echo 'Usage: tz-brasil [ --help | --force | --test | --version ]'
	echo '  --help    : prints version and exit'
	echo '  --version : prints this screen and exit'
	echo '  --force   : set VERBOSE to 2 and force a timezone fetch'
	echo '  --test    : set VERBOSE to 2'
	exit 0
	;;
    '')
    # no cmdline arguments
	;;
    *)
	echo "Unknown command line argument: '$1'" >&2
	echo 'Try: tz-brasil --help' >&2
	exit 35
	;;
esac

case $VERBOSE in
    0)
        # supress STDOUT and STDERR
	exec >/dev/null 2>/dev/null
	;;
    1)
        # supress STDOUT
	exec >/dev/null
	;;
    *)
	;;
esac

find $VARDIR -name $STAMPFILE -mmin +$FETCH_INTERVAL -exec rm -f '{}' ';'
if [ -e "$VARDIR/$STAMPFILE" ]; then
    echo "there was a successfull update not older than $FETCH_INTERVAL minutes. will not try to update again."
    echo 'run with argument "--force" if you want to update now'
    exit 0
fi

find $VARDIR -name $RETRYFILE -mmin +$RETRY_INTERVAL -exec rm -f '{}' ';'
if [ -e "$VARDIR/$RETRYFILE" ]; then
    echo "there was a retry not older than $RETRY_INTERVAL minutes. will not try to update again."
    echo 'run with argument "--force" if you want to update now'
    exit 0
fi

echo "touching retry-timestamp"
touch $VARDIR/$RETRYFILE
if [ "$?" != "0" ]; then
    echo "Could not touch retry-timestamp" >&2
    exit 5
fi

echo "generating a new tempfile"
TMP=`tempfile`
echo "tempfile is $TMP"
echo "Command: wget $WGETOPTS $SERVER$FILE -O $TMP"
wget $WGETOPTS "$SERVER$FILE" -O "$TMP" 2>&1
if [ "$?" != "0" ]; then
    # failed to get file
    echo "Command: wget $WGETOPTS $SERVER$FILE -O $TMP" >&2
    echo "Failed to get file from server" >&2
    rm -f $TMP
    exit 2
fi

echo "Got the file, now lets see it..."

if [ ! -e $ZIC ]; then
    # will create an empty file the first time it runs
    touch $ZIC
    if [ "$?" != "0" ]; then
	echo "Could not create $ZIC" >&2
	rm -f $TMP
	exit 3
    fi
fi

diff $ZIC $TMP
if [ "$?" == "0" ]; then
    echo "The retrieved file is the same"
    rm -f $TMP
    rm -f $VARDIR/$RETRYFILE
    touch $VARDIR/$STAMPFILE
    if [ "$?" != "0" ]; then
	echo "Could not touch timestamp" >&2
	exit 5
    fi
    echo "Success"
    exit 0
fi

# show the diferences to the user
echo "The following lines were changed in the timezone information:" >&2
diff -uw $ZIC $TMP | egrep '^[+-][^+#-]' >&2
echo "" >&2

/usr/sbin/zic $TMP
if [ "$?" != "0" ]; then
    # failed to compile file
    echo "Failed to compile the file" >&2
    rm -fv $TMP
    exit 4
fi

cat $TMP > $ZIC
rm -fv $TMP
rm -f $VARDIR/$RETRYFILE
touch $VARDIR/$STAMPFILE
if [ "$?" != "0" ]; then
    echo "Could not touch success-timestamp" >&2
    exit 5
fi
echo "Success"
exit 0
