#!/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" = "sysvinit"; then 
	set-init /etc/default/init
        update-grub || true
    elif test "$RET" = "systemd"; then
        if test -x /bin/systemd -o -e /run/init-select-nocheck; then
            set-init /etc/default/init /bin/systemd
            update-grub || true
        else
            db_input high init-select/no-systemd || true 
            db_go
            db_get init-select/no-systemd
	fi
    elif test "$RET" = "upstart"; then
        if test -x /bin/upstart -o -e /run/init-select-nocheck; then
            set-init /etc/default/init /bin/upstart
        else
            db_input high init-select/no-upstart || true 
            db_go
            db_get init-select/no-upstart
	fi
    elif test "$RET" = "openrc"; then
        if test -x /bin/openrc -o -e /run/init-select-nocheck; then
            set-init /etc/default/init /bin/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#
