#!/bin/sh
# postinst script for vicar
#
# 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>
#        * <postinst> `abort-remove'
#        * <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


case "$1" in
    configure)

	# Kill any running instances of the daemon (only used on Linux based systems)
	killall -q vicar-daemon || true
	killall -q vicar-telepathy || true

        echo "Starting Vicar daemon ..."
        [[ -x /opt/vicar/vicar-daemon ]] && su - user -c "exec /opt/vicar/vicar-daemon& >/dev/null"

        echo "Starting Vicar-Telepathy ..."
        [[ -x /opt/vicar/vicar-telepathy ]] && su - user -c "exec /opt/vicar/vicar-telepathy& >/dev/null"

        echo "Removing Vicar launch script..."
        [[ -x /etc/event.d/launch-vicar ]] && rm -f /etc/event.d/launch-vicar

        echo "Deleting previous release configuration from GConf ..."
        gconftool-2 --unset /apps/Maemo/vicar/routing_enabled
        gconftool-2 --unset /apps/Maemo/vicar/calling_card_number
        gconftool-2 --unset /apps/Maemo/vicar/dtmf_delay
        gconftool-2 --unset /apps/Maemo/vicar/numbers_to_exclude
        gconftool-2 --unset /apps/Maemo/vicar/dtmf_format
        gconftool-2 --unset /apps/Maemo/vicar/dtmf_prefix
        gconftool-2 --unset /apps/Maemo/vicar/dtmf_suffix

        echo "Creating Vicar Telepathy account ..."
        [[ -x /opt/vicar/vicar-utils ]] && su - user -c "exec /opt/vicar/vicar-utils INSTALL"

        echo "Updating permissions on Vicar DB file ..."
        [[ -e /home/user/vicar.db.sqlite ]] && chown user:users /home/user/vicar.db.sqlite

        echo "Creating Vicar Profiles database ..."
        [[ -x /opt/vicar/vicar-utils ]] && su - user -c "exec /opt/vicar/vicar-utils CREATEDB"

	dbus-send --type=method_call --dest=org.freedesktop.Notifications /org/freedesktop/Notifications org.freedesktop.Notifications.SystemNoteDialog 	string:"Starting with this release, Multiple profiles can be created for routing calls via VICaR.

	 Please launch VICaR to setup routing profiles." uint32:0 string:"OK"

    ;;

    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


