#! /bin/sh
set -e

. /usr/share/debconf/confmodule
db_version 2.0
db_capb backup

# Initializes debconf default values from the ones found in
# configuration files
if [ -L /etc/localtime ] ; then
    TIMEZONE=$(readlink /etc/localtime | sed 's%^/usr/share/zoneinfo/%%')
fi
if ! [ -f "/usr/share/zoneinfo/$TIMEZONE" ] && [ -f /etc/timezone ] ; then
    TIMEZONE=$(head -n 1 /etc/timezone | sed -e "s/ .*//")
fi
if ! [ -f "/usr/share/zoneinfo/$TIMEZONE" ] ; then
    TIMEZONE=Etc/UTC
fi

AREA=$(echo $TIMEZONE | sed 's%/.*$%%')
ZONE=$(echo $TIMEZONE | sed 's%^.*/%%')

db_set tzdata/Areas "$AREA"

STATE=1
while [ "$STATE" -ge 0 ]; do
    case "$STATE" in
    0)
        exit 1;
        ;;
    1)
        db_input high tzdata/Areas || true
        ;;
    2)
        db_get tzdata/Areas || RET=Etc
        NEWAREA=$RET
        if [ "$NEWAREA" = "$AREA" ] ; then
            db_set tzdata/Zones/$NEWAREA "$ZONE"
        fi
        db_input high tzdata/Zones/$NEWAREA || true
        ;;
    *)
        break
        ;;
    esac
    if db_go; then
        STATE=$(($STATE + 1))
    else
        STATE=$(($STATE - 1))
    fi
done

#DEBHELPER#

exit 0
