#!/bin/sh
# This script can be called in the following ways:
#
# After the package was installed:
#       <postinst> configure <old-version>
#
#
# If prerm fails during upgrade or fails on failed upgrade:
#       <old-postinst> abort-upgrade <new-version>
#
# If prerm fails during deconfiguration of a package:
#       <postinst> abort-deconfigure in-favour <new-package> <version>
#                  removing <old-package> <version>
#
# If prerm fails during replacement due to conflict:
#       <postinst> abort-remove in-favour <new-package> <version>

set -e

if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if [ -x /usr/bin/deb-systemd-helper ]; then
		# This will only remove masks created by d-s-h on package removal.
		deb-systemd-helper unmask 'live-broadcomstartup.service' >/dev/null || true
	
		# was-enabled defaults to true, so new installations run enable.
		if deb-systemd-helper --quiet was-enabled 'live-broadcomstartup.service'; then
			# Enables the unit on first installation, creates new
			# symlinks on upgrades if the unit file has changed.
			deb-systemd-helper enable 'live-broadcomstartup.service' >/dev/null || true
		else
			# Update the statefile to add new symlinks (if any), which need to be
			# cleaned up on purge. Also remove old symlinks.
			deb-systemd-helper update-state 'live-broadcomstartup.service' >/dev/null || true
		fi
	
		# This will only remove masks created by d-s-h on package removal.
		deb-systemd-helper unmask 'live-clock12or24.service' >/dev/null || true
	
		# was-enabled defaults to true, so new installations run enable.
		if deb-systemd-helper --quiet was-enabled 'live-clock12or24.service'; then
			# Enables the unit on first installation, creates new
			# symlinks on upgrades if the unit file has changed.
			deb-systemd-helper enable 'live-clock12or24.service' >/dev/null || true
		else
			# Update the statefile to add new symlinks (if any), which need to be
			# cleaned up on purge. Also remove old symlinks.
			deb-systemd-helper update-state 'live-clock12or24.service' >/dev/null || true
		fi
	
		# This will only remove masks created by d-s-h on package removal.
		deb-systemd-helper unmask 'live-swap.service' >/dev/null || true
	
		# was-enabled defaults to true, so new installations run enable.
		if deb-systemd-helper --quiet was-enabled 'live-swap.service'; then
			# Enables the unit on first installation, creates new
			# symlinks on upgrades if the unit file has changed.
			deb-systemd-helper enable 'live-swap.service' >/dev/null || true
		else
			# Update the statefile to add new symlinks (if any), which need to be
			# cleaned up on purge. Also remove old symlinks.
			deb-systemd-helper update-state 'live-swap.service' >/dev/null || true
		fi
	
		# This will only remove masks created by d-s-h on package removal.
		deb-systemd-helper unmask 'live-static-shutdown.service' >/dev/null || true
	
		# was-enabled defaults to true, so new installations run enable.
		if deb-systemd-helper --quiet was-enabled 'live-static-shutdown.service'; then
			# Enables the unit on first installation, creates new
			# symlinks on upgrades if the unit file has changed.
			deb-systemd-helper enable 'live-static-shutdown.service' >/dev/null || true
		else
			# Update the statefile to add new symlinks (if any), which need to be
			# cleaned up on purge. Also remove old symlinks.
			deb-systemd-helper update-state 'live-static-shutdown.service' >/dev/null || true
		fi
	
		# This will only remove masks created by d-s-h on package removal.
		deb-systemd-helper unmask 'live-usb-save.service' >/dev/null || true
	
		# was-enabled defaults to true, so new installations run enable.
		if deb-systemd-helper --quiet was-enabled 'live-static-shutdown.service'; then
			# Enables the unit on first installation, creates new
			# symlinks on upgrades if the unit file has changed.
			deb-systemd-helper enable 'live-usb-save.service' >/dev/null || true
		else
			# Update the statefile to add new symlinks (if any), which need to be
			# cleaned up on purge. Also remove old symlinks.
			deb-systemd-helper update-state 'live-usb-save.service' >/dev/null || true
		fi
	
		# This will only remove masks created by d-s-h on package removal.
		deb-systemd-helper unmask 'persist-autosave.service' >/dev/null || true
	
		# was-enabled defaults to true, so new installations run enable.
		if deb-systemd-helper --quiet was-enabled 'persist-autosave.service'; then
			# Enables the unit on first installation, creates new
			# symlinks on upgrades if the unit file has changed.
			deb-systemd-helper enable 'persist-autosave.service' >/dev/null || true
		else
			# Update the statefile to add new symlinks (if any), which need to be
			# cleaned up on purge. Also remove old symlinks.
			deb-systemd-helper update-state 'persist-autosave.service' >/dev/null || true
		fi
	
		# This will only remove masks created by d-s-h on package removal.
		deb-systemd-helper unmask 'hwclock-mx.service' >/dev/null || true
	
		# was-enabled defaults to true, so new installations run enable.
		if deb-systemd-helper --quiet was-enabled 'hwclock-mx.service'; then
			# Enables the unit on first installation, creates new
			# symlinks on upgrades if the unit file has changed.
			deb-systemd-helper enable 'hwclock-mx.service' >/dev/null || true
		else
			# Update the statefile to add new symlinks (if any), which need to be
			# cleaned up on purge. Also remove old symlinks.
			deb-systemd-helper update-state 'hwclock-mx.service' >/dev/null || true
		fi
	
		# This will only remove masks created by d-s-h on package removal.
		deb-systemd-helper unmask 'live-setup.service' >/dev/null || true
	
		# was-enabled defaults to true, so new installations run enable.
		if deb-systemd-helper --quiet was-enabled 'live-setup.serve'; then
			# Enables the unit on first installation, creates new
			# symlinks on upgrades if the unit file has changed.
			deb-systemd-helper enable 'live-setup.service' >/dev/null || true
		else
			# Update the statefile to add new symlinks (if any), which need to be
			# cleaned up on purge. Also remove old symlinks.
			deb-systemd-helper update-state 'live-setup.service' >/dev/null || true
		fi
	fi

	if [ -x /usr/sbin/update-rc.d ]; then
		update-rc.d live-setup defaults
	fi
fi

if [ -h /etc/systemd/system/initrd.target ]; then
    if [ $(readlink /etc/systemd/system/initrd.target) = "/dev/null" ]; then
		rm /etc/systemd/system/initrd.target
	fi
fi

exit 0
