#!/bin/sh

set -e

#DEBHELPER#

case "$1" in
    configure)
        # Attempt to (re-)load modules and tccd, fail silently if not possible

        # Stop tccd service if it was running before
        echo "Check tccd running status"
        TCCD_RUNNING=false
        if deb-systemd-invoke is-active tccd; then
            TCCD_RUNNING=true
            echo "Stop tccd temporarily"
            deb-systemd-invoke stop tccd 2>&1 || true
        fi

        # Stop UPower as it does not handle well when kbd_backlight entries disapear
        echo "Stop UPower temporarily"
        systemctl stop upower 2>&1 || true

        # (Re-)load modules
        echo "(Re)load modules if possible"
        rmmod tuxedo_nb02_nvidia_power_ctrl > /dev/null 2>&1 || true
        rmmod tuxedo_io > /dev/null 2>&1 || true
        rmmod uniwill_wmi > /dev/null 2>&1 || true
        rmmod clevo_wmi > /dev/null 2>&1 || true
        rmmod clevo_acpi > /dev/null 2>&1 || true
        rmmod tuxedo_keyboard > /dev/null 2>&1 || true
        rmmod ite_8291 > /dev/null 2>&1 || true
        rmmod ite_8291_lb > /dev/null 2>&1 || true
        rmmod ite_8297 > /dev/null 2>&1 || true
        rmmod ite_829x > /dev/null 2>&1 || true
        rmmod tuxedo_nb05_fan_control > /dev/null 2>&1 || true
        rmmod tuxedo_nb05_kbd_backlight > /dev/null 2>&1 || true
        rmmod tuxedo_nb05_keyboard > /dev/null 2>&1 || true
        rmmod tuxedo_nb05_sensors > /dev/null 2>&1 || true
        rmmod tuxedo_nb05_power_profiles > /dev/null 2>&1 || true
        rmmod tuxedo_nb05_ec > /dev/null 2>&1 || true
        rmmod tuxedo_nb04_kbd_backlight > /dev/null 2>&1 || true
        rmmod tuxedo_nb04_power_profiles > /dev/null 2>&1 || true
        rmmod tuxedo_nb04_sensors > /dev/null 2>&1 || true
        rmmod tuxedo_nb04_keyboard > /dev/null 2>&1 || true
        rmmod tuxedo_nb04_wmi_ab > /dev/null 2>&1 || true
        rmmod tuxedo_nb04_wmi_bs > /dev/null 2>&1 || true
        rmmod tuxedo_compatibility_check > /dev/null 2>&1 || true
        modprobe tuxedo_compatibility_check > /dev/null 2>&1 || true
        modprobe tuxedo_keyboard > /dev/null 2>&1 || true
        modprobe clevo_acpi > /dev/null 2>&1 || true
        modprobe clevo_wmi > /dev/null 2>&1 || true
        modprobe uniwill_wmi > /dev/null 2>&1 || true
        modprobe tuxedo_io > /dev/null 2>&1 || true
        modprobe tuxedo_nb02_nvidia_power_ctrl > /dev/null 2>&1 || true
        modprobe ite_8291 > /dev/null 2>&1 || true
        modprobe ite_8291_lb > /dev/null 2>&1 || true
        modprobe ite_8297 > /dev/null 2>&1 || true
        modprobe ite_829x > /dev/null 2>&1 || true
        modprobe tuxedo_nb05_ec > /dev/null 2>&1 || true
        modprobe tuxedo_nb05_power_profiles > /dev/null 2>&1 || true
        modprobe tuxedo_nb05_sensors > /dev/null 2>&1 || true
        modprobe tuxedo_nb05_keyboard > /dev/null 2>&1 || true
        modprobe tuxedo_nb05_kbd_backlight > /dev/null 2>&1 || true
        modprobe tuxedo_nb05_fan_control > /dev/null 2>&1 || true
        modprobe tuxedo_nb04_keyboard > /dev/null 2>&1 || true
        modprobe tuxedo_nb04_wmi_ab > /dev/null 2>&1 || true
        modprobe tuxedo_nb04_wmi_bs > /dev/null 2>&1 || true
        modprobe tuxedo_nb04_sensors > /dev/null 2>&1 || true
        modprobe tuxedo_nb04_power_profiles > /dev/null 2>&1 || true
        modprobe tuxedo_nb04_kbd_backlight > /dev/null 2>&1 || true

        # Restart UPower
        echo "Start UPower again"
        systemctl restart upower 2>&1 || true

        # Restart tccd after reload if it was running before
        if $TCCD_RUNNING; then
            echo "Start tccd again"
            deb-systemd-invoke restart tccd 2>&1 || true
        fi
    ;;
esac
