#!/bin/sh
#postrm script for init-default-mx-systemd
#ensures an init system is still set as default
#switches to sysvinit if available

set -e

# Ensure a primary init exists otherwise make another existing init primary
if [ -L /sbin/init ]; then
	rm -f /sbin/init
fi

if [ ! -e /sbin/init ]; then
		if [ -x /lib/sysvinit/init ]; then
			echo "Found sysvinit - linking to /sbin/init"
			ln -sf /lib/sysvinit/init /sbin/init
		elif [ -x /lib/systemd/systemd ]; then
			echo "Found systemd - linking to /sbin/init"
			ln -sf /lib/systemd/systemd /sbin/init
		fi
fi

# Removed inits will need to be removed from the GRUB menu
if mountpoint -q /live/linux; then
	echo "Live/Frugal 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"
	update-grub
fi

#DEBHELPER#

exit 0
