@echo off

cd %~p0
cd..
set REL_DESKTOP_PATH=%cd:\=\\%

@title=RestartPlayer.bat
set CHECK_INTERVAL=20
set SCRIPTFILE_VERSION=1.6

set HEAPSIZE=768M
set JARNAME=dmbplayer.jar
set PROGRAMNAME=java.exe
set JAVACRASHPROGRAM=WerFault.exe

set INST_DIRNAME=dmbplayer
set INST_DIR="%REL_DESKTOP_PATH%\\%INST_DIRNAME%"

set CNT_MONITOR=0
set /a CNT_MONITOR_MAX=60/%CHECK_INTERVAL%

set SYSUPDATESCRIPTF_DIR="%REL_DESKTOP_PATH%\\sysUpdates"
set SYSUPDATESCRIPT=sysUpdate.bat

echo Script v%SCRIPTFILE_VERSION% is running...

cd %INST_DIR%

rem Sync time
rem Get system time with system time-zone and compare it with online time. Update online time to local time if differ.
 
rem kill the dmbplayer
taskkill /f /im %PROGRAMNAME%

:CHECKAGAIN
tasklist /FI "IMAGENAME eq %PROGRAMNAME%" 2>NUL | find /I /N "%PROGRAMNAME%">NUL
if "%ERRORLEVEL%"=="1" goto NOTRUNNING

goto ISRUNNING

:NOTRUNNING
echo DMBPlayer is NOT RUNNING. RESTARTING...

taskkill /f /im vlc.exe
tasklist /fi "imagename eq %PROGRAMNAME%" | findstr "%PROGRAMNAME%" >NUL && taskkill /f /im %PROGRAMNAME%
tasklist /fi "imagename eq %PROGRAMNAME%" | findstr "%PROGRAMNAME%" >NUL && goto NOTRUNNING
start java -jar -Xss1m -Xmx%HEAPSIZE% -Xms%HEAPSIZE% %JARNAME%

:ISRUNNING
tasklist /FI "IMAGENAME eq %JAVACRASHPROGRAM%" 2>NUL | find /I /N "%JAVACRASHPROGRAM%">NUL
if "%ERRORLEVEL%"=="0" goto ClOSEERRORWINDOW

cd %INST_DIR%\\scripts
set playerLeft=100
set playerTop=100
set playerWidth=1920
set playerHeight=1080

rem for /f %%i in ('XML.EXE sel -t -v "//playerLeft" %INST_DIR%\resources\xml\preferences.xml') do set playerLeft=%%i
rem for /f %%i in ('XML.EXE sel -t -v "//playerTop" %INST_DIR%\resources\xml\preferences.xml') do set playerTop=%%i
rem for /f %%i in ('XML.EXE sel -t -v "//playerWidth" %INST_DIR%\resources\xml\preferences.xml') do set playerWidth=%%i
rem for /f %%i in ('XML.EXE sel -t -v "//playerHeight" %INST_DIR%\resources\xml\preferences.xml') do set playerHeight=%%i

for /f "tokens=3 delims=<>" %%i in ('findstr "<playerLeft>" "%INST_DIR%\resources\xml\preferences.xml"') do set "playerLeft=%%i"
for /f "tokens=3 delims=<>" %%i in ('findstr "<playerTop>" "%INST_DIR%\resources\xml\preferences.xml"') do set "playerTop=%%i"
for /f "tokens=3 delims=<>" %%i in ('findstr "<playerWidth>" "%INST_DIR%\resources\xml\preferences.xml"') do set "playerWidth=%%i"
for /f "tokens=3 delims=<>" %%i in ('findstr "<playerHeight>" "%INST_DIR%\resources\xml\preferences.xml"') do set "playerHeight=%%i"

echo [LOG] PlayerLeft   = %playerLeft%
echo [LOG] PlayerTop    = %playerTop%
echo [LOG] PlayerWidth  = %playerWidth%
echo [LOG] PlayerHeight = %playerHeight%


set currentleft=0
set currenttop=0
set currentwidth=0
set currentheight=0
for /f "tokens=1-11" %%a in ('cmdow "DMBPLAYER" /P ^| findstr "Unknown java"') do (
set currentleft=%%h
set currenttop=%%i
set currentwidth=%%j
set currentheight=%%k
)

echo "player prefernce width x height" %playerWidth% %playerHeight%
echo "Running Player width x height" %currentwidth% %currentheight% 

if Not %playerLeft% == %currentleft% cmdow "DMBPLAYER" /mov %playerLeft% %playerTop%
if Not %playerTop% == %currenttop% cmdow "DMBPLAYER" /mov %playerLeft% %playerTop%
if Not %playerWidth% ==0 if Not %playerWidth% == %currentwidth% cmdow "DMBPLAYER" /siz %playerWidth% %playerHeight%
if Not %playerHeight% ==0 if Not %playerHeight% == %currentheight% cmdow "DMBPLAYER" /siz %playerWidth% %playerHeight%

cd %INST_DIR%

echo DMBPlayer is running. Waiting %CHECK_INTERVAL% seconds...
ping -n %CHECK_INTERVAL% 127.0.0.1 > nul

rem upgrading...
if exist %SYSUPDATESCRIPTF_DIR%\\%SYSUPDATESCRIPT% (    
    echo Start to upgrade system...
    cd %SYSUPDATESCRIPTF_DIR%
    start /wait %SYSUPDATESCRIPT%
    del %SYSUPDATESCRIPT%
    cd %INST_DIR%
    start RestartPlayer.bat
    exit
)

rem restarting...
set /a CNT_MONITOR=CNT_MONITOR+1

if %CNT_MONITOR%==%CNT_MONITOR_MAX% (
    goto RESTARTPLAYER
)

goto CHECKAGAIN

:ClOSEERRORWINDOW
taskkill /f /im %JAVACRASHPROGRAM%
goto NOTRUNNING


:RESTARTPLAYER
rem get restart time
set TIMER_FILE_NAME=%INST_DIR%\\resources\\restart_time.txt
set LIST_CMD=type %TIMER_FILE_NAME%
set RESTARTTIME=-1:-1
for /F "delims=" %%A in ('%LIST_CMD%') do (
    set RESTARTTIME=%%A
)
rem echo %RESTARTTIME%

set RESTART_MINUTE=%RESTARTTIME:~3,2%
set RESTART_HOUR_1=%RESTARTTIME:~0,1%
set RESTART_HOUR=%RESTARTTIME:~0,2%
if %RESTART_HOUR_1%==0 (
    set RESTART_HOUR=%RESTARTTIME:~1,1%
)

set RESTART_FLAG=0
FOR %%? IN (%TIMER_FILE_NAME%) DO ( SET /A STRLEN=%%~z? )
if %STRLEN%==7 (
    set RESTART_FLAG=%RESTARTTIME:~6,1%
)
rem echo RestartTime: %RESTART_HOUR%:%RESTART_MINUTE%

rem get current time
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (
	set CUR_HOUR=%%a
	set CUR_MINUTE=%%b
)
rem echo CurrentTime: %CUR_HOUR%:%CUR_MINUTE%
if not %RESTART_HOUR%==-1 (
    if %CUR_HOUR%==%RESTART_HOUR% (
        if %CUR_MINUTE%==%RESTART_MINUTE% (
            echo Restarting DMB player...
            
            if %RESTART_FLAG%==1 (
                shutdown /r
            ) else (
                rem just kill the dmbplayer process
                taskkill /f /im %PROGRAMNAME%
            )
        )
    )
)
set CNT_MONITOR=0

goto CHECKAGAIN

pause
