@echo off

if [%1]==[] goto usage
for %%i in ("%~1") do (
    set "FILENAME=%%~ni"
    set "EXT=%%~xi"
    set "DIR=%%~dpi"
)
call C:\kabellaengen\bin\setenv.bat

REM echo Dateiname ohne Erweiterung: %FILENAME%
REM echo Erweiterung: %EXT%
REM echo Verzeichnis: %DIR%
REM 
REM Namen der Zwischenergebnis Dateien
set JSON_POS=%FILENAME%_positionsconv.json
set JSON_TODRAW=%FILENAME%_todraw.json
REM Namen der Ergebnisdateien
set ERROR_DOUBLE=%FILENAME%_errors.json
set RESULT_TIA=%FILENAME%-*_TIA.xlsx

if exist "%~dp0_setenv.bat" (
    echo Lade lokale Umgebungseinstellungen aus _setenv.bat...
    call "%~dp0_setenv.bat"
)
REM Zielverzeichnis
set TARGET_DIR=%PROJECT_IO_RESULTS%\%FILENAME%
mkdir "%TARGET_DIR%"
REM lösche alte Fehlermeldungen
del "%PROJECT_WORK%\%ERROR_DOUBLE%"



echo.
echo === Fetching Positions ===
call getpositions.bat --filename %1 -s -r -w  %JSON_POS% -e %ERROR_DOUBLE%
if exist "%PROJECT_WORK%\%ERROR_DOUBLE%" (
    @echo == failed: errors, e.g. duplicate IDs in given layout 
    pause
    move %PROJECT_WORK%\%ERROR_DOUBLE% %TARGET_DIR%
    move %PROJECT_WORK%\%JSON_TODRAW%  %TARGET_DIR%

    goto :eof
)
if not exist "%PROJECT_WORK%\%JSON_POS%" (
    @echo == failed: getpositions 
    pause
    goto :eof
)
echo === Creating Excel Files for TIA, WSCAD, .. ===
call portalexport.bat --filename %JSON_POS% --outname %FILENAME%
if not exist "%PROJECT_WORK%\%RESULT_TIA%" (
    @echo == failed: creating .xlsx files 
    pause
    goto :eof
)
echo move %PROJECT_WORK%\%FILENAME%-* %TARGET_DIR%
move %PROJECT_WORK%\%FILENAME%-* %TARGET_DIR%
pause
goto :eof


:usage
@echo Usage: %0 ^<dxfinWorkOrdner.dxf^>
exit /B 1
goto :eof


