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
TASKKILL /IM firefox2.exe /F
TASKKILL /IM firefox3.exe /F
TASKKILL /IM firefox4.exe /F
TASKKILL /IM firefox5.exe /F
TASKKILL /IM firefox6.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
)

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

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

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

	COPY "C:\Program Files\Mozilla Firefox\firefox.exe" "C:\Program Files\Mozilla Firefox\firefox6.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
TASKKILL /IM firefox5.exe /F
TASKKILL /IM firefox6.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_schedulefile1=%var_livedir%\live\screen%var_screen2%\schedules.txt
	)
	IF %var_noscreens% GTR 3 (
		SET var_schedulefile1=%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

	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
		
		IF %%B LEQ %_thishour% (
			IF %%C LEQ %_thisminute% (
				SET currentschedule=%%A
				ECHO Setting Schedule to %%A
			)
		)
	)

	DEL C:\LiveDigital\tasks.txt

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

	IF %var_noscreens% GTR 1 (
		CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox2.exe" -no-remote -P instance1 -safe-mode "file:///C:/LiveDigital/live/screen%var_screen1%/!currentschedule!/playlist-1.html"	
	)

) ELSE (

	CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox.exe" -no-remote -P instance0 -safe-mode "file:///C:/LiveDigital/live/screen%var_screen0%/playlist-1.html"
	PAUSE
	
	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 -safe-mode "file:///C:/LiveDigital/live/screen%var_screen1%/playlist-1.html"	
		PAUSE
	)
	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 -safe-mode "file:///C:/LiveDigital/live/screen%var_screen2%/playlist-1.html"	
		PAUSE
	)
	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 -safe-mode "file:///C:/LiveDigital/live/screen%var_screen3%/playlist-1.html"	
		PAUSE
	)
	IF %var_noscreens% GTR 4 (
		PING 127.0.0.1 -n 4 -w 500
		CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox5.exe" -no-remote -P instance4 -safe-mode "file:///C:/LiveDigital/live/screen%var_screen4%/playlist-1.html"	
		PAUSE
	)
	IF %var_noscreens% GTR 5 (
		PING 127.0.0.1 -n 4 -w 500
		CMDOW /RUN /MAX "C:\Program Files\Mozilla Firefox\firefox6.exe" -no-remote -P instance5 -safe-mode "file:///C:/LiveDigital/live/screen%var_screen5%/playlist-1.html"	
		PAUSE
	)
)

PING 127.0.0.1 -n 4 -w 500

CALL C:\LiveDigital\adjust.bat


EXIT