#!/bin/sh
# postinst script for geoip-database-contrib

set -e

case "$1" in
    configure)

        . /usr/share/debconf/confmodule

        geoip-database-contrib_update || echo "Failed to download some files. Run geoip-database-contrib_update manually for fix it."

        db_get geoip-database-contrib/install-cronjob
        INSTALL_CRONJOB="$RET"

        tfile=$(mktemp -t geoip-database-contrib_config.XXXXXXXX) || exit 1
        if [ "$INSTALL_CRONJOB" = "true" ]
        then
            cat > $tfile <<EOF
#
# Regular cron job for the geoip-database-contrib package,
# used to update the GeoLite database.
#
0 4 10 * *      root    [ -x /usr/sbin/geoip-database-contrib_update ] && /usr/sbin/geoip-database-contrib_update > /dev/null
EOF
        else
            cat > $tfile <<EOF
#
# Regular cron job for the geoip-database-contrib package,
# used to update the GeoLite database.
#
#0 4 10 * *      root    [ -x /usr/sbin/geoip-database-contrib_update ] && /usr/sbin/geoip-database-contrib_update > /dev/null
EOF
        fi

        mkdir -p /etc/cron.d
        ucf --debconf-ok $tfile /etc/cron.d/geoip-database-contrib
        rm $tfile

		if [ -f "/usr/share/GeoIP/GeoLiteCity.dat" ]
		then
			update-alternatives --install /usr/share/GeoIP/GeoIPCity.dat GeoIPCity.dat /usr/share/GeoIP/GeoLiteCity.dat 50
		else
			echo "Could not update alternatives for GeoIPCity.dat: Missing GeoLiteCity.dat"
		fi
    ;;
esac

#DEBHELPER#

exit 0
