@echo off
rem change to correct working directory even if working directory is UNC path
pushd "%1"

set workdir=%1
set inputFile=%2
set Debug=%3

set scriptlog=jtv5.log
set VERBOSITY=Error
if %Debug% EQU 1 (
    set VERBOSITY=Trace
	echo %* >  %scriptlog%
	echo    >> %scriptlog%
	set     >> %scriptlog%	
)


rem -----------------------------------------------------
rem CATIA and COMFOX folders
rem -----------------------------------------------------
set CATIA_FOLDER=C:\Program Files\Dassault Systemes\B18
set COMFOX_BASE_DIR=C:\Program Files\T-SYSTEMS\comfox_6_1_2_ix64\B18
set COMFOX_MODULE_DIR=C:\Program Files\T-SYSTEMS\comfox_6_1_2_ix64

set PATH=%CATIA_FOLDER%\win_b64\code\bin;%PATH%
set PATH=%COMFOX_BASE_DIR%\win_b64\code\bin;%PATH%
set PATH=%COMFOX_BASE_DIR%\java;%PATH%

set TGS_LICENSE_FILE=%COMFOX_MODULE_DIR%\TGSLicence\password.dat

rem -----------------------------------------------------
rem Replace the default environment with your CATIA 
rem enviroment if this is required!
rem -----------------------------------------------------
set CATENV=CATIA_AND_COMFOX_ENV.txt
set CATENVDIR=%COMFOX_BASE_DIR%

for /f "tokens=*" %%a in (%inputFile%) do (
	CALL :HandleInPutLine %%a
)
GOTO :EOF



:HandleInputLine
set InFile=%1
set OutFile=%2

call :prepareInput InFileO %InFile%
call :prepareInput OutFileO %OutFile%
set OutFileNoSpace=%OutFileO: =_%
call :getFilenameFromPath LogFile "%OutFileNoSpace%"
call :prepareInput LogFile %workdir%\%LogFile%.log

if %Debug% EQU 1 (
	echo InFile=%InFile% OutFile=%OutFile%  LogFile=%LogFile% >> %scriptlog%
)
call catstart -env "%CATENV%" -direnv "%CATENVDIR%" -run "CXPcomfox.exe \"%COMFOX_MODULE_DIR%\config\OptionTemplates\OptionTemplate_Catia_Jt.opt\" -InFile=\"%InFileO%\" -OutFile=\"%OutFileO%\" -LogFile=\"%LogFile%\" -OutFormat=JT -Verbosity=%VERBOSITY% "
GOTO :EOF


:prepareInput
::prepareInput(retVal, inputString)
rem remove " and escape &
 SET string=%2%
 set string=%string:&=AMPERSAND%
 set string=%string:"=%
 set "%~1=%string%"
GOTO :EOF


:getFilenameFromPath
  SET "%1=%~nx2"
GOTO :EOF




