#!/bin/sh
# postinst script for s6-linux-init.
#
# See: dh_installdeb(1).

set -e

# Summary of how this script can be called:
#        * <postinst> 'configure' <most-recently-configured-version>
#        * <old-postinst> 'abort-upgrade' <new version>
#        * <conflictor's-postinst> 'abort-remove' 'in-favour' <package>
#          <new-version>
#        * <postinst> 'abort-remove'
#        * <deconfigured's-postinst> 'abort-deconfigure' 'in-favour'
#          <failed-install-package> <version> 'removing'
#          <conflicting-package> <version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package.


case "$1" in
    configure)
	STAMP=$(date +%s) 
	if test -e /etc/s6-rc/s6-rc-init/current; then
		rm -r /etc/s6-rc/s6-rc-init/current
	fi
	/bin/s6-linux-init-maker -f "/etc/s6-rc/s6-rc-init/skel" -c /etc/s6-rc/s6-rc-init/current -G "/sbin/agetty tty1 38400 linux" -1 /etc/s6-rc/s6-rc-init/current
	if test -e /etc/s6-rc/s6-rc-init/current-release; then
		rm -r /etc/s6-rc/s6-rc-init/current-release
	fi
	mv /etc/s6-rc/s6-rc-init/current /etc/s6-rc/s6-rc-init/current-release
	ln -sf /etc/s6-rc/s6-rc-init/current-release /etc/s6-rc/s6-rc-init/current

    ;;

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

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

echo "Creating system user 's6log'"
useradd -r s6log -s /bin/nologin &>/dev/null

echo "copying s6-rc-init files to /lib/s6-rc folder"
	yes | cp -rf /etc/s6-rc/s6-rc-init/current/bin/init /lib/s6-rc/init
	yes | cp -rf /etc/s6-rc/s6-rc-init/current/bin/halt /lib/s6-rc/halt
	yes | cp -rf /etc/s6-rc/s6-rc-init/current/bin/reboot /lib/s6-rc/reboot
	yes | cp -rf /etc/s6-rc/s6-rc-init/current/bin/poweroff /lib/s6-rc/poweroff
	
echo "updating grub"
	update-grub

echo "s6-rc-init has been installed"

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

#DEBHELPER#
