#!/bin/sh
# postinst script for init-default-mx-systemd

case "$1" in
    configure|triggered)
    	# Force /sbin/init to point at systemd if missing
		if [ -e /usr/sbin/init ]; then
			rm -rf /sbin/init
		fi
		
		echo "Reinstating systemd as default init"
		ln -sf /usr/lib/systemd/systemd /usr/sbin/init
		ln --force --symbolic --relative "$(readlink --canonicalize /usr/sbin/init)" /usr/sbin/init
		# Removed inits will need to be removed from the GRUB menu
		if mountpoint -q /live/linux; then
			echo "Live system detected - Not updating GRUB"
			exit 0
		elif [ "$(stat -c %d/%i /)" != "$(stat -Lc %d/%i /proc/1/root 2>/dev/null)" ]; then
			echo "A chroot environment has been detected - Not updating GRUB"
			exit 0
		else
			echo "Updating GRUB to reveal the updated init entry"
			if [ -x /sbin/update-grub ]; then
				update-grub
			fi
		fi
     ;;

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

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

#DEBHELPER#

exit 0
