#! /bin/sh
# postinst script for tinc
#
# $Id: postinst,v 1.6 2000/05/21 23:01:28 zarq Exp $
#
# see: dh_installdeb(1)

TCONF="/etc/tinc"
NETSFILE="$TCONF/nets.boot"
RET="false"

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 /usr/doc/packaging-manual/
#
# 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'.

if [ -x "/etc/init.d/tinc" ]; then
    if [ "$1" = "configure" ] ; then
	if dpkg --compare-versions "$2" lt "1.0pre5-4" ; then
	    RET="true"
	else
	    if [ -e /usr/share/debconf/confmodule ] ; then
		. /usr/share/debconf/confmodule
		db_get tinc/restart_on_upgrade
		db_stop
	    else
		RET="true"
	    fi
	fi
    fi
fi

case "$1" in
    configure)
	if [ ! -e /dev/.devfsd ] ; then if [ ! -e /dev/.devfs ] ; then
		if [ ! -e /dev/net/tun ] ; then if [ ! -e /dev/tun ] ; then
			echo "Creating tun device..."
			cd /dev && ./MAKEDEV net/tun 2>/dev/null || ./MAKEDEV tun 2>/dev/null || echo "Failed to create tun device."
		fi; fi
	fi; fi

	if [ ! -e $NETSFILE ] ; then
	    echo "## This file contains all names of the networks to be started on system startup." > $NETSFILE
	fi

  	if [ "$RET" = "true" ] ; then
	    invoke-rc.d tinc restart
	else
	    echo "I have not stopped or restarted the tinc daemon.  You should do this"
	    echo "yourself whenever you're ready; type \`\`invoke-rc.d tinc restart''."
	fi
  ;;

    abort-upgrade|abort-remove|abort-deconfigure)

    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 0
    ;;
esac

#DEBHELPER#

exit 0
