:: Fixed Version

REM CMDOW @ /HID
@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
IF %var_noscreens% GTR 1 (
	TASKKILL /IM firefox2.exe /F
)
IF %var_noscreens% GTR 2 (
	TASKKILL /IM firefox3.exe /F
)
IF %var_noscreens% GTR 3 (
	TASKKILL /IM firefox4.exe /F
)
IF %var_noscreens% GTR 4 (
	TASKKILL /IM firefox5.exe /F
)
IF %var_noscreens% GTR 5 (
	TASKKILL /IM firefox6.exe /F
)

PING 127.0.0.1 -n 1 -w 500

IF %var_noscreens% GTR 1 (
	DEL "C:\Program Files\Mozilla Firefox\firefox2.exe"
	PING 127.0.0.1 -n 1 -w 500

	COPY "C:\Program Files\Mozilla Firefox\firefox.exe" "C:\Program Files\Mozilla Firefox\firefox2.exe"
)

IF %var_noscreens% GTR 2 (
	DEL "C:\Program Files\Mozilla Firefox\firefox3.exe"
	PING 127.0.0.1 -n 1 -w 500

	COPY "C:\Program Files\Mozilla Firefox\firefox.exe" "C:\Program Files\Mozilla Firefox\firefox3.exe"
)

IF %var_noscreens% GTR 3 (
	DEL "C:\Program Files\Mozilla Firefox\firefox4.exe"
	PING 127.0.0.1 -n 1 -w 500

	COPY "C:\Program Files\Mozilla Firefox\firefox.exe" "C:\Program Files\Mozilla Firefox\firefox4.exe"
)

IF %var_noscreens% GTR 4 (
	DEL "C:\Program Files\Mozilla Firefox\firefox5.exe"
	PING 127.0.0.1 -n 1 -w 500

	COPY "C:\Program Files\Mozilla Firefox\firefox.exe" "C:\Program Files\Mozilla Firefox\firefox5.exe"
)

IF %var_noscreens% GTR 5 (
	DEL "C:\Program Files\Mozilla Firefox\firefox6.exe"
	PING 127.0.0.1 -n 1 -w 500

	COPY "C:\Program Files\Mozilla Firefox\firefox.exe" "C:\Program Files\Mozilla Firefox\firefox6.exe"
)

PING 127.0.0.1 -n 2 -w 500

IF %var_enablescheduling%==1 (
	SET var_schedulefile0=%var_livedir%\live\screen%var_screen0%\schedules.txt

	IF %var_noscreens% GTR 1 (
		SET var_schedulefile1=%var_livedir%\live\screen%var_screen1%\schedules.txt
	)
	IF %var_noscreens% GTR 2 (
		SET var_schedulefile2=%var_livedir%\live\screen%var_screen2%\schedules.txt
	)
	IF %var_noscreens% GTR 3 (
		SET var_schedulefile3=%var_livedir%\live\screen%var_screen3%\schedules.txt
	)
	IF %var_noscreens% GTR 4 (
		SET var_schedulefile4=%var_livedir%\live\screen%var_screen4%\schedules.txt
	)
	IF %var_noscreens% GTR 5 (
		SET var_schedulefile5=%var_livedir%\live\screen%var_screen5%\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.
		)		
	)

	
	SET i = 1
	IF %var_noscreens% GTR 1 (
		ECHO Player Time Hour: %_thishour% Minute: %_thisminute%
	
		SET _currentschedule1=unscheduled
	
		FOR /F "tokens=1,2,3 delims=: " %%A IN (!var_schedulefile1!) DO (
			SET /A i += 1
			IF "%var_is64%"=="1" (
				Schtasks /create /tn "Schedule_S2-!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_S2-!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 1 Start Hour: %%B Minute: %%C tt: !tts!
			ECHO Checking !tts! LEQ !nts!
			IF !tts! LEQ !nts! (
				@ECHO Trigger this Schedule
				SET _currentschedule1=%%A
				@ECHO Setting Schedule1 to %%A
			) ELSE (
				@ECHO Scheduled for Later.
			)		
		)
	)


	SET i = 1
	IF %var_noscreens% GTR 2 (
		ECHO Player Time Hour: %_thishour% Minute: %_thisminute%
	
		SET _currentschedule2=unscheduled
	
		FOR /F "tokens=1,2,3 delims=: " %%A IN (!var_schedulefile2!) DO (
			SET /A i += 1
			IF "%var_is64%"=="1" (
				Schtasks /create /tn "Schedule_S3-!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_S3-!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 2 Start Hour: %%B Minute: %%C tt: !tts!
			ECHO Checking !tts! LEQ !nts!
			IF !tts! LEQ !nts! (
				@ECHO Trigger this Schedule
				SET _currentschedule2=%%A
				@ECHO Setting Schedule2 to %%A
			) ELSE (
				@ECHO Scheduled for Later.
			)		
		)
	)
	
	SET i = 1
	IF %var_noscreens% GTR 3 (
		ECHO Player Time Hour: %_thishour% Minute: %_thisminute%
	
		SET _currentschedule3=unscheduled
	
		FOR /F "tokens=1,2,3 delims=: " %%A IN (!var_schedulefile3!) DO (
			SET /A i += 1
			IF "%var_is64%"=="1" (
				Schtasks /create /tn "Schedule_S4-!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_S4-!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 3 Start Hour: %%B Minute: %%C tt: !tts!
			ECHO Checking !tts! LEQ !nts!
			IF !tts! LEQ !nts! (
				@ECHO Trigger this Schedule
				SET _currentschedule3=%%A
				@ECHO Setting Schedule3 to %%A
			) ELSE (
				@ECHO Scheduled for Later.
			)		
		)
	)
	
	DEL C:\LiveDigital\tasks.txt
	
	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!"
	)
	IF %var_noscreens% GTR 1 (
		ECHO Resulting Active Schedule1 is !_currentschedule1!
		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_screen1%&value=!_currentschedule1!"
		) ELSE (
			wget.exe --timeout=5 --tries=2 --delete-after "%var_pingaddress%%var_pingscript%?clientid=%var_client%&serverip=%var_supportip%&type=schedule&screen=%var_screen1%&value=!_currentschedule1!"
		)
	)

	IF %var_noscreens% GTR 2 (
		ECHO Resulting Active Schedule2 is !_currentschedule2!
		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_screen2%&value=!_currentschedule2!"
		) ELSE (
			wget.exe --timeout=5 --tries=2 --delete-after "%var_pingaddress%%var_pingscript%?clientid=%var_client%&serverip=%var_supportip%&type=schedule&screen=%var_screen2%&value=!_currentschedule2!"
		)
	)

	IF %var_noscreens% GTR 3 (
		ECHO Resulting Active Schedule3 is !_currentschedule3!
		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_screen3%&value=!_currentschedule3!"
		) ELSE (
			wget.exe --timeout=5 --tries=2 --delete-after "%var_pingaddress%%var_pingscript%?clientid=%var_client%&serverip=%var_supportip%&type=schedule&screen=%var_screen3%&value=!_currentschedule3!"
		)
	)
	
	ECHO Resulting Active Schedule0 is !_currentschedule0!
	ECHO Resulting Active Schedule1 is !_currentschedule1!
	ECHO Resulting Active Schedule2 is !_currentschedule2!
	ECHO Resulting Active Schedule3 is !_currentschedule3!

	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"
	IF %var_noscreens% GTR 1 (
		CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox2.exe" -no-remote -P instance1 "file:///C:/LiveDigital/live/screen%var_screen1%/!_currentschedule1!/playlist-1.html"
	)
	IF %var_noscreens% GTR 2 (
		CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox3.exe" -no-remote -P instance2 "file:///C:/LiveDigital/live/screen%var_screen2%/!_currentschedule2!/playlist-1.html"
	)
	IF %var_noscreens% GTR 3 (
		CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox4.exe" -no-remote -P instance3 "file:///C:/LiveDigital/live/screen%var_screen3%/!_currentschedule3!/playlist-1.html"
	)

	
) ELSE (
	IF %var_interactive% GTR 0 (
		CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox.exe" -no-remote -P instance0 "%var_interactiveurl%"

	) ELSE (
		CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox.exe" -no-remote -P instance0 "file:///C:/LiveDigital/live/screen%var_screen0%/playlist-1.html"
		PING 127.0.0.1 -n 4 -w 500
		CMDOW /RUN /HID "C:\LiveDigital\adjust.bat"
		START /B C:\LiveDigital\adjust.bat

		IF %var_noscreens% GTR 1 (
			CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox2.exe" -no-remote -P instance1 "file:///C:/LiveDigital/live/screen%var_screen1%/playlist-1.html"
			PING 127.0.0.1 -n 4 -w 500
			CMDOW /RUN /HID "C:\LiveDigital\adjust.bat"
			START /B C:\LiveDigital\adjust.bat
		)
		IF %var_noscreens% GTR 2 (
			CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox3.exe" -no-remote -P instance2 "file:///C:/LiveDigital/live/screen%var_screen2%/playlist-1.html"
			PING 127.0.0.1 -n 4 -w 500
			CMDOW /RUN /HID "C:\LiveDigital\adjust.bat"
			START /B C:\LiveDigital\adjust.bat
		)
		IF %var_noscreens% GTR 3 (
			CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox4.exe" -no-remote -P instance3 "file:///C:/LiveDigital/live/screen%var_screen3%/playlist-1.html"
			PING 127.0.0.1 -n 4 -w 500
			CMDOW /RUN /HID "C:\LiveDigital\adjust.bat"
			START /B C:\LiveDigital\adjust.bat	
		)
		IF %var_noscreens% GTR 4 (
			CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox5.exe" -no-remote -P instance4 "file:///C:/LiveDigital/live/screen%var_screen4%/playlist-1.html"
			PING 127.0.0.1 -n 4 -w 500
			CMDOW /RUN /HID "C:\LiveDigital\adjust.bat"
			START /B C:\LiveDigital\adjust.bat
		)
		IF %var_noscreens% GTR 5 (
			CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox6.exe" -no-remote -P instance5 "file:///C:/LiveDigital/live/screen%var_screen5%/playlist-1.html"
			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