#!/bin/sh
# s10e-minimal off|on: while the screen is off, the S10e keeps only what calls, SMS, alarms and the power key need
# (the owner, 2026-11-09: "keep calls, sms, alarms and power key only, drop the rest").
#
# s10e-sleepd runs "off" as before_sleep (before the first sleep after the screen goes off; it sleeps when this ends)
# and "on" as after_sleep (when the screen comes on again). A restart while it is off needs nothing: the Wi-Fi modules
# and the sensor hub load at boot, and the nft table and /run are gone.
#
# Off:
# - Wi-Fi: brcmfmac and the PCIe RC unloaded, so the chip loses its power (wlan_reg_on) instead of idling in D3.
#   NetworkManager connects again by itself once the modules are back (~8 s).
# - Mobile data: the bearer stays up (on LTE it is the attach that calls and SMS ride on), but nothing goes out on it:
#   with Wi-Fi gone the default route moves there, so an nft table drops output to the modem's net devices.
# - The sensor hub (bcm4775: the sensors and GNSS) unbound, so its MCU stops.
# In gateway mode (gsm2sipd owns calls and SMS and bridges them to SIP; Calls' daemon is then masked for the user)
# calls arrive over Wi-Fi, so Wi-Fi and data stay.
# Bluetooth stays as quick settings left it (the owner, 2026-11-24: a car connects to the phone in a pocket, and music
# and calls on it go on with the screen off; s10e-sleepd stays awake while Bluetooth audio runs). Its controller sleeps
# by itself and wakes the phone on its host_wake line.
# NFC is unpowered already (neard powers it only to poll). The AOD and the double tap are off in the settings
# (de.yesman.neo aod / double-tap-wake), and aod_clock=false in /etc/s10e-sleepd.conf.
SHUB=/sys/bus/spi/drivers/bcm4775-shub
STATE=/run/s10e-minimal
BAT=/sys/class/power_supply/max170xx_battery
log() { echo "s10e-minimal: $*"; }
# the gauge at each end of a sleep: the journal's timestamps then give the drain while the screen was off
battery() {
	echo "battery $(cat $BAT/capacity 2>/dev/null)% $(cat $BAT/charge_counter 2>/dev/null || cat $BAT/charge_now 2>/dev/null) uAh," \
		"charger $(cat /sys/class/power_supply/max77705-charger/online 2>/dev/null)"
}
# sleepd starts "on" without waiting when the screen comes on, which can be while "off" still runs: one at a time
exec 9>>/run/s10e-minimal.lock
flock 9

off() {
	: > $STATE
	if [ "$(readlink /home/user/.config/systemd/user/calls-daemon.service)" = /dev/null ]; then
		log "gateway mode: Wi-Fi and data stay"
	else
		if [ -d /sys/module/pcie_exynos9820 ]; then
			rmmod brcmfmac_wcc brcmfmac 2>/dev/null
			rmmod pcie_exynos9820 && echo wifi >> $STATE
		fi
		nft -f - <<-EOF && echo data >> $STATE
		table inet s10e_minimal
		delete table inet s10e_minimal
		table inet s10e_minimal {
			chain out {
				type filter hook output priority 0; policy accept;
				oifname "rmnet*" drop
				oifname "wwan*" drop
			}
		}
		EOF
	fi
	if [ -e $SHUB/spi1.0 ]; then
		echo spi1.0 > $SHUB/unbind && echo shub >> $STATE
	fi
	log "off: $(tr '\n' ' ' < $STATE); $(battery)"
}

on() {
	[ -s $STATE ] || exit 0
	if grep -qx wifi $STATE; then
		modprobe pcie_exynos9820 || log "modprobe pcie_exynos9820 failed"
	fi
	if grep -qx data $STATE; then
		nft delete table inet s10e_minimal 2>/dev/null
	fi
	if grep -qx shub $STATE && [ ! -e $SHUB/spi1.0 ]; then
		echo spi1.0 > $SHUB/bind
	fi
	# (an "off" before 2026-11-24 powered Bluetooth off, and wrote bt)
	if grep -qx bt $STATE; then
		timeout 5 bluetoothctl power on >/dev/null || log "Bluetooth did not power on"
	fi
	log "on: $(tr '\n' ' ' < $STATE); $(battery)"
	: > $STATE
}

case "$1" in
off) off ;;
on) on ;;
*)
	echo "usage: s10e-minimal off | on" >&2
	exit 2
	;;
esac
exit 0
