:: Copyright (c) 2000-2016 Autodesk, Inc.  All Rights Reserved. This
:: software is protected by United States and International Law, including
:: copyrights, trademarks, patents and pending patents.  Copying, duplication,
:: reproduction, or Distribution is strictly prohibited.

@ECHO OFF

set VNUM=2019
set ASCADIR=%~dp0
set ASCADIR=%ASCADIR:~0,-1%
set UNAME=heliusU
set UDLL=%UNAME%.dll

if "%1" == "" GOTO USAGE

set EXPORTS=-export:uexternaldb -export:umat -export:udmgini2 -export:udmgini3 -export:udmgevo2 -export:udmgevo3 -export:usermat -export:vumat -export:uel -export:disp -export:sdvini

set FTN_CMD=ifort -nologo -c -recursive -iface:cref -Qauto-scalar -Od -include:"%ASCADIR%\..\inc" -module:"%TEMP%"

set LINK_CMD=link -nologo %EXPORTS% -INCREMENTAL:NO -subsystem:console -LIBPATH:"%ASCADIR%" -NODEFAULTLIB:LIBC.LIB -NODEFAULTLIB:LIBCMT.LIB -DEFAULTLIB:OLDNAMES.LIB -DEFAULTLIB:LIBIFCOREMD.LIB -DEFAULTLIB:LIBIFPORTMD.LIB -DEFAULTLIB:LIBMMD.LIB -DEFAULTLIB:MSVCRT.LIB -DEFAULTLIB:kernel32.lib -DEFAULTLIB:user32.lib -DEFAULTLIB:netapi32.lib -DEFAULTLIB:advapi32.lib -FIXED:NO -dll -out:%UDLL% helius.lib heliusU_static.lib

set ASCAOBJ="%TEMP%\helius.obj"
set OBJS=%ASCAOBJ%

call %FTN_CMD% -Fo%ASCAOBJ% "%ASCADIR%\..\inc\helius.f90"

:COMPILE
    :: Check for more files
    if "%1" == "" GOTO LINK

    :: Get the file name and extension
    for /f "tokens=1,2 delims=." %%a in ("%1") do set FNAME=%%a & set EXT=%%b
    set FNAME=%FNAME:~0,-1%
    set THIS_OBJ="%TEMP%\%FNAME%.obj"
    set OBJS=%OBJS% %THIS_OBJ%

    call %FTN_CMD% -Fo%THIS_OBJ% %1

    :: Shift the arguments and return to the COMPILE label
    shift
    GOTO COMPILE

:LINK
    call %LINK_CMD% %OBJS%

    IF EXIST %UDLL%.manifest (
        mt -nologo -manifest %UDLL%.manifest -outputresource:%UDLL%;2
        del %UDLL%.manifest %UNAME%.lib %UNAME%.exp
    )

    GOTO END


:Localize
    set MSG_ADSK=Autodesk Helius PFA
    set MSG_FORTRAN_VERSIONS=(.f, .for, .f90, .f95)
    set MSG_FORTRAN_FILE_EXAMPLES=file1.f90 file2.f90
    set CURR_FILE=%~n0
    set RESOURCE_DIR=../../Build/win/translations/    
    
    ::Get Regional Settings/Format Language product language
    set KEY_NAME="HKEY_CURRENT_USER\Control Panel\International"
    set VALUE_NAME=LocaleName 
    set ADSKLocale=en-US

    FOR /F "usebackq skip=2 tokens=1,2*" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
        set ValueName=%%A
        set ValueType=%%B
        set ADSKLocale=%%C
    )

    set KEY_NAME="HKLM\SOFTWARE\Autodesk\Autodesk Simulation\Helius PFA\%VNUM%"
    set VALUE_NAME=InstallationLocation

    FOR /F "usebackq skip=2 tokens=1,2*" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (
        set ValueName=%%A
        set ValueType=%%B
        set RESOURCE_DIR=%%Cbin
    )

   ::requires 1252 to support German (and English)
   IF /I "%ADSKLocale%"=="de-DE" (
      CHCP 1252 >nul 2>&1
      IF ERRORLEVEL 1 (
         echo Could not find codepage for German, 1252. Defaulting to English.
         CALL "%RESOURCE_DIR%\en-US\hpfa_win.bat">nul           
      ) ELSE (
         CALL "%RESOURCE_DIR%\de-DE\hpfa_win.bat">nul
      )      
      GOTO END      
   )

   IF /I "%ADSKLocale%"=="ja-JP" (
      CHCP 932 >nul 2>&1
      IF ERRORLEVEL 1 (
        echo Could not find Japanese Codepage, 932. Defaulting to English.
        CALL "%RESOURCE_DIR%\en-US\hpfa_win.bat">nul
      ) ELSE (
        CALL "%RESOURCE_DIR%\ja-JP\hpfa_win.bat">nul
      )
      GOTO END
   )      
   
   IF /I "%ADSKLocale%"=="zh-CN" (
      CHCP 936 >nul 2>&1
      IF ERRORLEVEL 1 (
        echo Could not find Simplified Chinese Codepage, 936. Defaulting to English.
        CALL "%RESOURCE_DIR%\en-US\hpfa_win.bat">nul
      ) ELSE (
        CALL "%RESOURCE_DIR%\zh-CN\hpfa_win.bat">nul
      )
      GOTO END
   )            
   
   CALL "%RESOURCE_DIR%\en-US\hpfa_win.bat">nul
   
   GOTO END


:USAGE
    CALL :Localize
    echo:
    echo %MESSAGE_USAGE_5%
    echo %MESSAGE_USAGE_6%
    echo:
    echo %MESSAGE_USAGE_7%
    echo:
    echo %MESSAGE_USAGE_8%
    echo:
    echo %MESSAGE_USAGE_9%
    echo:

:END
