#!/bin/sh
# postinst script for ha-sync
#
# see: dh_installdeb(1)
set -e
. /usr/share/debconf/confmodule

# 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)

	{
	    update-rc.d ha-hostname start 3 S . 
	    update-rc.d ha-config start 30 S . 

	    test -f /etc/ha-sync/id_rsa || {
		    ssh-keygen -N "" -C "ha-sync@`hostname`" -f /etc/ha-sync/id_rsa >&2
		    chmod u=r,go= /etc/ha-sync/id_rsa
	    }

	} >&2


	TMP=`tempfile`; trap "rm -f $TMP" EXIT

	## Config file 
	{
	    echo "# see /usr/share/doc/ha-sync/examples/ha-sync.conf.ex for"
	    echo "# example"
	    echo ""

	    db_get ha-sync/dir
	    echo DIR="'$RET'"

	    db_get ha-sync/nodes
	    echo "$RET" | while read a b; do 
		    echo NODE_A="'$a'"
		    echo NODE_B="'$b'"
	    done 

	    db_get ha-sync/fs
	    echo FILESYSTEMS="'${RET//,/}'"

	    db_get ha-sync/exclude
	    echo EXCLUDE="'$RET'"

	} >> $TMP
	ucf $TMP /etc/ha-sync/ha-sync.conf

	## Exclude list
	ucf /usr/share/doc/ha-sync/examples/exclude.ex /etc/ha-sync/exclude

	## noch Verzeichnisse
	source /etc/ha-sync/ha-sync.conf
	install -d "$DIR";
	for node in $NODE_A $NODE_B; do
	    install -d $DIR/$node.etc
	done

    ;;

    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


# vim:sts=4 sw=4 aw ai sm:
