Emergency shutdown daemon for MNT Reform

I wrote a very simple daemon to make the mnt reform shutdown at 6%.

#!/usr/bin/env sh

main() {
    iPrevBattLvl=0 iCurrBattLvl=0 bPoweroffTriggered=0

    while true; do

        # Runs while on battery power.
        if [ "$(cat /sys/class/power_supply/BAT0/status)" = "Discharging" ];then

            iPrevBattLvl=$(cat /sys/class/power_supply/BAT0/capacity)
            sleep 120 # How much to wait to make sure the battery is indeed <=6
            iCurrBattLvl=$(cat /sys/class/power_supply/BAT0/capacity)

            # Check that the battery stayed at the same <=6 value for at least 120 seconds.
            # This is to avoid a random shutdown caused by a wrong battery read.
            if [ $iCurrBattLvl -le 6 ] && [ $iCurrBattLvl = $iPrevBattLvl ] && [ $bPoweroffTriggered = 0 ];then
                bPoweroffTriggered=1
                gnome-session-quit --power-off --force # TRIGGER POWEROFF
            fi
        # Runs when plugged in.
        else
            # If the emergency shutdown was triggered but the laptop
            # is now plugged, run the abort command to try to stop the poweroff.
            if [ $bPoweroffTriggered = 1 ];then
                bPoweroffTriggered=0
                systemctl poweroff --when=cancel; shutdown -c # UNTRIGGER POWEROFF
            fi

            sleep 20 # How ofthen to check if the laptop is plugged.
        fi

    done
}
main

I haven’t tested it yet but I really need it because when my MNT runs out of battery I need to open it up and disconnect + reconnect the battery cables for the keyboard to work again. It has probably already been fixed in a new firmware update. I really need to update it.

What you are experiencing is probably this issue?

https://source.mnt.re/bugs/bugs/-/work_items/60