#!/bin/sh
set -e

MYNAME="${0##*/}"

report() { echo "${MYNAME}: $*" ; }
report_warn() { report "Warning: $*" >&2 ; }
report_err() { report "Error: $*" >&2 ; }

process_state_file()
{
    rm -f /run/network/ifstatenew
}

process_udev()
{
    if [ -e /etc/udev/rules.d/80-networking.rules ]; then
	udevlink=$(readlink /etc/udev/rules.d/80-networking.rules 2>/dev/null || true)
	[ -n "$udevlink" -a "$udevlink" = "/dev/null" ] && rm -f /etc/udev/rules.d/80-networking.rules
    fi

    if  [ -e /etc/udev/rules.d/60-bridge-network-interface.rules ]; then
	udevlink=$(readlink /etc/udev/rules.d/60-bridge-network-interface.rules 2>/dev/null || true)
	[ -n "$udevlink" -a "$udevlink" = "/dev/null" ] && rm -f /etc/udev/rules.d/60-bridge-network-interface.rules
    fi
}

proxmox_compatibility()
{
    perl -e '
        my $haspve_common; eval { require PVE::INotify; $haspve_common = 1; };
        exit if !$haspve_common;
        my $config = PVE::INotify::read_file('interfaces', 1);
        my $configdata = $config->{data};

        PVE::INotify::write_file('interfaces', $configdata);

        $config = PVE::INotify::read_file('interfaces', 1);

        if (defined($config->{changes})) {
            #print $config->{changes};
            print "\nnetwork config changes have been detected for ifupdown2 to ifupdown1 switch compatibility.\nSaved in /etc/network/interfaces.new for next reboot\n\n";
        }
    ' || true
}

case "$1" in
    purge|remove|abort-install|disappear)
        process_state_file
        process_udev
        proxmox_compatibility
    ;;

    upgrade|failed-upgrade|abort-upgrade|disappear)
    ;;

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


# Automatically added by dh_installsystemd/13.11.4
if [ "$1" = remove ] && [ -d /run/systemd/system ] ; then
	systemctl --system daemon-reload >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.11.4
if [ "$1" = "purge" ]; then
	if [ -x "/usr/bin/deb-systemd-helper" ]; then
		deb-systemd-helper purge 'networking.service' >/dev/null || true
	fi
fi
# End automatically added section


exit 0
