##############################################################################
# Logs a message to a log file (or to console if argument is /dev/stdout)

logger () {
	MSG=$1
	
	# do nothing if logging is not enabled
	if [ "x1" != "x$LOGGING" ]; then
		return
	fi

	# if no logfile is specified, set a default
	if [ -z $LOGFILE ]; then
		$LOGFILE=stdout
	fi

	echo `date`: $MSG >> $LOGFILE
}


##############################################################################
# Retrieves the current time in seconds

currentTime () {
	date +%s
}


##############################################################################
# sets an RTC alarm
# arguments: $1 - time in seconds from now

wait_for () {
    ENDWAIT=$(( $(currentTime) + $1 ))
    REMAININGWAITTIME=$(( $ENDWAIT - $(currentTime) ))
    logger "Starting to wait for timeout to expire: $1"

    # wait for timeout to expire
    while [ $REMAININGWAITTIME -gt 0 ]; do
        EVENT=$(lipc-wait-event -s $1 com.lab126.powerd readyToSuspend,wakeupFromSuspend,resuming)
        REMAININGWAITTIME=$(( $ENDWAIT - $(currentTime) ))
        logger "Received event: $EVENT"

        case "$EVENT" in
            readyToSuspend*)
                set_rtc_wakeup $REMAININGWAITTIME
            ;;
            wakeupFromSuspend*|resuming*)
                logger "Finishing the wait"
                break
            ;;
            *)
                logger "Ignored event: $EVENT"
            ;;
        esac
    done

    logger "Wait finished"
}
