@echo off
cd /D %~dp0%

set KEY_NAME="HKEY_LOCAL_MACHINE\SOFTWARE\EMC\DDBFS"
set VALUE_NAME=Path

FOR /F "skip=2 tokens=1,2*" %%A IN ('REG QUERY "%KEY_NAME%" /v "%VALUE_NAME%" 2^>nul') DO (
    set ValueName=%%A
    set InstallDir=%%C\
)
if defined ValueName (
	@echo Install directory = %InstallDir%
) else (
    @echo FS Agent path not present.
    EXIT 0
)

IF "%InstallDir:~-1%"=="\" SET InstallDir=%InstallDir:~0,-1%
SET DpsappsDir=%InstallDir:fsagent=%
IF "%DpsappsDir:~-1%"=="\" SET DpsappsDir=%DpsappsDir:~0,-1%
@echo Dpsapps directory = %DpsappsDir%
set CUR_HH=%time:~0,2%
if %CUR_HH% lss 10 (set CUR_HH=0%time:~1,1%)
set DATE_TIME=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%-%CUR_HH%%TIME:~3,2%%TIME:~6,2%
set BACKUP="%InstallDir%backups\%DATE_TIME%"


if "%~1"=="-u" (
if "%~2"=="-a" (
echo Upgrading FsAgent...
CALL :UpgradeFsAgent
)
)
if "%~1"=="-u" (
if "%~2"=="-s" (
echo Upgrading Agent service...
CALL :UpgradeAgentService
)
)

if "%~1"=="-b" (
    CALL :BackupFile
)

if "%~1"=="-d" (
    CALL :DeleteBackup
)

if "%~1"=="-r" (
if "%~2"=="-n" (
    CALL :RestoreFile
)
)

if "%~1"=="-r" (
    set /p prompt="Should the pushupdate.bat file restore succeed [Y/N]? ")

if "%prompt%"=="Y" (
    CALL :RestoreFile
) else (
    EXIT 0
)


echo "Invalid parameters"
EXIT 3


:BackupFile

REM Creating Backup directory and backing up pushupdate.bat

mkdir %BACKUP%
if %ERRORLEVEL% NEQ 0 echo "Unable to create pushupdate script backup directory, backup will be aborted." & EXIT 4

Xcopy "%InstallDir%\bin\pushupdate.bat" %BACKUP% /E /H /C /I
if %ERRORLEVEL% NEQ 0 echo "Unable to copy pushupdate script to backup directory, backup will be aborted." & EXIT 4


if errorlevel 0 (
    echo "Pushupdate script backup successfull"
    EXIT 0
)

:RestoreFile

REM Restoring pushupdate.bat

SET BackupPath= "%InstallDir%backups"

FOR /F "delims=" %%i IN ('dir %BackupPath% /b /ad-h /t:c /od') DO SET LatestBackup=%%i

Xcopy "%InstallDir%backups\%LatestBackup%\pushupdate.bat" "%InstallDir%\bin" /E /H /C /I
if %ERRORLEVEL% NEQ 0 echo "Unable to copy pushupdate script to original directory, restore will be aborted." & EXIT 4

if errorlevel 0 (
    echo "Pushupdate.bat Restore successful"
    EXIT 0
)

:DeleteBackup

rmdir /s /q "%InstallDir%backups"

if errorlevel 0 (
    echo "Pushupdate script backup deletion successful"
    EXIT 0
) else (
    echo "Pushupdate script backup deletion failed"
    EXIT 4
)

:UpgradeFsAgent
REM Upgrading Fs Agent
Start /wait "$0\" fsagent-19.18.0.61.exe -s -upgrade SkipAgentServiceUpgrade=1 SkipAppAgentUpgrade=0 ForceUpgrade=1 EnableFirewallRules=1 ProductInstallPath="%DpsappsDir%"
if errorlevel 0 (
echo "Agent upgraded successfully"
EXIT 0) else (
echo "Agent Upgrade failed"
EXIT 1)

:UpgradeAgentService
REM Upgrading Agent service
Start /wait "$0\" fsagent-19.18.0.61.exe -s -upgrade SkipAgentServiceUpgrade=0 SkipAppAgentUpgrade=1 ForceUpgrade=1 EnableFirewallRules=1 ProductInstallPath="%DpsappsDir%"
if errorlevel 0 (
echo "Agent service upgraded successfully"
EXIT 0) else (
echo "Agent Service Upgrade failed"
EXIT 2)