@echo off
setlocal

::  Launcher for building tclMuPdf on Windows.
::   Using MS Visual Studio  (Visual Studio 2019 Community Edition)
::   for x64 target architecture.
::
::  Instructions for debug;
::   set the following enviroment variables:
::  set CFLAGS=/Zi /Od
::  set LFLAGS=/DEBUG


:: =======================================================================

 :: Edit this line if MS Visual Studio is installed in a different directory.
 :: 
SET _ctoolsSETUP=%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat

:: =======================================================================


set _THISCMD=%0
set _ARCH=x64
shift

echo =================================
echo Preliminary checks .....
echo =================================


:: prepare the C-compiler environment with the right _ARCH
if NOT EXIST "%_CTOOLSSETUP%" (
   :: NOTE: don't use parens in this echo block ...
  echo ERROR on setting the C build environment  ...
  echo Currently we expect MS Visual Studio [Community Edition] installed at
  echo   "%_CTOOLSSETUP%%"
  echo If installed at a different location, please adjust the _CTOOLSSETUP variable
  echo in this batch file.
  goto :EOF
)

call "%_CTOOLSSETUP%"  %_ARCH%
 :: checking if something went wrong is not easy, since the VisualStudio setup
 ::  doesn't return always an error code.
 :: Anyway, first check is on error code
 if errorlevel 1 (
  echo ERROR on setting the C build environment  ...
  echo Something went wrong ... please check if "%_ARCH%" is a valid parameter
  echo   for "%_CTOOLSSETUP%"
  goto :EOF
 )                     
 
 ::  other weak checks ...
 if NOT DEFINED VisualStudioVersion (
  echo ERROR on setting the C build environment  ...
  echo Expected a variable named VisualStudioVersion
  echo Something went wrong ... please check if "%_ARCH%" is a valid parameter
  echo   for "%_CTOOLSSETUP%"
  goto :EOF
  )
  :: .. ? other checks ? ...
  
  

echo =================================
echo Preliminary checks ..... DONE.
echo =================================
echo.
   
 :: you can also pass extra args to nmake  ...
nmake -f ms-makefile OS=win ARCH=%_ARCH% %1 %2 %3 %4 %5 %6 %7 %8 %9
