@ECHO OFF
CALL C:\LiveDigital\config.bat

FOR /f "tokens=1-2 delims=/:" %%a IN ("%TIME%") DO (
	SET _thishour=%%a
	SET _thisminute=%%b
)

SETLOCAL EnableDelayedExpansion

TASKKILL /IM firefox.exe /F

PING 127.0.0.1 -n 2 -w 500

SET var_schedulefile0=%var_livedir%\live\screen%var_screen0%\schedules.txt

DEL C:\LiveDigital\tasks.txt
	
Schtasks /query /FO TABLE /NH > C:\LiveDigital\tasks.txt

FART.exe "C:\LiveDigital\tasks.txt" "Folder: " " "
FART.exe -C "C:\LiveDigital\tasks.txt" \\ " "
	
FOR /F "tokens=1,2,4,5 delims=_ " %%A IN (C:\LiveDigital\tasks.txt) DO (
	IF "%%A"=="Schedule" (
		Schtasks /Delete /tn "%%A_%%B" /f
		ECHO Deleting Task %%A_%%B
	)
)

SET i = 1

ECHO Player Time Hour: %_thishour% Minute: %_thisminute%
	
SET _currentschedule0=unscheduled

FOR /F "tokens=1,2,3 delims=: " %%A IN (!var_schedulefile0!) DO (
	SET /A i += 1
	IF "%var_is64%"=="1" (
		Schtasks /create /tn "Schedule_S1-!i!%%A" /sc daily /st %%B:%%C /tr "C:\LiveDigital\hstart64.exe /NOCONSOLE /ELEVATE ""C:\LiveDigital\startplayer.bat""" /rl HIGHEST
	) ELSE (
		Schtasks /create /tn "Schedule_S1-!i!%%A" /sc daily /st %%B:%%C /tr "C:\LiveDigital\hstart.exe /NOCONSOLE /ELEVATE ""C:\LiveDigital\startplayer.bat""" /rl HIGHEST
	)		
	
	SET /A _tasktime=%%B*60+%%C
	SET /A tts="!_tasktime! + 0"
		SET /A _nowtime=%_thishour%*60+%_thisminute%
	SET /A nts="!_nowtime! + 0"		
		SET tasktime!i!=!_tasktime!
	@ECHO TT: !tts!
	@ECHO NT: !nts!

	ECHO Schedule Screen 0 Start Hour: %%B Minute: %%C tt: !tts!
	ECHO Checking !tts! LEQ !nts!
	IF !tts! LEQ !nts! (
		@ECHO Trigger this Schedule
		SET _currentschedule0=%%A
		@ECHO Setting Schedule0 to %%A
	) ELSE (
		@ECHO Scheduled for Later.
	)		
)

ECHO Resulting Active Schedule0 is !_currentschedule0!
IF "%var_is64%"=="1" (
	wget64.exe --timeout=5 --tries=2 --delete-after "%var_pingaddress%%var_pingscript%?clientid=%var_client%&serverip=%var_supportip%&type=schedule&screen=%var_screen0%&value=!_currentschedule0!"
) ELSE (
	wget.exe --timeout=5 --tries=2 --delete-after "%var_pingaddress%%var_pingscript%?clientid=%var_client%&serverip=%var_supportip%&type=schedule&screen=%var_screen0%&value=!_currentschedule0!"
)

TIMEOUT 5
	
CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox.exe" -no-remote -P instance0 "file:///C:/LiveDigital/live/screen%var_screen0%/!_currentschedule0!/playlist-1.html"
CMDOW /RUN /HID "C:\LiveDigital\adjust.bat"
START /B C:\LiveDigital\adjust.bat

PING 127.0.0.1 -n 4 -w 500
CMDOW /RUN /HID "C:\LiveDigital\adjust.bat"
START /B C:\LiveDigital\adjust.bat

PING 127.0.0.1 -n 4 -w 500
CMDOW /RUN /HID "C:\LiveDigital\adjust.bat"
START /B C:\LiveDigital\adjust.bat