#!/bin/sh

# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. "$(which sxmo_common.sh)"

# This hook goal is to return a non zero exit code if the device
# must be considered not idle (ongoing call, playing mpd, etc)

exit_not_idle() {
	printf %s "$1"
	exit 1
}


ongoing_call() {
	pgrep -f sxmo_modemcall.sh > /dev/null && exit_not_idle "call"
}

proximity_lock_on() {
	pgrep -f sxmo_proximitylock.sh > /dev/null && exit_not_idle "proxlock"
}

playing_mpc() {
	command -v mpc > /dev/null || return # no mpc installed

	(mpc status | grep -q '\[playing\]') && exit_not_idle "mpc"
}

waiting_rtcwake() {
	if grep -q crust "$LASTSTATE" && \
		grep -q rtc "$UNSUSPENDREASONFILE"; then
			exit_not_idle "rtcwake"
	fi
}

ongoing_call
proximity_lock_on
playing_mpc
waiting_rtcwake

exit 0
