#!/bin/bash
#
# This script is used to poll the shutdown request triggered by
# the Moitessier driver.
#



# https://misc.flogisoft.com/bash/tip_colors_and_formatting
FORMAT_RED="\e[1;31m"
FORMAT_YELLOW="\e[1;93m"
FORMAT_GREEN="\e[1;32m"
FORMAT_DEFAULT="\e[0m"


shutdownFlag=/sys/module/moitessier/parameters/DO_SHUTDOWN

# DO_SHUTDOWN
#       0...do nothing
#       1...shutdown the system
#       2...reboot system

# DO_SHUTDOWN will be set to 1, if pressing the push button of the Moitessier HAT. You might also want
# to directly set the value via terminal (e.g. to set to 2 for testing purpose).
# target> echo 2 | sudo tee /sys/module/moitessier/parameters/DO_SHUTDOWN

info()
{
    echo
    echo "******************************************************************************************"
    echo $1
    echo "******************************************************************************************"
    echo
}

echo "moitessier: started $0" > /dev/kmsg    

while true
do
    if [ -e "${shutdownFlag}" ]
    then
        req=$(cat "${shutdownFlag}")
        if [ "$req" = "1" ]
        then
            info "Shutting down system"
            echo "moitessier: shutting down" > /dev/kmsg
            sudo shutdown now
        elif [ "$req" = "2" ]
        then
            info "Rebooting system"
            echo "moitessier: rebooting" > /dev/kmsg
            sudo reboot
        fi
    fi
    sleep 2
done

exit 0