#!/bin/sh
# postinst script for xxxxx-init-diversity inits

case "$1" in
    configure|triggered)
    
		# postinst script to create fifo in /etc/s6-rc/current/run-image/service/s6-linux-init-shutdownd
		if [ ! -e /etc/s6-rc/current/run-image/service/s6-linux-init-shutdownd/fifo ]; then
		echo "creating fifo in /etc/s6-rc/current/run-image/service/s6-linux-init-shutdownd/fifo"
			mkfifo /etc/s6-rc/current/run-image/service/s6-linux-init-shutdownd/fifo
		fi
		# postinst script to create fifo in /etc/s6-rc/current/run-image/service/s6-svscan-log
		if [ ! -e /etc/s6-rc/current/run-image/service/s6-svscan-log/fifo ]; then
		echo "creating fifo in /etc/s6-rc/current/run-image/service/s6-svscan-log/fifo"
			mkfifo /etc/s6-rc/current/run-image/service/s6-svscan-log/fifo
		fi
		# convert /sbin/init-s6-rc to a relative symlink
		if test -L /usr/sbin/init-s6-rc; then
		echo "Converting init-s6-rc symlink to relative"
			ln --force --symbolic --relative "$(readlink --canonicalize /usr/sbin/init-s6-rc)" /usr/sbin/init-s6-rc
		fi  
		# postinst script to replace poweroff
		if test -e /usr/lib/init-diversity/poweroff.sh; then
		echo "Reinstating multi-init poweroff function"
			ln -sf /usr/lib/init-diversity/poweroff.sh /usr/sbin/poweroff
		fi
		# postinst script to replace reboot
		if test -e /usr/lib/init-diversity/reboot.sh; then
		echo "Reinstating multi-init reboot function"
			ln -sf /usr/lib/init-diversity/reboot.sh /usr/sbin/reboot
		fi
		# postinst script to replace shutdown
		if test -e /usr/lib/init-diversity/shutdown.sh; then
		echo "Reinstating multi-init shutdown function"
			ln -sf /usr/lib/init-diversity/shutdown.sh /usr/sbin/shutdown
		fi
		# postinst script to replace rc - mainly for sysvinit & OpenRC
		if [ -e /usr/lib/init-diversity/rc.sh ] && [ -d /etc/init.d/ ]; then
		echo "Reinstating multi-init rc function"
			ln -sf /usr/lib/init-diversity/rc.sh /etc/init.d/rc
		fi
		# postinst script to replace rcS - mainly for sysvinit & OpenRC
		if [ -e /usr/lib/init-diversity/rcS.sh ] && [ -d /etc/init.d/ ]; then
		echo "Reinstating multi-init rcS function"
			ln -sf /usr/lib/init-diversity/rcS.sh /etc/init.d/rcS
		fi
		# automatically recompiling database 
		if test -e /usr/bin/s6-db-reload || test -e /usr/local/bin/s6-db-reload ; then
		echo "recompiling database"
			s6-db-reload
		else
		echo "dbus service files have been installed
			s6 database needs to be manually recompiled in order to activate"
		fi
		# ensuring user s6log exists 
		if id -u s6log >/dev/null 2>&1 ; then 
		echo "User 's6log' already exists"
		else useradd -r s6log -s /bin/nologin >/dev/null 2>&1
		fi

		# users need to be in the "input" group to control keyboard & mouse when logging in with startx without sudo
		{ getent passwd $(basename -a /home/*) | awk -F: '{print $1}' | while read USER; do usermod -a -G input $USER >/dev/null 2>&1; done } 2>/dev/null
		
		# new init will need to be added to the GRUB menu
		if mountpoint -q /live/aufs; then
		echo "Live/Frugal system detected - Not updating GRUB"
		exit 0		
		else
		echo "Updating GRUB to reveal the updated init entry"
		update-grub
		fi
    ;;

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

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

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.



exit 0
