/*
 * Script to set up the environment for the Qt4 Development package
 */

/*
 * You do not normally need to change anything in this script
 *------------------------------------------------------------------------------
 */

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

parse arg aArgs

eol = '0D0A'x

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

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

/* Find out where the Qt4 development package is installed */

etc = value('ETC',, 'OS2ENVIRONMENT')
if (etc == '') then etc = value("ETC",, 'OS2ENVIRONMENT')
if (etc == '') then etc = SysBootDrive()

qt_conf = etc"\qtsys.conf"

basedir = stream(qt_conf, 'C', 'QUERY EXISTS')
if (basedir \== '') then
    basedir = filespec('D', basedir)||filespec('P',basedir)
else do
    say 'ERROR: Cannot find "'qt_conf'". Proabaly, the Qt4 Development package is not installed.'
    exit 1
end

look = 0
G.Prefix = ''
G.Binaries = ''
do while lines(qt_conf)
    v = linein(qt_conf)
    parse var v v'='val
    v = strip(v)
    val = translate(strip(strip(val), 'B', '"'), '\','/')
    if (v == '[Paths]') then look = 1
    else if (left(v, 1) == '[') then look = 0
    else if (look) then do
        select
            when (v == 'Prefix') then G.Prefix = val
            when (v == 'Binaries') then G.Binaries = val
            otherwise nop
        end
    end
end
call lineout qt_conf

curdir = directory()

call directory basedir
if (G.Prefix \== '') then
    G.Prefix = directory(G.Prefix)
else
    G.Prefix = basedir
G.Prefix = FixDir(G.Prefix)

if (G.Binaries \== '') then
    G.Binaries = directory(G.Binaries)
else
    G.Binaries = G.Prefix'\bin'
G.Binaries = FixDir(G.Binaries)

call directory curdir

/* Set up the environment */

'set QT_BASE='G.Prefix
'set QT_BIN='G.Binaries

'set PATH='G.Binaries';%PATH%'
'set BEGINLIBPATH='G.Binaries';%BEGINLIBPATH%'
'set QMAKESPEC=%QT_BASE%\mkspecs\default'

/* Add the path to the system DLLs to allow direct linking with DLLs without
 * creating import libraries */
bootDrv = SysBootDrive()
'set LIBRARY_PATH='bootDrv'\OS2\DLL;'bootDrv'\MPTN\DLL;%LIBRARY_PATH%'

/* Override the shell used by GNU make */
'set MAKESHELL='value('%COMSPEC%',,'OS2ENVIRONMENT')

'set QTENV_CMD_DONE=1'

'call c:\GCC\gcc452.cmd'

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 'Qt4 base directory (QT_BASE)  : 'G.Prefix
    say 'Qt4 binary directory (QT_BIN) : 'G.Binaries
    say
    say 'Qt4 Environment is set up. If you use the qmake tool, it already knows'
    say 'where to look for Qt parts. If you use your own makefiles, you may use the'
    say 'environment variables shown in parenthesis above to refer to the corresponding'
    say 'locations.'
    say
    say 'Please note that this script does *not* set up the compiler environment!'
    say 'You should do it yourself in order to be able to compile Qt programs.'
end

exit

FixDir: procedure
    parse arg aDir
    if (right(aDir, 1) == '\') then
        aDir = left(aDir, length(aDir) - 1)
    return aDir

OnError:
	exit

