@echo off
cls
:: Startup Batch file for PTC/USER WF5 HOW
:: 1.0   02-Jun-2009   tmcnaney@ptc.com   Created from WF4-HOW files

set VERSION=1.0
title %~nx0 (v%VERSION%)

:: Initialize environment with extended features
setlocal enableextensions enabledelayedexpansion

set PROE_DIR=
set PROE_CMD=
set PROE_VER=Pro/ENGINEER Wildfire 5.0

:: set DBG=echo to print commands instead of executing
set DBG=
set ERROR_COLOR=0E

:: capture script pathname and filename
for %%a in ("%~f0") do (
   set CMDPATH=%%~dpa
   set CMDNAME=%%~na
   set CMDEXT=%%~xa
   set CMDINI=!CMDPATH!!CMDNAME!.ini
)

:: create INI template if not found
if not exist "%CMDINI%" call :EDIT_INI

:: process command line
if [%1]==[] (
   :: use default startup trail file
   set ARGS=startwf5.txt   
   goto CONTINUE
)

:: bypass error checking, and just pass entire cmdline
set ARGS=%*

:CONTINUE

:READ_INI
:: REQUIRED - Pro/E installation folder
:: OPTIONAL - startup command, version string
for /F "usebackq tokens=1,2 delims==" %%a in ("%CMDINI%") do (
   if %%a==PROE_DIR set PROE_DIR=%%b
   if %%a==PROE_CMD set PROE_CMD=%%b
   if %%a==PROE_VER set PROE_VER=%%b
)

if not defined PROE_DIR (
   call :INI_ERROR "PROE_DIR not defined."
   goto READ_INI
)

if not exist "%PROE_DIR%\bin" (
   call :INI_ERROR "%PROE_VER% installation not found in [%PROE_DIR%]"
   goto READ_INI
)

if not defined PROE_CMD set PROE_CMD=%PROE_DIR%\bin\proe.exe
if not exist "%PROE_DIR%\bin\%PROE_CMD%" (
   call :INI_ERROR "%PROE_CMD% not found in [%PROE_DIR%\bin]"
   goto READ_INI
)

:: set working root to folder containing this script
set PTC_WORK_ROOT=%~dp0
set PTC_START_DIR=%PTC_WORK_ROOT%Startup

set PTC_STDS=%PTC_WORK_ROOT%Standards

:: Keep settings and cache isolated
set PTC_WF_ROOT=%PTC_WORK_ROOT%Settings
set PTC_WF_CACHE=%PTC_WORK_ROOT%Cache

:: create folders if they don't exist
if not exist "%PTC_START_DIR%" mkdir "%PTC_START_DIR%"
if not exist "%PTC_WF_ROOT%" mkdir "%PTC_WF_ROOT%"
if not exist "%PTC_WF_CACHE%" mkdir "%PTC_WF_CACHE%"

:: ModelCHECK configuration - set to path containing config folder
set MCDIR=%PTC_STDS%\modchk\usascii

:: set any special environment variables needed
:: compatibility to run WF4 Whats New tutorial in WF5
set PTC_WORK_ROOT_WF4=%PTC_WORK_ROOT%\..\..\WF4_HOW\Student

:: change startup dir to folder containing config.pro and config.win
pushd "%PTC_WORK_ROOT%Startup"

echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
echo.
echo Pro/E Loadpoint   =  %PROE_DIR%
echo Pro/E Command     =  %PROE_CMD% %ARGS%
echo.
echo Startup Directory =  %CD%
echo.
echo Wildfire Settings =  %PTC_WF_ROOT%
echo Wildfire WS Cache =  %PTC_WF_CACHE%
echo.
echo PTC Standards     =  %PTC_STDS%
echo ModelCHECK config =  %MCDIR%
echo.
echo :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
echo.

:: set path for Pro/E and Mechanica
set PATH=%PROE_DIR%\bin;%PROE_DIR%\mech\bin;%PATH%

echo Starting %PROE_VER%...
echo.
:: trail file will be used to set options not available in config, and
:: Pro/E working dir for exercises
%DBG% "%PROE_DIR%\bin\%PROE_CMD%" %ARGS%

:: endlocal
goto :EOF

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutines
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:EDIT_INI
 > %CMDINI% echo :: Configuration variables for %CMDNAME%%CMDEXT%
>> %CMDINI% echo.
>> %CMDINI% echo [REQUIRED]
>> %CMDINI% echo :: full pathname to Pro/E installation  
>> %CMDINI% echo PROE_DIR=
>> %CMDINI% echo.
>> %CMDINI% echo [OPTIONAL]
>> %CMDINI% echo :: specify .bat to bypass proe.exe chooser
>> %CMDINI% echo PROE_CMD=proe.exe
>> %CMDINI% echo.
>> %CMDINI% echo :: String used in messages
>> %CMDINI% echo PROE_VER=%PROE_VER%
:: launch in editor
color %ERROR_COLOR%
echo.
echo %CMDINI% not found - use Notepad to update values, then Save ^& Exit.
echo.
start /wait notepad %CMDINI%
color
goto :EOF

:INI_ERROR
color %ERROR_COLOR%
echo.
echo %~1
echo.
echo Please update in editor, then Save ^& Exit.
echo.
start /wait notepad %CMDINI%
color
cls
goto :EOF
