@Echo off
SETLOCAL ENABLEEXTENSIONS
SET me=%~n0
SET parent=%~dp0

:: BatchGotAdmin
:-------------------------------------
REM  --> Check for permissions
    IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)

REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
    echo Requesting administrative privileges...
    goto UACPrompt
) else ( goto gotAdmin )

:UACPrompt
    echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
    set params = %*:"=""
    echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"

    "%temp%\getadmin.vbs"
    del "%temp%\getadmin.vbs"
    exit /B

:gotAdmin
    pushd "%CD%"
    CD /D "%~dp0"

CLS
Echo RailPro Assistant v3.00 offline Installer
Echo v1.2 by The other Tim
Echo.
Echo You use this offline installer at your own risk. It should work for any verions
Echo of Windows though it has not been tested on any version other than XP. You may
Echo need to run this batch file with administrator rights.
Echo.
Echo Press the Control (CTRL) and C keys together to exit now (and then press Y
Echo when asked to terminate the batch job) or 
pause

:Start

@echo off
set progDirRPA="c:\Program Files\Ring Engineering\RailPro Assistant\"
set dataDirRPA="c:\ProgramData\Ring Engineering\RailPro Assistant\"
set progNameRPA="RailProAssistant.exe"
set dataNameRPA="RailPro Assistant Storage"
set rpaDataBackup=False

rem check if RPA directory exists
Echo.
Echo Checking for previous installation of RailPro Assistant...
if not exist %progDirRPA% (
        goto :FullInstall
        ) else (
        goto :Upgrade
        )


:Upgrade
Echo.
Echo Performing Upgrade

set installType=Upgrade

rem check if folder writable
@echo>%progDirRPA%writable.txt
if not exist %progDirRPA%writable.txt (
		Echo.
		Echo Error. Program folder is not writable. Try running this batch file
		Echo with Administrator privileges. Exiting
		pause
		Exit/b		
	) Else (
		Echo.
		Echo Success. Program directory writable
	)


if not exist RailProAssistantPackage.exe (
         Echo RailProAssistantPackage.exe not found. Did you download it? 
		 Echo Please download it again. It must be downloaded to the same folder
		 Echo this batch file is located in. Exiting
         goto :Error
        )


if not exist %dataDirRPA% (
        Echo.
        Echo Data directory not found. Creating...
        mkdir %dataDirRPA%

		if exist %progDirRPA%%dataNameRPA% (
			Echo.
			Echo Moving Storage File...
			copy %progDirRPA%%dataNameRPA% %dataDirRPA%
                if %ERRORLEVEL% EQU 0 (
                  Echo.
				  Echo Success.
                  del %progDirRPA%%dataNameRPA%
				) else (
        Echo.
		Echo Storage file not found...
			)
		)
	)
if exist %dataDirRPA%%dataNameRPA% (
        Echo.
		Echo Backing up storage file to "RPA Storage.bak"
		copy %dataDirRPA%%dataNameRPA% %dataDirRPA%"RPA Storage.bak"
                if %ERRORLEVEL% EQU 0 (
                  Echo Success.
                  set rpaDataBackup=True          
				) else (
				Echo Storage file not found...
				set rpaDataBackup=False
				)
	)

if exist %progDirRPA%RailProAssistantPackage.exe rename %progDirRPA%RailProAssistantPackage.exe RailProAssistantPackage.bak

if exist %progDirRPA%RailProAssistantUnpacker.exe del %progDirRPA%RailProAssistantUnpacker.exe

if exist %progDirRPA%*.rsc (del %progDirRPA%*.rsc)

Echo.
Echo Copying RailProAssistantPackage.exe
copy RailProAssistantPackage.exe %progDirRPA%
if %ERRORLEVEL% EQU 0 (
                  Echo Success.
        ) else (
        Echo Copying failed. Exiting.
        goto :Error
        )



if exist %progDirRPA%%progNameRPA% rename %progDirRPA%%progNameRPA% "RailProAssistant.bak"

rem Run the package
%progDirRPA%RailProAssistantPackage.exe

Echo.
Echo Moving files...
move %progDirRPA%*.rsc %dataDirRPA%
if %ERRORLEVEL% EQU 0 (
                  Echo Success.
        ) else (
        Echo Moving failed. Exiting.
        goto :Error
        )

goto :End


:FullInstall

Echo.
Echo Performing Full Install
set installType=Full

rem check if folder writable
mkdir %progDirRPA%
if not exist %progDirRPA% (
		Echo.
		Echo Error. Program Files folder is not writable. Try running this batch file
		Echo with Administrator privileges. Exiting
		pause
		Exit/b		
	) Else (
		Echo.
		Echo Success. Program directory writable
	)

if not exist RailProAssistantPackage.exe (
         Echo.
         Echo RailProAssistantPackage.exe not found. Did you download it?
         Echo Please download it again. It must be downloaded to the same folder
		 Echo this batch file is located in. Exiting
		 goto :Error
        )

if not exist ftd2xx.dll (
         Echo.
         Echo ftd2xx.dll not found. Did you download it?
         Echo Please download it again. It must be downloaded to the same folder
		 Echo this batch file is located in. Exiting
		 goto :Error
        )

if not exist CDM20600.exe (
         Echo.
         Echo CDM20600.exe not found. Did you download it?
         Echo Please download it again. It must be downloaded to the same folder
		 Echo this batch file is located in. Exiting
		 goto :Error
        )


if not exist %progDirRPA% mkdir %progDirRPA%
if not exist %dataDirRPA% mkdir %dataDirRPA%

Echo.
Echo Copying RailProAssistantPackage.exe
copy RailProAssistantPackage.exe %progDirRPA%
if %ERRORLEVEL% EQU 0 (
                  Echo Success.
        ) else (
        Echo Copying failed. Exiting.
        goto :Error
        )

Echo Copying ftd2xx.dll
copy ftd2xx.dll %progDirRPA%
if %ERRORLEVEL% EQU 0 (
                  Echo Success.
        ) else (
        Echo Copying failed. Exiting.
        goto :Error
        )

Echo Copying CDM20600.exe
copy CDM20600.exe %progDirRPA%
if %ERRORLEVEL% EQU 0 (
                  Echo Success.
        ) else (
        Echo Copying failed. Exiting.
        goto :Error
        )


%progDirRPA%RailProAssistantPackage.exe

Echo.
Echo Moving files...
move %progDirRPA%*.rsc %dataDirRPA%
if %ERRORLEVEL% EQU 0 (
                  Echo Success.
        ) else (
        Echo Moving failed. Exiting.
        goto :Error
        )

rem %progDirRPA%CDM20600.exe

goto :End

:Error
exit /b


:End

set RPAlink="RailPro Assistant.lnk"

if not exist "%userprofile%\Desktop\"%RPAlink% (
        copy %progDirRPA%%RPAlink% "%userprofile%\Desktop\"
        )

Echo.
Echo Tidying up...
Echo Deleting unneeded installation files.
if exist %progDirRPA%RailProAssistant.bak del %progDirRPA%RailProAssistant.bak
if exist %progDirRPA%RailProAssistantPackage.bak del %progDirRPA%RailProAssistantPackage.bak
if exist %progDirRPA%writable.txt del %progDirRPA%writable.txt
rem del RailProAssistantPackage.exe
rem del ftd2xx.dll
rem del CDM20600.exe		
		
Echo Finished...
Echo.

if %installType%==Upgrade (
					Echo You performed an Upgrade of RailPro Assistant
					
					if %rpaDataBackup%==True (
							Echo.
							Echo Your Storage file was backed up as part of the installation process.
							Echo.        
							Echo Once you have run RailPro Assistant and verified that all is working correctly,
							Echo you can delete the backup storage file at %dataDirRPA%RPA Storage.bak
							Echo.

						) Else (

							Echo.
							Echo Your storage file was not backed up as it was not found.
						)
					 ) Else (
							Echo Your performed a Full Install of RailPro Assistant
							Echo.
							Echo There is nothing further that you need to do.
					)
pause
goto :EOF



:EOF
