CMDOW @ /HID
@ECHO OFF
CALL C:\LiveDigital\config.bat

IF EXIST C:\LiveDigital\timer.txt ( EXIT )

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

SETLOCAL EnableDelayedExpansion

TASKKILL /IM firefox.exe /F
TASKKILL /IM firefox2.exe /F
TASKKILL /IM firefox3.exe /F
TASKKILL /IM firefox4.exe /F

PING 127.0.0.1 -n 2 -w 500

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

	COPY "C:\Program Files\Mozilla Firefox\firefox.exe" "C:\Program Files\Mozilla Firefox\firefox2.exe"
	PING 127.0.0.1 -n 4 -w 500
)

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

	COPY "C:\Program Files\Mozilla Firefox\firefox.exe" "C:\Program Files\Mozilla Firefox\firefox3.exe"
	PING 127.0.0.1 -n 4 -w 500
)

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

	COPY "C:\Program Files\Mozilla Firefox\firefox.exe" "C:\Program Files\Mozilla Firefox\firefox4.exe"
	PING 127.0.0.1 -n 4 -w 500
)

TASKKILL /IM firefox.exe /F
TASKKILL /IM firefox2.exe /F
TASKKILL /IM firefox3.exe /F
TASKKILL /IM firefox4.exe /F

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
	)

	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
		Schtasks /create /tn "Schedule_S1%%A!i!" /sc daily /st %%B:%%C /tr "C:\LiveDigital\hstart.exe /NOCONSOLE /ELEVATE ""C:\LiveDigital\startplayer.bat""" /rl HIGHEST
		
		SET /A _tasktime=%%B*60+%%C
		CALL SET tasktime!i!=!_tasktime!
		
		ECHO Setting tasktime!i! to !tasktime%i%!
		ECHO Schedule Screen 0 Start Hour: %%B Minute: %%C tt: !tasktime[%i%]!	
		
		IF %%B==%_thishour% (
			ECHO Hours are equal, checking minutes
		
			IF %%C LEQ %_thisminute% (
				ECHO Minutes are lte, setting schedule
				SET _currentschedule0=%%A
				ECHO Setting Schedule0 to %%A
			)
		)

		IF %%B LSS %_thishour% (
			ECHO Schedule Start Hour is lss to Now Hour %%B lte %_thishour%
		
			SET _currentschedule0=%%A
			ECHO Setting Schedule0 to %%A		
		)
	)
	
	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 _tasktime=%%B*60+%%C
			SET scheduleexists=0
			for /L %%k in (1, 1, !i!) do (
				REM ECHO Checking !_tasktime! VS !tasktime%%k! - %%k
				IF !_tasktime!==!tasktime%%k! (
					SET scheduleexists=1
					ECHO Schedule Clash: !_tasktime! and !tasktime%%k!
				)
			)
			
			IF !scheduleexists!==0 (
				SET /A i += 1
				Schtasks /create /tn "Schedule_S2%%A!i!" /sc daily /st %%B:%%C /tr "C:\LiveDigital\hstart.exe /NOCONSOLE /ELEVATE ""C:\LiveDigital\startplayer.bat""" /rl HIGHEST
			
				ECHO Schedule Created: Screen 2 Start Hour: %%B Minute: %%C tt: !tasktime[%i%]!	
			) ELSE (
				ECHO Schedule Existed: Screen 2 Start Hour: %%B Minute: %%C tt: !tasktime[%i%]!	
			)
			
			IF %%B==%_thishour% (
				ECHO Hours are equal, checking minutes
		
				IF %%C LEQ %_thisminute% (
					ECHO Minutes are lte, setting schedule
					SET _currentschedule1=%%A
					ECHO Setting Schedule1 to %%A
				)
			)

			IF %%B LSS %_thishour% (
				ECHO Schedule Start Hour is lss to Now Hour %%B lte %_thishour%
		
				SET _currentschedule1=%%A
				ECHO Setting Schedule1 to %%A		
			)
		)
	)

	
	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 _tasktime=%%B*60+%%C
			SET scheduleexists=0
			for /L %%k in (1, 1, !i!) do (
				REM ECHO Checking !_tasktime! VS !tasktime%%k! - %%k
				IF !_tasktime!==!tasktime%%k! (
					SET scheduleexists=1
					ECHO Schedule Clash: !_tasktime! and !tasktime%%k!
				)
			)
						
			IF !scheduleexists!==0 (
				SET /A i += 1
				Schtasks /create /tn "Schedule_S3%%A!i!" /sc daily /st %%B:%%C /tr "C:\LiveDigital\hstart.exe /NOCONSOLE /ELEVATE ""C:\LiveDigital\startplayer.bat""" /rl HIGHEST
			
				ECHO Schedule Created: Screen 3 Start Hour: %%B Minute: %%C tt: !tasktime[%i%]!	
			) ELSE (
				ECHO Schedule Existed: Screen 3 Start Hour: %%B Minute: %%C tt: !tasktime[%i%]!	
			)
			
			IF %%B==%_thishour% (
				ECHO Hours are equal, checking minutes
		
				IF %%C LEQ %_thisminute% (
					ECHO Minutes are lte, setting schedule
					SET _currentschedule2=%%A
					ECHO Setting Schedule2 to %%A
				)
			)

			IF %%B LSS %_thishour% (
				ECHO Schedule Start Hour is lss to Now Hour %%B lte %_thishour%
		
				SET _currentschedule2=%%A
				ECHO Setting Schedule2 to %%A		
			)
		)
	)
	
	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 _tasktime=%%B*60+%%C
			SET scheduleexists=0
			for /L %%k in (1, 1, !i!) do (
				REM ECHO Checking !_tasktime! VS !tasktime%%k! - %%k
				IF !_tasktime!==!tasktime%%k! (
					SET scheduleexists=1
					ECHO Schedule Clash: !_tasktime! and !tasktime%%k!
				)
			)
			
			IF !scheduleexists!==0 (
				SET /A i += 1
				Schtasks /create /tn "Schedule_S4%%A!i!" /sc daily /st %%B:%%C /tr "C:\LiveDigital\hstart.exe /NOCONSOLE /ELEVATE ""C:\LiveDigital\startplayer.bat""" /rl HIGHEST
			
				ECHO Schedule Created: Screen 4 Start Hour: %%B Minute: %%C tt: !tasktime[%i%]!	
			) ELSE (
				ECHO Schedule Existed: Screen 4 Start Hour: %%B Minute: %%C tt: !tasktime[%i%]!	
			)
			
			IF %%B==%_thishour% (
				ECHO Hours are equal, checking minutes
		
				IF %%C LEQ %_thisminute% (
					ECHO Minutes are lte, setting schedule
					SET _currentschedule3=%%A
					ECHO Setting Schedule3 to %%A
				)
			)

			IF %%B LSS %_thishour% (
				ECHO Schedule Start Hour is lss to Now Hour %%B lte %_thishour%
		
				SET _currentschedule3=%%A
				ECHO Setting Schedule3 to %%A		
			)
		)
	)
	
	DEL C:\LiveDigital\tasks.txt
	
	ECHO Resulting Active Schedule0 is !_currentschedule0!
	ECHO Resulting Active Schedule1 is !_currentschedule1!
	ECHO Resulting Active Schedule2 is !_currentschedule2!
	ECHO Resulting Active Schedule3 is !_currentschedule3!
	
	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 (

	CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox.exe" -no-remote -P instance0 "file:///C:/LiveDigital/live/screen%var_screen0%/playlist-1.html"

	IF %var_noscreens% GTR 1 (
		PING 127.0.0.1 -n 4 -w 500
		CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox2.exe" -no-remote -P instance1 "file:///C:/LiveDigital/live/screen%var_screen1%/playlist-1.html"	
	)
	IF %var_noscreens% GTR 2 (
		PING 127.0.0.1 -n 4 -w 500
		CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox3.exe" -no-remote -P instance2 "file:///C:/LiveDigital/live/screen%var_screen2%/playlist-1.html"	
	)
	IF %var_noscreens% GTR 3 (
		PING 127.0.0.1 -n 4 -w 500
		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

PAUSE

CALL C:\LiveDigital\adjust.bat


EXIT