@echo off
setlocal EnableDelayedExpansion
chcp 65001 >nul

rem ### DEFAULT PLATFORM NAME EXTENSIONS FOR PRODUCT ###########################
set NAME=HDClone
set X86=32
set X64=
set AA64=.A64
rem ############################################################################

set NL=^


set txt[0]=Starting Boot Setup...
set txt[1]=Please use "Start as Administrator" for this batch file.
set txt[2]=Please copy package files to a local disk and start over.
set txt[3]=Startup file not found:
set txt[4]=Program has already been started in another instance.
set txt[5]=Opening parentheses "(" in the pathname are not allowd.!NL!Please rename the file path.
set txt[10]=Es ist ein unbekannter Fehler aufgetreten.

set tmpfile=%PUBLIC%\_symbstmp1.txt

set probe=%~f0
set probe=!probe:^(=!

net session >nul 2>&1

if %ERRORLEVEL% EQU 0 (
   set startNEL=0
   set params=%*
   if "%~1"=="NEL" (
      echo STA1> %tmpfile%
      set startNEL=1
      set params=
      for /f "usebackq tokens=1,*" %%G in ('%*') do (
         set params=%%H
      )
   )
   set aext=%X86%
   if not "%ProgramFiles(x86)%"=="" set aext=%X64%
   if "%PROCESSOR_ARCHITECTURE%"=="AMD64" set aext=%X64%
   if "%PROCESSOR_ARCHITECTURE%"=="ARM64" set aext=%AA64%
   if "%PROCESSOR_ARCHITECTURE%"=="X86" set aext=%X86%
   if not exist "%~dp0%NAME%%aext%.exe" (
      if exist "%~dp0%NAME%%X86%.exe" (
         set aext=%X86%
      )
   )
   if not exist "%~dp0%NAME%!aext!.exe" (
      if not exist "%~dp0" (
         if !startNEL!==1 (echo ERR2> "%tmpfile%") else (echo !txt[2]!)
      ) else (
         if !startNEL!==1 (
            echo ERR3 ==^> "%~dp0%NAME%!aext!.exe"> "%tmpfile%"
         ) else (
            echo !txt[3]!
            echo ==^> "%~dp0%NAME%!aext!.exe"
         )
      )
   ) else (
      start "" "%~dp0%NAME%!aext!.exe" install !params!
      if !startNEL!==1 echo OK> "%tmpfile%"
   )
) else (
   echo !txt[0]!
   set _return=0
   echo TST> "%tmpfile%"
   set /p tmptest= < "%tmpfile%"
   if not "!tmptest!"=="TST" (
      set _return=1
   ) else (
      powershell start-process -WindowStyle hidden -wait -verb runas '"%~f0"' -argumentlist 'NEL %*' >nul
      if "!errorlevel!"=="1" (
         set _return=1
         set netdrive=0
         for /f "tokens=1,2,3" %%G in ('net use') do (
            set drive=%%H
            if "!drive:~1,1!"==":" (
               if "!drive~0,2!"=="%~d0" set netdrive=1
            )
         )
         for /f "tokens=1,2,3" %%G in ('subst') do (
            set drive=%%G
            if "!drive:~1,1!"==":" (
               if "!drive:~0,2!"=="%~d0" set netdrive=1
            )
         )
         if !netdrive!==1 set _return=2
      ) else (
         if "!errorlevel!"=="2" (
            set _return=4
         ) else (
            set _exitcode=
            if exist "%tmpfile%" (
               set /p _exitcode= < "%tmpfile%"
               if not "!_exitcode:~0,3!"=="TST" (
                  if not "!_exitcode:~0,2!"=="OK" (
                     if "!_exitcode:~0,3!"=="ERR" (
                        set _return=!_exitcode:~3,1!
                        set _returnmsg=!_exitcode:~5!
                     ) else (
                        set _return=1
                     )
                  )
               ) else (
                  if not "!probe!"=="%~f0" (
                     set _return=5
                  ) else (
                     set _return=2
                  )
               )
            ) else (
               set _return=1
            )
         )
      )
   )
   del "%tmpfile%" >nul
   if !_return! GTR 0 (
      echo.
      for %%t in (!_return!) do echo !txt[%%t]!
      if not "!_returnmsg!"=="" echo !_returnmsg!
      echo.
      pause
   )
   endlocal
   exit /b !_return!
)
:finish
endlocal