/*
 * Script to set up the environment for the GCC 4.x.x compiler
 */

/* detect script file and directory */
parse source . . G.ScriptFile
G.ScriptDir = filespec('D', G.ScriptFile) || filespec('P', G.ScriptFile)

/**
 * This variable contans the full path to the GCC installation tree. If you
 * move this script file outside the original location, update this variable
 * to meet your GCC installation path.
 */
G.GCC_PATH      = G.ScriptDir

/**
 * This defines the linker and should normally be 'WLINK'. Don not change this
 * unless you know waht you are doing (all IBM linkers supported by GCC have
 * serious plroblems linking DLLs with many thousands of exported symbols which
 * is a typical case for DLLs containing C++ classes and templates).
 */
G.GCC_LINKER    = 'WLINK'

/*
 * You do not normally need to change anything else below this line
 *------------------------------------------------------------------------------
 */

'@echo off'
trace off
signal on error name OnError

parse arg aArgs

rc = rxFuncAdd('SysLoadFuncs', 'REXXUTIL', 'SysLoadFuncs')
rc = SysLoadFuncs()

if (value('GCC442_CMD_DONE',,'OS2ENVIRONMENT') == 1) then do
    say
    say 'GCC 4 Environment is already set up.'
    exit 0
end

/* setup the GCC environment */
cmdline = 'call' G.GCC_PATH'\usr\local442\bin\gccenv.cmd' G.GCC_PATH'\usr' G.GCC_LINKER
cmdline
drop cmdline

'set GCC442_CMD_DONE=1'

if (aArgs == '@') then nop /* Silence... */
else if (aArgs \== '') then do
    /* Start the program */
    prg = translate(word(aArgs, 1))
    isCmd = 0
    realPrg = SysSearchPath(PATH, prg)
    if (realPrg == '') then realPrg = SysSearchPath(PATH, prg'.EXE')
    if (realPrg == '') then realPrg = SysSearchPath(PATH, prg'.COM')
    if (realPrg == '') then realPrg = SysSearchPath(PATH, prg'.CMD')
    if (realPrg \== '') then do
        if (right(realPrg, 4) == '.CMD') then isCmd = 1
    end
    if (isCmd) then 'call' aArgs
    else aArgs
end
else do
    /* Print some information about the environment */
    say
    say 'GCC 4 Environment is set up.'
end
exit

OnError:
    if (rc \= 0 & symbol('cmdline') == 'VAR') then do
        say 'ERROR: Executing "'cmdline'" failed with code' rc'.'
        exit rc
    end
	exit
