/* $VER: SCMsg_AZur.raz 1.2 (6.9.95)
 * Inspiré de MiddleMan.rexx de Kenneth Yarnall distribué avec SAS C
 * Modifié par DBernard puis JM Forgeas
 */

OPTIONS RESULTS

IF SHOW('P','AZUR_SASC') THEN EXIT 0

/* ouvre la bibliothèque pour la gestion du port */
IF ~SHOW('L',"rexxsupport.library") THEN DO
    IF ~ADDLIB("rexxsupport.library",0,-30,0) THEN EXIT 10
END

ADDRESS COMMAND 'Run <>NIL: sc:c/scopts ERRORREXX'

CALL OpenAZur

/* Ouvre le fichier passé en argument (s'il y en a un)
 */

PARSE ARG fullname

IF (fullname ~= "") THEN CALL OpenFile fullname

/* Création du port de messages pour la communication avec SCMsg
 */

IF ~OPENPORT('AZUR_SASC') THEN EXIT 10

/* Sit back and wait for messages. */

Done = 0
DO UNTIL Done
    WAITPKT('AZUR_SASC')
    Empty = 0
    DO UNTIL Empty
        msg = GETPKT('AZUR_SASC')
        IF msg = '00000000'X THEN Empty = 1
        ELSE DO
            PARSE VALUE GETARG(msg) WITH command arg1
            REPLY(msg,0)
            command = UPPER(command)
            SELECT
                WHEN command = 'QUIT' THEN    /* Interprétation des différentes     */
                    Done = 1                  /* Commandes supportées par le script */
                WHEN command = 'OPEN' THEN DO /* ... Facile à étendre               */
                    /* Vérifie qu'AZur est toujours actif */
                    Call OpenAZur
                    Call OpenFile arg1
                END
                WHEN command = 'MOVE' THEN
                    CALL MoveToLine arg1
                OTHERWISE
                    NOP
            END
        END
    END
END

CLOSEPORT('AZUR_SASC')
ADDRESS COMMAND 'Run <>NIL: sc:c/scopts NOERRORREXX'
IF SHOW('P','SC_SCMSG') THEN ADDRESS SC_SCMSG 'Quit'
EXIT 0


/* Lance AZur s'il n'est pas actif */

OpenAZur:

    masterport = 'AZURMAST'
    IF OPEN(in,"ENV:AZurEditor/AZur_SAS_Master","R") ~= 0 THEN DO
        masterport = READLN(in)
        CALL CLOSE(in)
    END
    IF ~SHOW('P',masterport) THEN DO
        ADDRESS COMMAND
        'Run <>NIL: AZur:AZur NOPROC MPN' masterport  /* lance AZur. */
        'WaitForPort' masterport
        IF (RC ~= 0) THEN EXIT 10
    END

RETURN


/* Ouvre (ou active) une fenêtre AZur contenant le fichier passé en argument
 */

OpenFile: PROCEDURE

    PARSE ARG '"' fullname '"'

    /* Obtient la liste des ports de fenêtres AZur */
    masterport = 'AZURMAST'
    IF OPEN(in,"ENV:AZurEditor/AZur_SAS_Master","R") ~= 0 THEN DO
        masterport = READLN(in)
        CALL CLOSE(in)
    END
    ADDRESS VALUE masterport
    'GetPortList'; ports = RESULT
    port = ""

    DO WHILE (ports ~= '')
        PARSE VAR ports port ',' ports
        port = STRIP(port,,'"')
        ADDRESS VALUE port
        'GetFilePath'
        IF (RESULT ~= fullname) THEN port = ""; ELSE ports = ""
    END

    IF (port = "") THEN DO
        'File2NewWin NAME' fullname
        port = RESULT
    END

    /* Active la fenêtre */
    ADDRESS VALUE port
    'WinIconify OFF'
    'Win2Front'
    'ActivateWindow'

    /* Mémorise le port */
    SETCLIP('Actual_AZur_SASC_Port', port)

RETURN


/* Déplace le curseur de la fenêtre courante à la ligne donnée
 */

MoveToLine: PROCEDURE

    PARSE ARG lineno
    port = GETCLIP('Actual_AZur_SASC_Port')
    ADDRESS VALUE port
    'Cursor FOLDS LINE' lineno

RETURN
