#!/bin/sh

set -e

. /usr/share/debconf/confmodule

# clean up old conf file
rm -f /etc/grub.d/30_init-select || true

if test "$1" = "configure"; then
    db_get init-select/choose
    if test "$RET" = "systemd"; then 
	/usr/lib/init-select/set-init /etc/default/init
        update-grub || true
    elif test "$RET" = "sysvinit"; then
        if test -x /lib/sysvinit/init -o -e /run/init-select-nocheck; then
            /usr/lib/init-select/set-init /etc/default/init /lib/sysvinit/init
            update-grub || true
	fi
    elif test "$RET" = "openrc"; then
        if test -x /sbin/openrc -o -e /run/init-select-nocheck; then
            /usr/lib/init-select/set-init /etc/default/init /sbin/openrc
        else
            db_input high init-select/no-openrc || true 
            db_go
            db_get init-select/no-openrc
	fi
    else
        echo "error: unknown init somehow selected"
        exit 1
    fi
fi

#DEBHELPER#
