#!/bin/sh
set -e

. /usr/share/debconf/confmodule
if [ "$1" = "prepare" ]; then
    FREEONLY="--free-only"
    NOOEM=""
    db_input high ubiquity/use_nonfree || true
    db_get ubiquity/use_nonfree
    if [ "$RET" = "true" ]; then
	    FREEONLY=""
    fi
    db_input high ubiquity/install_oem || true
    db_get ubiquity/install_oem
    if [ "$RET" = "false" ]; then
	    NOOEM="--no-oem"
    fi

    rm -f /run/ubuntu-drivers.autoinstall
    ubuntu-drivers install $FREEONLY $NOOEM --package-list /run/ubuntu-drivers.autoinstall || \
        logger -t ubiquity "ubuntu-drivers install failed with code: $?"

    db_input high ubiquity/secureboot_key || true
    db_get ubiquity/secureboot_key
    if [ -n "$RET" ]; then
        mkdir -p /var/lib/shim-signed/mok
        /usr/sbin/update-secureboot-policy --new-key
        if [ -e /var/lib/shim-signed/mok/MOK.der ]; then
            printf '%s\n%s\n' "$RET" "$RET" | mokutil --import /var/lib/shim-signed/mok/MOK.der >/dev/null || true
            mokutil --timeout -1 >/dev/null || true
        fi
    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

