#!/bin/sh

if ! grep -qw "ts-silo=1" /proc/cmdline 2>&1; then
	# Supercaps are not present or turned off
	exit 1
fi

MODEL=$(strings /proc/device-tree/model | tr ' ' '\n' | grep TS-)

if [ "$MODEL" != "TS-7990" ]; then
	echo "Supercaps not supported on $MODEL"
	exit 1
fi

# This is extremely conservative, but allows running off
# of caps for ~10-15 seconds
RESET_PCT=90

# Pin 7 of the DIO header will go high if VIN is removed.
# We can poll the FPGA pin (GPIO 249), but the CPU GPIO supports interrupts.
# Change the FPGA crossbar so FPGA_IRQ_1 outputs the value on DIO_6
export FPGA_IRQ_1=DIO_6
tshwctl --set

# export FPGA_IRQ_1/GPIO_4
echo 4 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio4/direction
echo rising > /sys/class/gpio/gpio4/edge

# while POWER_FAIL is high, check supercap levels
while true ; do
	eval "$(gpioctl --getin 4)" # will set $gpio4

	# shellcheck disable=SC2154
	if [ "${gpio4}" -ne "1" ]; then
		# This will block until there is an IRQ on POWER_FAIL
		gpioctl --waitfor 4
		continue
	fi

	eval "$(tsmicroctl -i)" # sets $SUPERCAP_PCT
	echo "Supercaps at ${SUPERCAP_PCT}"

	if [ "${SUPERCAP_PCT}" -le ${RESET_PCT} ]; then
		wall "The script tssilomon has detected main power has been lost!  Shutting down safely to prevent filesystem damage."
		reboot
		exit
	fi

	sleep .5
done
