@echo off
echo.
echo   System Unit Build Script                         (C) 2000 vpascal.com
echo.

rem Run with ASM as parameter to re-assemble .asm files. This requires
rem that TASM.EXE is available in PATH.

rem Make environment space available in Windows NT:
set processor_identifier=

rem *** Change this line according to where VP is installed
set VPBase={BASEDIR}
if not exist "%vpbase%\bin.os2\vpc.exe" goto help

if "%1"=="D32" goto DPMI32Target
if "%1"=="d32" goto DPMI32Target
if "%1"=="OS2" goto Os2Target
if "%1"=="os2" goto Os2Target
if "%1"=="W32" goto Win32Target
if "%1"=="w32" goto Win32Target
if "%1"=="LNX" goto LinuxTarget
if "%1"=="lnx" goto LinuxTarget

echo ! Cannot execute, required parameter missing.
echo     MakeSys OS2,W32,D32 or LNX [ASM Ý Options]
echo   where
echo     OS2: build the OS/2 System unit
echo     W32: build the Win32 System unit
echo     D32: build the DPMI32 System unit
echo     LNX: build the Linux System unit
echo     ASM: If specified, .asm files will be recompiled.  Required TASM in path.
echo     Options: Extra options passed to the compiler
echo.
goto end

:help
echo Cannot execute; VPBase setting is probably incorrect.  Please modify this
echo script to contain the correct location of your VP installation.
echo.
goto end

:LinuxTarget
  set Target=lnx
  set option=-CW:LNX:LINUX
goto CheckOS

:DPMI32Target
  set TARGET=d32
  set option=-CW:D32:DPMI32
goto CheckOS

:Os2Target
  set TARGET=os2
  set option=-CO
goto CheckOS

:Win32Target
  set TARGET=w32
  set option=-CW

:CheckOS
echo - Determining under which OS we are running
  %vpbase%\bin.os2\vpc.exe >nul
  if errorlevel 255 goto IsWin32
  if errorlevel 254 goto IsOs2
  goto IsWin32
:IsOs2
  set OS=os2
  goto GetOptions
:IsWin32
  set OS=w32

:GetOptions
if "%2" == "ASM" goto MakeSystem
  set option=%option% %2
  echo Options are %option%

:MakeSystem
echo   Current OS is %OS%
echo - Building System unit
"%vpbase%\bin.%os%\vpc" %option% system "/O%vpbase%\out.%target%\units"
if errorlevel 1 goto error

if not "%2"=="ASM" goto DoneAsm
Echo - Assembling .asm files
tasm io.asm /q /oi /m /ml
tasm thunk.asm /q /oi /m /ml
tasm iodos.asm /q /oi /m /ml
:DoneAsm

cd ..
echo - Linking VpSysLow to new System unit
"%vpbase%\bin.%os%\vpc" %option% vpsyslow /$SmartLink+ "/O%vpbase%\out.%target%\units" "/U%vpbase%\source\%target%" /m >nul
if errorlevel 1 goto error
if .%target% == .w32 goto LibW32
if .%target% == .d32 goto LibD32
if .%target% == .lnx goto LibLnx

echo - Linking OS/2-specific .obj files to new System unit
"%vpbase%\bin.%os%\lib" "%vpbase%\out.%target%\units\system.lib" +sys\io +sys\thunk "+%vpbase%\out.%target%\units\vpsyslow.lib" "+%vpbase%\out.%target%\units\strings.lib">nul
if errorlevel 1 goto error
goto ok

:LibW32
echo - Linking Win32-specific .obj files to new System unit
"%vpbase%\bin.%os%\lib" "%vpbase%\out.%target%\units\system.lib" +sys\iodos "+%vpbase%\out.%target%\units\vpsyslow.lib" >nul
if errorlevel 1 goto error
goto ok

:LibD32
echo - Linking DPMI32-specific .obj files to new System unit
"%vpbase%\bin.%os%\lib" "%vpbase%\out.%target%\units\system.lib" "+%vpbase%\out.%target%\units\vpsyslow.lib" "+%vpbase%\out.%target%\units\dpmi32.lib" "+%vpbase%\out.%target%\units\dpmi32df.lib" >nul
if errorlevel 1 goto error
goto ok

:LibLnx
echo - Linking Linux-specific .obj files to new System unit
"%vpbase%\bin.%os%\lib" "%vpbase%\out.%target%\units\system.lib" "+%vpbase%\out.%target%\units\vpsyslow.lib" "+%vpbase%\out.%target%\units\linux.lib" "+%vpbase%\out.%target%\units\strings.lib" "+%vpbase%\out.%target%\units\lnxres.lib"
if errorlevel 1 goto error
goto ok

:error
echo.
echo   An error occurred in the build script.  Please check any messages
echo   above for the cause of the problem.
goto end

:ok
cd sys
echo - Copying new System unit to units.%target%
copy "%vpbase%\out.%target%\units\system.lib" "%vpbase%\units.%target%"
copy "%vpbase%\out.%target%\units\system.vpi" "%vpbase%\units.%target%"
echo.
echo Finished.

:end

