#! /bin/sh
# postinst script for tz-brasil
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the `postinst' is called with `abort-upgrade',
#     `abort-remove' or `abort-deconfigure'.

case "$1" in
    configure)

        # remove executable-permission from conffile, previous versions
	# of tz-brasil installed the conffile 0755
	if [ -x /etc/tz-brasil.conf ]; then
	  chmod a-x /etc/tz-brasil.conf
	fi
	
	# Do package configuration before get a new zic information file (gleydson)
	if [ -f /var/lib/tz-brasil/info-install ];then
	    zic /var/lib/tz-brasil/info-install
	    rm -f /var/lib/tz-brasil/info-install
	    tz-brasil --refresh >/dev/null || true
	fi

	echo ""
	echo "Configuring package..."
	rm -f /var/lib/tz-brasil/success-stamp
	
        # Added to allow the package to be configured even if you are off-line
        # while installing the system from a CD
        # as sucess-stamp will not be created, once you get a ppp conection or
        # permanent connected, the new zic will be downloaded (gleydson)
	echo -n "Testing internet connection: "
	set +e
	fping -q www.debian.org
	R="$?"
	set -e
	if [ "$R" = "0" ]; then
	    echo "ok. updating tz-brasil database"
	    /usr/sbin/tz-brasil || true
	else
	    echo "internet is not available."
	    echo " * will update tz-brasil database later, when you are online"
	    echo " * to force a database update, run: tz-brasil --force"
	fi

	echo -n "Now it is: "
	date -u
	date
	echo ""
	
	echo "Running tz-brasil-restart..."
	/usr/sbin/tz-brasil-restart
	;;
    
    abort-upgrade|abort-remove|abort-deconfigure)
	
	;;
    
    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
	;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
