#!/bin/sh


PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME="nox-cheatcodes"

if [ -r /etc/default/distro ]; then
    . /etc/default/distro
fi

. /lib/lsb/init-functions

. /lib/init/fll

systemctl stop wpa_supplicant.service
systemctl mask wpa_supplicant.service
systemctl stop NetworkManager.service
systemctl enable --now iwd.service
systemctl start NetworkManager.service

if [ -r /etc/default/fll-locales ]; then
    . /etc/default/fll-locales
fi

FLL_LIVE_USER_HOME=$(getent passwd ${FLL_LIVE_USER} | cut -d \: -f 6)

###
# cheatcode handling
###
for param in $(cat /proc/cmdline); do
    case "${param}" in
        flldebug=*)
        if [ "${param#flldebug=}" = "${NAME}" ] || [ "${param#flldebug=}" = "all" ]; then
            fll_redirect
        fi
        ;;
    nointro)
        NOINTRO="nointro"
        ;;
    esac
done

no_intro() {
    if [ -z "${NOINTRO}"  ] ; then
        [ ! -f ${FLL_LIVE_USER_HOME}/.config/autostart/${FLL_DISTRO_NAME}.desktop ] && \
            sudo -u ${FLL_LIVE_USER} ln -sf "${FLL_LIVE_USER_HOME}/Desktop/${FLL_DISTRO_NAME}.desktop" \
            "${FLL_LIVE_USER_HOME}/.config/autostart/"
    elif [ -e "${FLL_LIVE_USER_HOME}/.config/autostart/${FLL_DISTRO_NAME}.desktop" ]; then
        # toram + persistency
        rm -f "${FLL_LIVE_USER_HOME}/.config/autostart/${FLL_DISTRO_NAME}.desktop"
    fi
}

do_start() {
    if [ -f ${FLL_LIVE_USER_HOME}/Desktop/${FLL_DISTRO_NAME}.desktop  ]; then
        no_intro
    fi
}

case "${1}" in
    start)
        log_daemon_msg "${NAME}"
        do_start
        log_end_msg "${?}"
        ;;
    stop)
        ;;
    restart|force-reload)
        echo "Error: argument '${1}' not supported" >&2
        exit 3
        ;;
    *)
        echo "Usage: ${NAME} {start|stop}" >&2
        exit 3
        ;;
esac
