:: Copyright (c) 2000-2017 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

SETLOCAL ENABLEDELAYEDEXPANSION

SET CPU=winx64
SET HMCTBIN=%~dp0
SET OP=%PATH%
SET VALIDANSYS=180 181 182 190

SET FILE_EXT=%~x2
if "%1" == "" GOTO ERROR
if "%2" == "" GOTO ERROR
if "%1" == "-?" GOTO ERROR
if NOT "%FILE_EXT%" == ".cdb" if NOT "%FILE_EXT%" == ".ans" if NOT "%FILE_EXT%" == ".inp" if NOT "%FILE_EXT%" == ".dat" GOTO ERROR

SET ANSV=%1
SET JOB=%~n2

SET "ANSENVNAME=ANSYS%1_DIR"
SET ANSDIR=!%ANSENVNAME%!

SET ANSOKAY=0
for %%i in (%VALIDANSYS%) do (
    if %%i == %ANSV% (
        SET ANSOKAY=1
    )
)

if "%ANSOKAY%" == "0" GOTO ERROR

:ANSYS
SET PATH=%ANSDIR%\bin\%CPU%;%HMCTBIN%\ansys;%PATH%
SET ANS_USER_PATH=%HMCTBIN%\ansys
ansys%ANSV% -b -i "%JOB%%FILE_EXT%" -o "%JOB%.out" -j "%JOB%" -np 1

GOTO END

:ERROR
CALL :Localize

echo %MESSAGE_USAGE_1%
echo:
echo %MESSAGE_USAGE_2%
echo:
echo %MESSAGE_USAGE_3%
echo %MESSAGE_USAGE_4%
echo:

GOTO END

:Localize
set MSG_ANSYS=ANSYS
set MSG_ADSK=Autodesk Helius PFA
set MSG_VERSIONS=%VALIDANSYS%
set MSG_BATCHNAME=ansys-helius
set MSG_EXTENSION=.ans, .inp, .dat, or .cdb
set RESOURCE_DIR=../../../translations/
    
IF NOT EXIST %RESOURCE_DIR% (
   set RESOURCE_DIR=
)

::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\2019"
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

:LAUNCHER
start "" "%~dp0ansys-helius-launcher"

:END
ENDLOCAL
