#!/bin/ash
[ -z "$ROOT" ] && ROOT=/www
CONFIG=$ROOT/config
SCRIPT=$ROOT/script

source $CONFIG/ap.conf
source $CONFIG/ng.conf

source $SCRIPT/utils/wifi.sh

# Init schedule
dir=/var/run/schedule
rm -rf $dir
[ -z "$SCHEDULE" ] || echo $SCHEDULE | tr ";" "\n" | awk -F':' -v dir="$dir" '{
  system("mkdir -p "dir"/"$1);
  system("echo -n "$2" > "dir"/"$1"/from");
  system("echo -n "$3" > "dir"/"$1"/to");
}'

# Schedule
schedule=online
curDoW=`date +'%u'`
if [ -d $dir ] ; then
	if [ -d "$dir/$curDoW" ] ; then
		fromMin="`cat /var/run/schedule/$curDoW/from`"
		toMin="`cat /var/run/schedule/$curDoW/to`"
		curHour=`date +'%H' | sed 's/^0*//'`
		curMin=`date +'%M' | sed 's/^0*//'`
		nowInMin=$((curHour * 60 + curMin))
		if [ "$nowInMin" -lt "$fromMin" ] || [ "$nowInMin" -gt "$toMin" ] ; then
			schedule=offline
		fi
	else
		schedule=offline
	fi
fi

[ "$schedule" == offline ] && goOffline

echo $schedule