rem set USB_DRIVE_NAME=F

@echo off

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

set INST_FOLDER_NAME=dmbplayer
set DMBPLAYER_SCRIPT_NAME_OLD=dmbplayer.bat
set DMBPLAYER_SCRIPT_NAME=RestartPlayer.bat
set DMBPLAYER_SCRIPT_NAME_LNK=%DMBPLAYER_SCRIPT_NAME%.lnk

echo %USERNAME%

rem determine the USB driver letter
set USB_DRIVE_NAME=-1
set USB_FILE_NAME=updates_*.zip

if exist F:\\%USB_FILE_NAME% (
	set USB_DRIVE_NAME=F
) else (
	if exist E:\\%USB_FILE_NAME% (
		set USB_DRIVE_NAME=E
	) else (
		if exist G:\\%USB_FILE_NAME% (
			set USB_DRIVE_NAME=G
		) else (
			if exist D:\\%USB_FILE_NAME% (
				set USB_DRIVE_NAME=D
			) else (
				if exist H:\\%USB_FILE_NAME% (
					set USB_DRIVE_NAME=H
				)
			)
		)
	)
)

if %USB_DRIVE_NAME%==-1 (
	echo Updates.zip Not Found
	exit
)

echo Find update file: %USB_DRIVE_NAME%:\\%USB_FILE_NAME%


REM # getting the backup file name in USB drive
REM ########################################################################
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c%%a%%b)
For /f "tokens=1-2 delims=/:" %%a in ('echo %time%') do (set mytime=%%a%%b)

set suffix=%mydate%%mytime%
REM # backup file name suffix source folder
REM #########################################################################

set c_source=%USB_DRIVE_NAME%:\\updates_*.zip

set c_desti_bak_path=%REL_DESKTOP_PATH%\\%INST_FOLDER_NAME%\\resources\\update
set c_desti_bak="%c_desti_bak_path%\\updates_*.zip"

REM ################################ Getting boxid ############################

set c_source_backup=%USB_DRIVE_NAME%:\\updates%suffix%.zip
set c_desti="%REL_DESKTOP_PATH%\\%INST_FOLDER_NAME%\\resources\\update\\resources.zip"

set NEWPLAYER_PATH=dmbplayer
set playerpath="%REL_DESKTOP_PATH%\\%INST_FOLDER_NAME%"
set PlayerLibPath="%REL_DESKTOP_PATH%\\%INST_FOLDER_NAME%\\dmbplayer_lib"
	
if exist %USB_DRIVE_NAME%:\\ (

	REM # Checking if source update file is exists
	REM #########################################################################
	if exist %c_source% (

		REM ########################### copy source to destination backup file	
		mkdir "%c_desti_bak_path%"
		copy %c_source% %c_desti_bak%

		%USB_DRIVE_NAME%:
		mkdir backup
		del backup\\updates_*.zip
		copy updates_*.zip backup\\updates_*.zip
		del updates_*.zip
		
		echo "Player content updated successfully"
	) else (
		REM # Upadate file doesn't exists
		REM ##########################################################################
		echo "Player content doesn't exist"
	)

	
	REM ######################### this is player update ##########################
    %USB_DRIVE_NAME%:
	if exist %NEWPLAYER_PATH% (	
		taskkill /f /im java.exe
        
        rd /s /q %PlayerLibPath%
		xcopy /y %NEWPLAYER_PATH% %playerpath%
		ren %NEWPLAYER_PATH% player_backup
		
		C:
		cd "%REL_DESKTOP_PATH%"

		rem start the player
		set LIST_CMD=dir /a-d /o-d /b *.lnk
		for /F "delims=" %%A in ('%LIST_CMD%') do (
			echo.%%A | findstr /C:"layer" 1>nul
			if not errorlevel 1 (
				echo Get the old shortlink name: %%A
				start %%A
				goto :GET_BAT_FILE
			)
		)

		if exist %DMBPLAYER_SCRIPT_NAME_LNK% (
			start %DMBPLAYER_SCRIPT_NAME_LNK%
		) else (
			if exist %DMBPLAYER_SCRIPT_NAME% (
				start %DMBPLAYER_SCRIPT_NAME%
			) else (
				start %DMBPLAYER_SCRIPT_NAME_OLD%
			)	
		)

		:GET_BAT_FILE
		echo "Player updated successfully"

	) else ( 
		echo "Player update not available" 
	)

	REM ###########################################################################

) else ( 
	echo "USB drive doesn't exist" 
)