IDENTIFICATION DIVISION.
PROGRAM-ID. COBCURSES-MENU-SD-MENU-MREFS.
*>
*>     THIS MODULE IS A DYNAMIC MENU LOAD MODULE FOR MENU REFERENCES
*>
*>     LS-REQUEST-TYPE :
*>         'X' - SET CONTEXT (SCREEN NAME)
*>         'O' - OPEN FILE (DOES NOT USE OTHER ARGS)
*>         'R' - READ NEXT (MENU ITEM)
*>         'C' - CLOSE
*>
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.

    SELECT MREF-FILE
        ASSIGN TO WS-MREF-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS MREF-KEY.

    SELECT MENU-FILE
        ASSIGN TO WS-MENU-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS MNU-KEY.

DATA DIVISION.
FILE SECTION.

    FD  MREF-FILE.
    01  MREF-RECORD.
        COPY MENUREF.

    FD  MENU-FILE.
    01  MENU-RECORD.
        COPY MENURECD.

WORKING-STORAGE SECTION.

    01  WS-SCREEN-NAME                      PIC X(16) VALUE SPACES.
    01  WS-FILE-NAME-LENGTH                 PIC 9999.
    01  WS-MREF-FILE-NAME                   PIC X(512)      VALUE "${COBCURSES_DATADIR}/MENUREFS.X".
    01  WS-MENU-FILE-NAME                   PIC X(512)      VALUE "${COBCURSES_DATADIR}/MENUS.X".

LINKAGE SECTION.

    01  LS-REQUEST-TYPE                     PIC X.          *> CALL REQUEST TYPE
    01  LS-ITEM-NAME                        PIC X(32).      *> RETURNED MENU ITEM "NAME"
    01  LS-ITEM-DESCRIPTION                 PIC X(64).      *> RETURNED MENU ITEM "DESCRIPTION"

PROCEDURE DIVISION
    USING LS-REQUEST-TYPE, LS-ITEM-NAME, LS-ITEM-DESCRIPTION.

*>
*>  THIS DISPATCHES ACCORDING TO LS-REQUEST-TYPE :
*>
    MOVE ZERO TO RETURN-CODE

    EVALUATE LS-REQUEST-TYPE
        WHEN 'X'
            MOVE LS-ITEM-NAME TO WS-SCREEN-NAME
        WHEN 'O'
            PERFORM 200-OPEN-FILE
        WHEN 'R'
            PERFORM 500-READ-FILE
        WHEN 'C'
            PERFORM 900-CLOSE-FILE
        WHEN OTHER
            MOVE 1 TO RETURN-CODE
    END-EVALUATE.
    GOBACK.

200-OPEN-FILE.
*>
*>  'O' - OPEN FILE REQUEST
*>
    MOVE LENGTH OF WS-MREF-FILE-NAME TO WS-FILE-NAME-LENGTH.
    CALL "COBCURSES-INIT-PATHNAME" USING WS-MREF-FILE-NAME, WS-FILE-NAME-LENGTH. 
    CALL "COBCURSES-INIT-PATHNAME" USING WS-MENU-FILE-NAME, WS-FILE-NAME-LENGTH. 

    OPEN INPUT MREF-FILE, MENU-FILE.
*>
*>     PREPARE FOR THE 'R' READ NEXT RECORD REQUEST :
*>
    INITIALIZE MREF-RECORD.
    MOVE WS-SCREEN-NAME TO MREF-SCREEN-NAME.
    START MREF-FILE KEY IS >= MREF-KEY
        INVALID KEY
            PERFORM 900-CLOSE-FILE
            MOVE 1 TO RETURN-CODE
    END-START.
    EXIT.

500-READ-FILE.
*>
*>  'R' - READ NEXT RECORD
*>
    READ MREF-FILE NEXT RECORD
        AT END
            PERFORM 510-END-FILE
        NOT AT END
            IF MREF-SCREEN-NAME = WS-SCREEN-NAME THEN
                MOVE MREF-MENU-NAME TO LS-ITEM-NAME
                PERFORM 510-LOOKUP-MENU
            ELSE
                PERFORM 510-END-FILE
            END-IF
    END-READ
    EXIT.

510-LOOKUP-MENU.
    INITIALIZE MENU-RECORD.
    MOVE MREF-MENU-NAME TO MNU-MENU-NAME.
    READ MENU-FILE
        INVALID KEY
            INITIALIZE LS-ITEM-DESCRIPTION
        NOT INVALID KEY
            MOVE MNU-TITLE TO LS-ITEM-DESCRIPTION
    END-READ.
    EXIT.

510-END-FILE.
    MOVE 1 TO RETURN-CODE
    INITIALIZE LS-ITEM-NAME, LS-ITEM-DESCRIPTION
    EXIT.

900-CLOSE-FILE.
*>
*>     'C' - CLOSE FILE
*>
    CLOSE MREF-FILE, MENU-FILE.
    EXIT.

END PROGRAM COBCURSES-MENU-SD-MENU-MREFS.
