#!/bin/sh
set -e

. /usr/share/debconf/confmodule
if [ "$1" = "prepare" ]; then
    db_input high ubiquity/use_nonfree || true
    db_get ubiquity/use_nonfree
    if [ "$RET" = "true" ]; then
        env -u DEBIAN_HAS_FRONTEND -u DEBCONF_REDIR \
            DEBCONF_DB_REPLACE=configdb DEBCONF_DB_OVERRIDE="Pipe{infd:none outfd:none}" \
            ubuntu-drivers autoinstall --package-list /run/ubuntu-drivers.autoinstall || \
		logger -t ubiquity "ubuntu-drivers autoinstall failed with code: $?"
    fi
    db_input high ubiquity/secureboot_key || true
    db_get ubiquity/secureboot_key
    if [ -n "$RET" ]; then
        mkdir -p /var/lib/shim-signed/mok
        /usr/lib/ubiquity/shim-signed/update-secureboot-policy --new-key
        printf '%s\n%s\n' "$RET" "$RET" | mokutil --import /var/lib/shim-signed/mok/MOK.der >/dev/null || true
    fi
    # Always clear secureboot key.
    db_set ubiquity/secureboot_key ''
elif [ "$1" = "wireless" ]; then
    db_get ubiquity/online
    if [ "$RET" != true ]; then
        db_input high ubiquity/ssid || true
    fi
fi

