/*
** CCN_IBrowse.rexx
**
** Run IBrowse 2.x from an AREXX script
**
** Christophe Crébier (alternos@free.fr)
**
** Created on: February, 10th, 1999
** Last modif on: September, 10th, 1999
**
** $VER: CCN_IBrowse.rexx v1.990910 (10.September.1999)
**
** Syntax: RX CCN_IBrowse.rexx [option] , [config] , [URL]
**        options
**              = : don't call CCN_LecConfig.rexx, caller already did it.
**              ) : send 'QUIT' AREXX command to the browser.
**             )) : send 'QUIT FORCE'
**             NW : open a new window
**
**         config : name of the config
**
**            url : valid URL address
**
** Required: S:CCN.config
**           CCN_LecConfig.rexx
**           (rexxreqtools.library)
*/


OPTIONS RESULTS

/*
/* Library */
rexxlib = "rexxreqtools.library"
addrexxlib = 0
*/


/* IBrowse default values */
BPORT     = 'IBROWSE'
bpath     = 'IBrowse:IBrowse'
bprefs    = ''
bupath    = ''
newwindow = 0


/*
/* Open used Libs */
IF ~SHOW(LIBRARIES,rexxlib) THEN IF ~ADDLIB(rexxlib,0,-30,0) THEN EXIT 20
                 ELSE NOP
ELSE addrexxlib = 1
*/


/* Base name for rexx clip */
base = 'CCN_Config_'

CALL SETCLIP(base||'ibport',BPORT)
CALL SETCLIP(base||'ib path',bpath)


/* Read arguments : config , url */
PARSE ARG opt ',' config ',' url
opt    = STRIP(opt)
config = STRIP(config)
url    = STRIP(url)


/* Options */

/* Quit the browser */
IF LEFT(opt,1) = ')' THEN DO
    IF SHOW('P',BPORT) THEN DO
    ADDRESS (BPORT)
    IF ABBREV(opt,'))') THEN 'QUIT FORCE' ; ELSE 'QUIT'
    EXIT
    END
    ELSE DO
    SAY "ERROR: Can't quit a program who is not running!"
    EXIT 5
    END
END


/* Load user environment */
IF POS('=',opt) = 0 THEN DO
    IF config ~= '' THEN DO
    CALL CCN_LecConfig.rexx config
    IF RESULT ~= 'RESULT' THEN EXIT 10
    END
END


/* Open a new window option */
IF POS('NW',UPPER(opt)) ~= 0 THEN newwindow = 1


IF config ~= '' THEN DO
    bprefs = GETCLIP(base||'bprefs')
    bupath = GETCLIP(base||'bupath')

    lps = LASTPOS('/',bprefs)
    IF lps == 0 THEN lps = LASTPOS(':' , bprefs)
    IF lps ~= 0 THEN prefs = LEFT(bprefs , lps)
    ibprefs = 'CONFIGDIR='||prefs
END


/* Déjà réalisé par CCN_Oisans.rexx
/* Ask user for URL if he entered: URL=? */
IF UPPER(url) = 'URL=?' THEN DO
    ReqTitle   = "CCN"
    ReqText    = "Please, enter URL"
    ReqString  = "http://"
    url        = rtgetstring(ReqString,ReqText,ReqTitle,,,,)
END
*/


/* URL */
IF LEFT(UPPER(url),4) = 'URL=' THEN url = RIGHT(url, LENGTH(url) - 4)


/* Argument line */
argument = bpath || ' ' || ibprefs


/* IBrowse */
IF ~SHOW('P',BPORT) THEN DO
     ADDRESS COMMAND
     IF bupath ~= '' THEN 'Echo >t:browser Cd' bupath
     'Echo >>t:browser RUN >NIL:' argument
     'Execute t:browser'
     'Delete >NIL: t:browser'
     'SYS:rexxc/WaitForPort' BPORT
     ADDRESS
     IF RC>0 THEN DO
     SAY "IBrowse : Error !"
     EXIT 5
     END
END


/* Go to URL */
ADDRESS (BPORT)
IF newwindow = 1 THEN 'NEWWINDOW'
'GOTOURL ' url
'SCREENTOFRONT'


/* Removed unused lib */
IF addrexxlib ~= 1 THEN REMLIB(rexxlib)



/* End of the script */
EXIT 0
