@echo off

@REM Common variables:
@set MAINAPP=hdm17.exe
@set NETCONFIG_APP=netconfig.exe
@set PNPENFORCE_APP=pnpenforce.exe
@set LOGFILE_PATH=%WinDir%\temp\log.log
@set CUSTOM_FOLDER_PATH=%systemdrive%\custom

@REM This script is optional. If found - it applied. It can be placed to
@REM 'custom' folder. It must contain only commands of setting locales. 
@REM It ought to be generated by rmb engine when setting desired 
@REM locales in winpe.
@set CUSTOM_LOCALES_SCRIPT=setup_kblayout.bat

@REM The file rc.local.bat is optional script that can be placed to 'custom'
@REM folder. If found - it will be executed just before main app start
@REM It can contain any script actions.
@set CUSTOM_OPT_SCRIPT=rc.local.bat

@REM The file rc.conf.local.bat is optional script that can be placed to 'custom'
@REM folder. If found - it will apply variables overwriting just before any
@REM actions.
@REM IMPORTANT: It can contain ONLY overwriting existing variables instructions, 
@REM for example:
@REM SET MAINAPP=hdm18.exe
@set CUSTOM_CONF=rc.conf.local.bat

@REM Overwrite preset variables if _optional_ config found:
@if exist %CUSTOM_FOLDER_PATH%\%CUSTOM_CONF% (
    pushd %CUSTOM_FOLDER_PATH%
    @call %CUSTOM_FOLDER_PATH%\%CUSTOM_CONF%
    popd
    ) 

call :progress init
wpeinit
@REM Pnp enforce app must be placed in windows/system32 forder if exist
@if exist %PNPENFORCE_APP% (
    @call %PNPENFORCE_APP%
    ) 

call :progress registry
rem reg for NTLM
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa" /v lmcompatibilitylevel /t reg_dword /d 00000001 /f
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v Hidden /t reg_dword /d 00000001 /f
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" /v ShowSuperHidden /t reg_dword /d 00000001 /f

call :progress firewall
rem echo Stop windows firewall and related services
net stop MpsSvc /y > %LOGFILE_PATH%
net stop IKEEXT /y > %LOGFILE_PATH%
net stop BFE /y > %LOGFILE_PATH%

@REM -------------------------------------------------------------------------
@REM Apply custom configuration
@REM ------------------------------------------------------------------------- 

@REM echo Parse \custom storage
if not exist %CUSTOM_FOLDER_PATH% goto continue
cd /D %CUSTOM_FOLDER_PATH%

@REM Apply custom locales setup if optional script present
@if exist %CUSTOM_FOLDER_PATH%\%CUSTOM_LOCALES_SCRIPT% (
    @call %CUSTOM_FOLDER_PATH%\%CUSTOM_LOCALES_SCRIPT%
    ) 

:interfaces
ipconfig /all > lans.lst
if errorlevel 1 goto error

call :progress network
:netconfig
echo apply settings for network adapters
set NETWORK_MANUAL_FLAG=false
if exist network.ini (
    call netcfg.bat
) else (
    echo start network manually
    set NETWORK_MANUAL_FLAG=true
    cd "%systemdrive%\programs\Paragon Software\program"
    call %NETCONFIG_APP%
    cd /D %CUSTOM_FOLDER_PATH%
)
if errorlevel 1 goto error

call :progress mount
:mount
rem echo mount shared folder if it's defined in configuration
if exist %CUSTOM_FOLDER_PATH%\share.bat (
    cd "%systemdrive%\programs\Paragon Software\program"
    call %NETCONFIG_APP% -mount %CUSTOM_FOLDER_PATH%
) else if "%NETWORK_MANUAL_FLAG%"=="false" (
    echo start mount network share manually
    cd "%systemdrive%\programs\Paragon Software\program"
    call %NETCONFIG_APP%
)

cd /D %CUSTOM_FOLDER_PATH%
if %errorlevel% equ 0 goto continue
if %errorlevel% equ -1 echo Can't read share folder
:error
echo Error occurred %errorlevel%

call :progress partition
:continue
cd /d %systemdrive%\windows\system32
set flag=
diskpart /s diskpart_script.txt >out1.txt
FIND /I "winpe" out1.txt > out2.txt
FOR /F "tokens=2,3*" %%i in (out2.txt) do (
	if not "%%j"=="" ( set flag=%%j
	goto do_autotest 
)
)

call :progress autotests
:do_autotest
rem verify gate for autotest
if not "%flag%"=="" (
	if EXIST "%flag%:\ParagonAutoTest1234\autotest4321.cmd" (
		%flag%:\ParagonAutoTest1234\autotest4321.cmd %flag%
		goto :eof
	)
)

@REM If _optional_ user script is placed in custom folder, run it before main app
@if exist %CUSTOM_FOLDER_PATH%\%CUSTOM_OPT_SCRIPT% (
    pushd %CUSTOM_FOLDER_PATH%
    @call %CUSTOM_FOLDER_PATH%\%CUSTOM_OPT_SCRIPT%
    popd
    ) 

rem cd /D X:\Programs\
rem echo ####exit >X:\Programs\splash_mes.txt

@REM -------------------------------------------------------------------------
@REM Finally start main application
@REM -------------------------------------------------------------------------
cd "%systemdrive%\programs\Paragon Software\program"

call :progress
echo start ExpLauncher
%MAINAPP%

goto :eof

:progress
start "" "%systemdrive%\programs\Paragon Software\program\winpe_progress.exe" %1
goto :eof

