#! /bin/sh
# postinst script for tvtime
#
# 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'.

setuid () {
    db_get tvtime/setuid
    if [ -x /usr/bin/tvtime ] && [ "$RET" = "false" ] ; then
	if ! dpkg-statoverride --list /usr/bin/tvtime >/dev/null; then
	    chown root:root /usr/bin/tvtime
	    chmod u=rwx,go=rx /usr/bin/tvtime
	fi
    else
	if ! dpkg-statoverride --list /usr/bin/tvtime >/dev/null; then
	    chown root:root /usr/bin/tvtime
	    chmod u=rwxs,go=rx /usr/bin/tvtime
	fi
    fi
}

# Parse the option requested from tvtime-configure
get_option () {
    OPTION=`tvtime-configure --$1 2>/dev/null | awk -F: '{ print \$2 }'`
}

suck_tvtime_xml () {
    # If tvtime-configure was not there at Debconf, but
    # /etc/tvtime/tvtime.xml was, then we presume that the
    # administrator knew what he was doing.  We will therefore suck
    # this information into the Debconf database now that
    # tvtime-configure is installed.
    if [ -f /etc/tvtime/debconf.tvtime.xml ]; then
	get_option norm
	db_set tvtime/norm "$OPTION"

	get_option frequencies
	db_set tvtime/frequencies-ntsc "$OPTION"
	db_set tvtime/frequencies-jp "$OPTION"
	db_set tvtime/frequencies-pal "$OPTION"

	get_option device
	db_set tvtime/v4ldevice "$OPTION"

	get_option vbidevice
	db_set tvtime/vbidevice "$OPTION"

	get_option priority
	db_set tvtime/processpriority "$OPTION"
    fi
}

### MAIN POSTINST ###
case "$1" in
    configure)
	# Load Debconf library
	. /usr/share/debconf/confmodule

	# Handle the setuid bit.
	setuid

	# Load /etc/tvtime/tvtime.xml into Debconf, if necessary
	suck_tvtime_xml

	# Try to set the /var/run/tvtime directory to video group
	if [ -d /var/run/tvtime ] ; then
	    if ! dpkg-statoverride --list /var/run/tvtime >/dev/null; then
		chmod ug=rwx,o=rxt /var/run/tvtime
		chown root:video /var/run/tvtime
	    fi
	fi

	CONFIGFILE=/etc/tvtime/tvtime.xml

	db_get tvtime/norm
	NORM=$RET
	case "$NORM" in
	    NTSC|PAL-M|PAL-Nc)
		db_get tvtime/frequencies-ntsc
		case "$RET" in
		    Cable) FREQTABLE=us-cable ;;
		    Broadcast) FREQTABLE=us-broadcast ;;
		    *) FREQTABLE=us-cable100 ;;
		esac
		;;
	    NTSC-JP)
		db_get tvtime/frequencies-jp
		case "$RET" in
		    Cable) FREQTABLE=japan-cable ;;
		    *) FREQTABLE=japan-broadcast ;;
		esac
		;;
	    PAL|PAL-60|PAL-N|SECAM)
		db_get tvtime/frequencies-pal
		case "$RET" in
		    Europe) FREQTABLE=europe ;;
		    France) FREQTABLE=france ;;
		    Russia) FREQTABLE=russia ;;
		    Australia) FREQTABLE=australia ;;
		    "New Zealand") FREQTABLE=newzealand ;;
		    "China Broadcast") FREQTABLE=china-broadcast ;;
		    "Australia Optus cable") FREQTABLE=australia-optus ;;
		    *) FREQTABLE=custom ;;
		esac
		;;
	    *)
		FREQTABLE=custom
		;;
	esac

	db_get tvtime/v4ldevice
	V4LDEV=$RET
	db_get tvtime/vbidevice
	VBIDEV=$RET
	db_get tvtime/processpriority
	PRI=$RET

	db_stop

	# Create the configuration file if it doesn't exist
	if [ ! -f $CONFIGFILE ]; then
	    cp /usr/share/tvtime/default.tvtime.xml $CONFIGFILE
	fi

	# Configure tvtime.
	tvtime-configure --configfile="$CONFIGFILE" --norm="$NORM" \
	    --frequencies="$FREQTABLE" --device="$V4LDEV" \
	    --vbidevice="$VBIDEV" --priority="$PRI" 2>/dev/null

	;;
    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
