set CHECK_INTERVAL=20

set PROGRAMNAME=java.exe
set DESKTOP_DIR=%USERPROFILE%\Desktop
set BACKUP_DIR=%DESKTOP_DIR%\auto_update_player_backup
set DMBPLAYER_DIR=%DESKTOP_DIR%"\dmbplayer"
set BACKUP_FOLDER=auto_update_player_backup
set DMBPLAYER_folder=dmbplayer
set UNZIP_APP=installation\unzip.exe
set UPDATE_ZIP=installation\dmbplayer.zip
set INSTALLATION_PKG_NAME=dmbplayer.zip
set INSTALLATION_FOLDER=%DESKTOP_DIR%\installation
set errmsg=final
set /p boxid=< "%INSTALLATION_FOLDER%\boxid.txt"
echo %boxid%
set token="menuboxid="%boxid%"&updatetoken=dmb20101512&reqtype=playerupdatefailed&errmsg="
set url=http://www.digitalmarketingbox.com/webtool/playerupdate.php
set finishdata="menuboxid="%boxid%"&updatetoken=dmb20101512&reqtype=completeupdate&os=windows&version=13.04&playerversionupdate=1"
set backupLogFolder="%BACKUP_DIR%\logs"
set LogFolder="%DMBPLAYER_DIR%\logs"
set backupResourceFolder="%BACKUP_DIR%\resources"
set ResourceFolder="%DMBPLAYER_DIR%\resources"


rem close player
taskkill /f /im cmd.exe /fi "windowtitle eq RestartPlayer.bat"
taskkill /f /im cmd.exe /fi "windowtitle eq Administrator: =RestartPlayer.bat"
ping -n %CHECK_INTERVAL% 127.0.0.1 > nul
taskkill /f /im java.exe

rem backup the old player 
cd %DESKTOP_DIR%
rd /s /q %BACKUP_FOLDER%
if not exist %BACKUP_FOLDER% mkdir %BACKUP_FOLDER%
xcopy /s %DMBPLAYER_folder% %BACKUP_FOLDER%
set _TMP=
	for /f "delims=" %%a in ('dir /b "%DESKTOP_DIR%/%BACKUP_FOLDER%\*.*"') do set _TMP=%%a
IF {%_TMP%}=={} (
	set errmsg=BackUpFailed
	goto UPDATEFAILED
  )


rem delete old dmbplayer folder
rd /s /q %DMBPLAYER_folder%

rem recreate dmbplayer folder and copy zip file to new dmbplayer folder. then extract the zip file
if not exist %DMBPLAYER_folder% mkdir %DMBPLAYER_folder%
xcopy /s %UNZIP_APP% %DMBPLAYER_folder%
xcopy /s %UPDATE_ZIP% %DMBPLAYER_folder%
cd %DMBPLAYER_DIR%
unzip -o %INSTALLATION_PKG_NAME%

set _TMP=
	for /f "delims=" %%a in ('dir /b "%DESKTOP_DIR%/%DMBPLAYER_folder%\*.*"') do set _TMP=%%a
IF {%_TMP%}=={} (
	set errmsg=ExtractNewPlayerFailed
	goto UPDATEFAILED
  )

rem remove unneccessary file
del unzip.exe
del dmbplayer.zip

if not exist %LogFolder% mkdir %LogFolder%
xcopy /s %backupLogFolder% %LogFolder%
if not exist %ResourceFolder% mkdir %ResourceFolder%
xcopy /s %backupResourceFolder% %ResourceFolder%
start RestartPlayer.bat

set cmd="tasklist /fi "ImageName eq java.exe" /nh"
ping -n 5 127.0.0.1 > nul
FOR /F "tokens=2" %%i IN (' %cmd% ') DO call SET firsttime=%%firsttime%%%%i
ping -n 20 127.0.0.1 > nul
FOR /F "tokens=2" %%i IN (' %cmd% ') DO call SET secondTime=%%secondTime%%%%i
echo %firsttime%
echo %secondTime%
if "%firsttime%" == "%secondTime%" if NOT "%secondTime%" == "No" (
cd %INSTALLATION_FOLDER%
curl --data %finishdata% %url%
)


exit

:UPDATEFAILED
cd %DESKTOP_DIR%/installation
curl --data %token%%errmsg% %url%
if %errmsg% == ExtractNewPlayerFailed goto UNDOUPDATE
cd %DESKTOP_DIR%%DMBPLAYER_folder%
start RestartPlayer.bat
exit

:UNDOUPDATE
cd %DESKTOP_DIR%
xcopy /s %BACKUP_FOLDER% %DMBPLAYER_folder%
cd %DMBPLAYER_folder%
start RestartPlayer.bat
exit