#!/bin/sh
set -uo pipefail
set +e
exec 0<&-

#logfile="/mnt/SDCARD/.userdata/tg5040/logs/suspend_$(date +%Y%m%d_%H%M%S).log"
#exec >> "$logfile" 2>&1

wifid_running=
bluetoothd_running=

sleep_retval=

asound_state_dir=/tmp/asound-suspend

before() {
	>&2 echo "Preparing for suspend..."

	# Run pre-sleep hooks synchronously before services are stopped.
	# Subshell ensures a crashing hook cannot block suspend.
	( "$SYSTEM_PATH/bin/run_hooks.sh" pre-sleep.d --sync-only ) >/dev/null 2>&1 || true

	>&2 echo "Saving mixer state..."
	mkdir -p "$asound_state_dir"
	alsactl --file "$asound_state_dir/asound.state.pre" store || true

	if pgrep bluetoothd; then
		bluetoothd_running=1
		>&2 echo "Stopping bluetoothd..."
		$SYSTEM_PATH/etc/bluetooth/bt_init.sh stop || true
	fi

	if pgrep wpa_supplicant; then
		wifid_running=1
		>&2 echo "Stopping wpa_supplicant..."
		$SYSTEM_PATH/etc/wifi/wifi_init.sh stop || true
	fi
}

after() {
	>&2 echo "Resumed from suspend."

	# Run post-resume hooks in background immediately after wake.
	( "$SYSTEM_PATH/bin/run_hooks.sh" post-resume.d ) >/dev/null 2>&1 &

	if [ -n "$wifid_running" ]; then
		>&2 echo "Starting wpa_supplicant..."
		$SYSTEM_PATH/etc/wifi/wifi_init.sh start || true
	fi

	if [ -n "$bluetoothd_running" ]; then
		>&2 echo "Starting bluetoothd..."
		/etc/bluetooth/bt_init.sh start || true
	fi

	# >&2 echo "Restoring mixer state..."
	# alsactl --file "$asound_state_dir/asound.state.post" store || true
	# alsactl --file "$asound_state_dir/asound.state.pre" restore || true
}

# NextUI exports NEXTUI_POWEROFF_SECS when the power off timeout is on. We arm
# the RTC alarm for that long. If the alarm ends the suspend, we exit with 3
# and NextUI powers off. Deadlines use the RTC's clock, the one the alarm runs
# on, so the system clock doesn't matter.
rtc=/sys/class/rtc/rtc0
poweroff_at=
poweroff_due=

arm_poweroff_alarm() {
	[ "${NEXTUI_POWEROFF_SECS:-0}" -gt 0 ] 2>/dev/null || return 0
	rtc_now=$(cat "$rtc/since_epoch" 2>/dev/null)
	if [ -n "$rtc_now" ]; then
		echo enabled > "$rtc/device/power/wakeup" 2>/dev/null
		# The kernel rejects a new alarm while one is enabled.
		echo 0 > "$rtc/wakealarm" 2>/dev/null
		echo "$((rtc_now + NEXTUI_POWEROFF_SECS))" > "$rtc/wakealarm" 2>/dev/null
		# Empty if the write failed. The driver may also round the time.
		poweroff_at=$(cat "$rtc/wakealarm" 2>/dev/null)
	fi
	if [ -z "$poweroff_at" ]; then
		>&2 echo "Could not set the RTC alarm, the device will not power off by itself."
		return 0
	fi
	>&2 echo "Power off alarm set for ${NEXTUI_POWEROFF_SECS}s from now."
}

# Sets poweroff_due once the RTC is within $1 seconds of the alarm time.
check_poweroff_due() {
	[ -n "$poweroff_at" ] || return 1
	rtc_now=$(cat "$rtc/since_epoch" 2>/dev/null)
	[ -n "$rtc_now" ] && [ "$((rtc_now + $1))" -ge "$poweroff_at" ] && poweroff_due=1
	[ -n "$poweroff_due" ]
}

before
arm_poweroff_alarm

>&2 echo "Suspending..."
sleep_max_tries=5
for i in $(seq 1 $sleep_max_tries); do
	>&2 echo "Deep sleep attempt $i of $sleep_max_tries"

	# If the alarm fired before the suspend started, nothing would wake the
	# device, so the last 2 seconds count as due.
	check_poweroff_due 2 && break

	echo mem >/sys/power/state
	sleep_retval=$?

	# Read the RTC before anything else takes time.
	check_poweroff_due 0 && break

	>&2 echo "Deep sleep: kernel returned $sleep_retval"
	if [ $sleep_retval -eq 0 ]; then
	    break
	fi

	>&2 echo "Deep sleep failed: retrying in 3 seconds"
	sleep 3
done

[ -n "$poweroff_at" ] && echo 0 > "$rtc/wakealarm" 2>/dev/null

if [ -n "$poweroff_due" ]; then
	# NextUI is about to power off, so skip the radios and the resume hooks.
	>&2 echo "Power off timeout expired, skipping resume tasks."
	exit 3
fi

# Resume services in background to reduce UI latency
after &

exit $sleep_retval
