#!/bin/sh
# configversion: 1

# 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() {
	sxmo_log "$*"
	printf %s "$1"
	exit 1
}


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

proximity_lock_on() {
	sxmo_daemons.sh running proximity_lock -q && exit_not_idle "proxlock"
}

camera_open() {
	pgrep -f megapixels > /dev/null && exit_not_idle "camera"
}

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

auto_screenoff() {
	[ -e "$XDG_CACHE_HOME/sxmo/sxmo.noidle" ] && exit_not_idle "auto_screenoff_off"
}

ongoing_call
proximity_lock_on
waiting_rtcwake
camera_open
auto_screenoff

exit 0
