#!/bin/sh
#===============================================================================
#
#  standby-actions
#
#  Copyright (C) 2023, 2024 by Digi International Inc.
#  All rights reserved.
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License version 2 as published by
#  the Free Software Foundation.
#
#
#  !Description: manage interfaces before suspending and after resuming from
#                suspend
#
#===============================================================================

RESUME_ACTIONS="/tmp/resume_actions"

wifi_actions_needed() {
	[ -d "/proc/device-tree/wireless" ] && [ ! -e "/sys/firmware/devicetree/base/soc@0/bus@42800000/mmc@428b0000/keep-power-in-suspend" ]
}

bt_actions_needed() {
	systemctl -q is-active bluetooth-init
}

if [ "${1}" = "pre" ]; then
	rm -f "${RESUME_ACTIONS}"

	# Stop NetworkManager before suspend
	systemctl stop NetworkManager

	if wifi_actions_needed; then
		for iface in wlan0 uap0 wfd0; do
			if grep -qs ${iface} /var/run/ifstate; then
				ifdown ${iface} && echo "ifup ${iface}" >> "${RESUME_ACTIONS}"
			fi
		done

		{
			echo "sleep 0.5"
			echo "/etc/udev/scripts/load_iw612.sh"
		} >> "${RESUME_ACTIONS}"
		modprobe -r moal
	fi

	if bt_actions_needed; then
		# bluetooth service relies on bluetooth-init service, so stop/start it unconditionally
		{
			echo "systemctl start bluetooth-init"
			echo "systemctl start bluetooth"
		} >> "${RESUME_ACTIONS}"
		systemctl stop bluetooth-init
		systemctl stop bluetooth
	fi
elif [ "${1}" = "post" ]; then
	if [ -f ${RESUME_ACTIONS} ]; then
		chmod +x "${RESUME_ACTIONS}"
		eval "${RESUME_ACTIONS}"
		rm -f "${RESUME_ACTIONS}"
	fi

	# Resume NetworkManager after suspend
	systemctl start NetworkManager
fi
