:: Cache some environment variables.
@set ROOT=%~dp0
@set SCRIPT_NAME=%~nx0

:: Set the default arguments
@set TARGET_ARCH=
@set ARCH_PATH=
@set WIN_TITLE_ARCH=
@set PILSSCMD_DEBUG_LEVEL=

:ParseArgs
:: Parse the incoming arguments
@if /i "%1"==""          goto Checkargs
@if /i "%1"=="x86"      (set TARGET_ARCH=x86)        & (set ARCH_PATH=x86)    & shift & goto ParseArgs
@if /i "%1"=="x64"      (set TARGET_ARCH=x64)        & (set ARCH_PATH=x64) & shift & goto ParseArgs
@if /i "%1"=="debug"    (set PILSSCMD_DEBUG_LEVEL=1) & shift & goto ParseArgs
@if /i "%1"=="debug1"   (set PILSSCMD_DEBUG_LEVEL=1) & shift & goto ParseArgs
@if /i "%1"=="debug2"   (set PILSSCMD_DEBUG_LEVEL=2) & shift & goto ParseArgs
@if /i "%1"=="debug3"   (set PILSSCMD_DEBUG_LEVEL=3) & shift & goto ParseArgs

:Checkargs
:: Set debug level first
@if defined PILSSCMD_DEBUG_LEVEL (echo PILSSCMD_DEBUG_LEVEL: %PILSSCMD_DEBUG_LEVEL% & echo.)
@if NOT "%PILSSCMD_DEBUG_LEVEL%" GEQ "3" @echo off
@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "2" pause
:: target architecture is mandatory
if /i "%TARGET_ARCH%"=="" goto Syntax
goto Build

:Build
:: set variables
@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "1" echo Set variables & echo.
@rem set PILSSCMD_VER_SHORT=2.0
set PILSSCMD_VER_SEMANTIC=V2.3.0
set PRODUCT_NAME_SHORT=PILSS CLI
set PRODUCT_NAME_FULL=PILSS Command Line Interface
set PRODUCT_SUBTITLE=Pipelines, umbilicals, cables and flexibles dynamic Lateral Stability Software

@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "2" pause

:: set calculated variables
@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "1" echo Set calculated variables & echo.
if /i "%TARGET_ARCH%"=="x86"         set WIN_TITLE_ARCH=32-bit
if /i "%TARGET_ARCH%"=="x64"         set WIN_TITLE_ARCH=64-bit

:: print PILSS CLI headers
:print_PILSScli_headers
@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "1" echo Set cmd window bar title & echo.
@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "2" pause
set WIN_TITLE=%PRODUCT_NAME_SHORT% %WIN_TITLE_ARCH% %PILSSCMD_VER_SEMANTIC%
title %WIN_TITLE%
@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "2" pause

@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "1" echo Print PILSS CLI header & echo.
@echo ***********************************************************************************
@echo  %PRODUCT_NAME_FULL% %PILSSCMD_VER_SEMANTIC%
@echo   "%PRODUCT_SUBTITLE%"
@REM @echo  Copyright ^(C^) 2024 DNV
@echo ***********************************************************************************
@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "2" pause

:: Set environmental PATH variable
@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "1" echo Set environmental PATH variable & echo.

if /i "%PILSSCMD_VER_SEMANTIC%"=="vD.E.V.DEV" (
	echo "DEV NOTE! PILSS environmental variable must be set by installer (or manually by developer), e.g. 'PILSS_V_2_0_0' (or 'PILSS_V_2_3_0')." & echo.
)

if not defined PILSS_V_2_3_0 (
	echo ERROR: PILSS_V_2_3_0 is NOT defined!
)
if defined PILSS_V_2_3_0 (
	echo Type 'pilss' to get started.
)

if /i "%TARGET_ARCH%"=="x86" (
	if defined PILSS_V_2_3_0 (
		if exist "%PILSS_V_2_3_0%\bin\x86" (
			set "PATH=%PILSS_V_2_3_0%\bin\x86;%PATH%"
			@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "2" pause
		)
		goto End
	)
)
if /i "%TARGET_ARCH%"=="x64" (
	if defined PILSS_V_2_3_0 (
		if exist "%PILSS_V_2_3_0%\bin\x64" (
			set "PATH=%PILSS_V_2_3_0%\bin\x64;%PATH%"
			@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "2" pause
		)
		goto End
	)
)
@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "2" pause

:: end
goto End

:Error
@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "1" echo Error section: & echo.
echo Invalid command line argument: %1
echo.
@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "2" pause
exit /B 1

:Syntax
@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "1" echo Syntax section: & echo.
@echo Syntax:
@echo  %SCRIPT_NAME% [-arch] ^<arch^> [debug]
@echo.
@echo    ^<arch^> must be is one of the following
@echo        x86         : Set up for 32-bit target
@echo        x64         : Set up for 64-bit target
@echo    If specified, ^<debug^> must be one of the following
@echo        debug       : Enable debugging with default debug level (1)
@echo        debug1      : Debug level 1 - print debug messages to screen
@echo        debug2      : Debug level 2 - add 'pause' after each significant command
@echo        debug3      : Debug level 3 - echo on (print every line not starting with '@' to screen
@echo    If ^<debug^> is not specified, the script will run without stop.
@echo.
@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "2" pause
exit /B 1

:ResetTempVariables
@set ROOT=
@set SCRIPT_NAME=
@set TARGET_ARCH=
@set ARCH_PATH=
@set WIN_TITLE_ARCH=
@set PILSSCMD_DEBUG_LEVEL=
@set PILSSCMD_VER_SHORT=
@set PILSSCMD_VER_SEMANTIC=
@set PRODUCT_NAME_SHORT=
@set PRODUCT_NAME_FULL=
@set PRODUCT_SUBTITLE=
@set WIN_TITLE=

:End
@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "1" echo End section: & echo.
@if "%PILSSCMD_DEBUG_LEVEL%" GEQ "2" pause
exit /B 0


