/* Skeleton.flex
 * $VER: Skeleton.flex 1.2 (2.6.96)
 *
 * This script shows how to use ARexx and GUI locking in your
 * own scripts. You ought to use Skeleton.flex or similar code
 * when writing your own scripts
 */

SIGNAL ON FAILURE
OPTIONS FAILAT 20

/* Initialization goes here
 * Retries   - how many retries
 * WaitDelay - how many ticks to wait between each retry
 *             50 ticks = 1 second
 */

Retries = 25   
WaitDelay = 50

ADDRESS 'REFLEX'
OPTIONS RESULTS

OwnerShip = 0

/*
 * rexxsupport.library needed for delay
 */

IF (~SHOW('L',"rexxsupport.library")) THEN DO
    IF (~ADDLIB("rexxsupport.library",0,-30,0)) THEN DO
        SAY '* Terminating; need rexxsupport.library'
        EXIT 10
    END
END

/*
 * Attempt to lock the Arexx interface
 * 'Retries' attempts are made; waiting 'WaitDelay'
 * ticks between each attempt
 */

DO WHILE ((OwnerShip ~= 1) & (Retries > 0))
    QUERYOWNERSHIP LOCK
    Res = RC
    IF (Res == 0) THEN
        OwnerShip = 1
    ELSE
        CALL DELAY(WaitDelay)
    Retries = Retries - 1
END

IF (OwnerShip == 1) THEN DO 
    LOCKGUI

    /* This is where you put your own
     * possibly critical code - the ARexx
     * port is for your use only
     */

    UNLOCKGUI  
    UNLOCKAREXX
END
EXIT 

/*
 * Handle errors
 */

Failure:
    IF (OwnerShip == 1) THEN DO
        UNLOCKGUI
        UNLOCKAREXX
    END
EXIT 10
