#!/bin/sh -e

. /usr/share/debconf/confmodule

RULES_PATH="/etc/ufw"
USER_PATH="/var/lib/ufw"
TEMPLATE_PATH="/usr/share/ufw"
case "$1" in
    configure)
        # these files are required, but don't want to change them if
        # the user modified them
        for f in before.rules before6.rules after.rules after6.rules
        do
            ucf --debconf-ok $TEMPLATE_PATH/$f $RULES_PATH/$f
        done

        if [ -e "/etc/ufw/ufw.rules" ]; then
            mv -f /etc/ufw/ufw.rules /etc/ufw/ufw.rules.dpkg-old
        fi

        for f in user.rules user6.rules
        do
            if [ ! -e "$USER_PATH/$f" ]; then
                # if no config, copy the template
                cp $TEMPLATE_PATH/$f $USER_PATH/$f
            fi
        done

        if [ ! -e "/etc/ufw/ufw.conf" ]; then
            cp $TEMPLATE_PATH/ufw.conf /etc/ufw
        fi

        if [ ! -z "$2" ] && dpkg --compare-versions "$2" lt "0.16.2.4" ; then
            # remove these symlinks for existing installations
            rm -f /etc/rc0.d/K39ufw
            rm -f /etc/rc6.d/K39ufw
        fi

        update-rc.d ufw start 39 S . stop 39 1 . > /dev/null
        ;;
    abort-upgrade|abort-remove|abort-deconfigure)
        ;;
    *)
        echo "postinst called with unknown argument '$1'" >&2
        exit 1
        ;;
esac

#DEBHELPER#
