IDENTIFICATION DIVISION.
PROGRAM-ID. SDLOAD.
*>
*> THIS PROGRAM LOADS FROM A BACKUP TEXT FILE INTO
*> THE CORRESPONDING INDEXED FILE, ACCORDING TO THE
*> RECORD TYPE. 
*>
*> THE FILE BEING LOADED IS NORMALLY CREATED BY
*> SDBACKUP.
*>
*> NOTE THAT RECORDS ARE UPDATED/ADDED BY THIS
*> PROGRAM. THE INDEXED FILES ARE **NOT** CLEARED.
*>
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.

    SELECT BKU-FILE
        ASSIGN TO BK-LOAD-FILE-NAME
        ORGANIZATION IS LINE SEQUENTIAL.

    SELECT SC-FILE
        ASSIGN TO SC-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS SCN-NAME.
    
    SELECT FD-FILE
        ASSIGN TO FD-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS SCR-FDEF-KEY.
    
    SELECT BG-FILE
        ASSIGN TO BG-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS SCRBG-KEY.
    
    SELECT FS-FILE
        ASSIGN TO FS-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS SCR-FST-KEY.
    
    SELECT CS-FILE
        ASSIGN TO CS-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS CHARSET-NAME.
    
    SELECT MU-FILE
        ASSIGN TO MU-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS MNU-KEY.
    
    SELECT IT-FILE
        ASSIGN TO IT-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS ITM-KEY.

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

DATA DIVISION.
FILE SECTION.

    FD  BKU-FILE.
    01  BKU-RECORD.
        COPY BKU-RECD.

    FD  SC-FILE.
    01  SC-RECORD.
        COPY SCREEN-01.

    FD  FD-FILE.
    01  FD-RECORD.
        COPY SCREEN-FD.

    FD  BG-FILE.
    01  BG-RECORD.
        COPY SCREEN-BG.

    FD  FS-FILE.
    01  FS-RECORD.
        COPY SCREEN-FS.

    FD  CS-FILE.
    01  CS-RECORD.
        COPY SCREEN-CS.

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

    FD  IT-FILE.
    01  IT-RECORD.
        COPY ITEMRECD.

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

WORKING-STORAGE SECTION.

    01  WS-PATHNAMES.
        10  FILE-NAME-LENGTH            PIC 9999.       *> LENGTHS FOR ALL PATHNAMES BELOW :
        10  SC-FILE-NAME                PIC X(512)      VALUE "${COBCURSES_DATADIR}/SCREENS.X".
        10  FD-FILE-NAME                PIC X(512)      VALUE "${COBCURSES_DATADIR}/SCRFDEF.X".
        10  BG-FILE-NAME                PIC X(512)      VALUE "${COBCURSES_DATADIR}/SCRNBG.X".
        10  FS-FILE-NAME                PIC X(512)      VALUE "${COBCURSES_DATADIR}/SCRFSTA.X".
        10  CS-FILE-NAME                PIC X(512)      VALUE "${COBCURSES_DATADIR}/SCRCHRSET.X".
        10  MU-FILE-NAME                PIC X(512)      VALUE "${COBCURSES_DATADIR}/MENUS.X".
        10  IT-FILE-NAME                PIC X(512)      VALUE "${COBCURSES_DATADIR}/ITEMS.X".
        10  MR-FILE-NAME                PIC X(512)      VALUE "${COBCURSES_DATADIR}/MENUREFS.X".
        10  BK-LOAD-FILE-NAME           PIC X(512)      VALUE "backup.bak".

    01  WS-FLAGS.
        10  WS-EOF-FLAG                 PIC X.          *> END-OF-FILE FLAG
            88  WS-HAVE-INPUT           VALUE "N".      *> NOT AT END-OF-FILE
            88  WS-EOF                  VALUE "Y".      *> AT END-OF-FILE
        10  WS-FLAG                     PIC X.

    01  WS-COUNTERS.
        10  WS-BKU-RECDS                PIC 9(9) VALUE 0.   *> INPUT BACKUP RECD COUNT
        10  WS-SC-RECDS                 PIC 9(9) VALUE 0.   *> SCREEN DEFINITIONS
        10  WS-FD-RECDS                 PIC 9(9) VALUE 0.   *> FIELD DEFINITIONS
        10  WS-BG-RECDS                 PIC 9(9) VALUE 0.   *> BACKGROUND SEGMENTS
        10  WS-FS-RECDS                 PIC 9(9) VALUE 0.   *> FIELD STATES
        10  WS-CS-RECDS                 PIC 9(9) VALUE 0.   *> CHARSETS
        10  WS-MU-RECDS                 PIC 9(9) VALUE 0.   *> MENUS
        10  WS-IT-RECDS                 PIC 9(9) VALUE 0.   *> MENU ITEMS
        10  WS-MR-RECDS                 PIC 9(9) VALUE 0.   *> MENU REFS
        10  WS-ERROR-COUNT              PIC 9(9) VALUE 0.   *> ERROR COUNT
        10  WS-BEFORE-COUNT             PIC 9(9).           *> SAVED ERROR COUNT BEFORE RECORD IS VALIDATED
        10  WS-TOTAL-COUNT              PIC 9(9) VALUE 0.   *> TOTAL RECORD COUNT
        10  WS-SKIP-LOAD-FLAG           PIC X.              *> RECORD SKIP FLAG
            88  LOAD-THIS-RECD          VALUE 'N'.
            88  SKIP-THIS-RECD          VALUE 'Y'.

    01  WS-LEN-HEX-SEGMENT              PIC 9999 COMP-5.    *> LENGTH OF THE HEXADECIMAL TEXT
    01  WS-LEN-SCRBG-SEGMENT            PIC 9999 COMP-5.    *> BYTE LENGTH OF THE SCREEN SEGMENT

    01  WS-MESSAGE.
        10  FILLER                      PIC X(9)        VALUE "*** LINE ".
        10  WS-MSG-LINE-NO              PIC 9(9).
        10  FILLER                      PIC X           VALUE SPACE.
        10  WS-MSG-TEXT                 PIC X(50).

PROCEDURE DIVISION.

MAIN-PROGRAM.
    PERFORM 1000-INITIALIZE.
    PERFORM 5000-PROCESS.
    PERFORM 9000-FINALIZE.
    STOP RUN.

1000-INITIALIZE.
    PERFORM 1010-EDIT-PATHNAMES.                        *> SUBSTITUTE FOR ENVIRONMENT VARIABLES IN FILE NAMES
*>
*>  IF ENVIRONMENT VARIABLE EXISTS, USE THAT FOR THE INPUT FILE NAME :
*>
    ACCEPT BK-LOAD-FILE-NAME FROM ENVIRONMENT "COBCURSES_SDLOAD_FILE".
    IF BK-LOAD-FILE-NAME = SPACES
        MOVE "backup.txt" TO BK-LOAD-FILE-NAME          *> DEFAULT TO backup.txt FOR THE INPUT FILE NAME
    END-IF.
    PERFORM 9100-OPEN-FILES.
    EXIT.

1010-EDIT-PATHNAMES.
    MOVE LENGTH OF SC-FILE-NAME TO FILE-NAME-LENGTH.
    CALL "COBCURSES-INIT-PATHNAME" USING SC-FILE-NAME, FILE-NAME-LENGTH.
    CALL "COBCURSES-INIT-PATHNAME" USING FD-FILE-NAME, FILE-NAME-LENGTH.
    CALL "COBCURSES-INIT-PATHNAME" USING BG-FILE-NAME, FILE-NAME-LENGTH.
    CALL "COBCURSES-INIT-PATHNAME" USING FS-FILE-NAME, FILE-NAME-LENGTH.
    CALL "COBCURSES-INIT-PATHNAME" USING CS-FILE-NAME, FILE-NAME-LENGTH.
    CALL "COBCURSES-INIT-PATHNAME" USING MU-FILE-NAME, FILE-NAME-LENGTH.
    CALL "COBCURSES-INIT-PATHNAME" USING IT-FILE-NAME, FILE-NAME-LENGTH.
    CALL "COBCURSES-INIT-PATHNAME" USING MR-FILE-NAME, FILE-NAME-LENGTH.
    EXIT.

5000-PROCESS.
    PERFORM 7000-READ-BACKUP                            *> READ INPUT BACKUP TEXT FILE
    PERFORM UNTIL WS-EOF                                *> UNTIL END-OF-FILE
        PERFORM 6010-DISPATCH-RECD                      *> LOAD ACCORDING TO RECORD TYPE
        PERFORM 7000-READ-BACKUP                        *> READ NEXT RECORD
    END-PERFORM.
    EXIT.

6010-DISPATCH-RECD.
    IF BKU-FILE-TYPE NOT = "SD" THEN                    *> MAKE SURE THIS IS A SDBACKUP FILE
        MOVE "INCORRECT FILE TYPE" TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
        SET WS-EOF TO TRUE                              *> FAKE AN EOF
        EXIT                                            *> GO NO FURTHER
    END-IF.

    IF BKU-RECD-VERSION NOT NUMERIC                     *> VERSION NUMBER NUMERIC?
        MOVE "INCORRECT RECORD VERSION" TO WS-MSG-TEXT  *> VERY BAD..
        PERFORM 9900-ERROR-MESSAGE
    ELSE
        EVALUATE BKU-RECD-VERSION                       *> CHECK FOR VALID VERSION NUMBERS
            WHEN 01
                CONTINUE
            WHEN 02
                CONTINUE
            WHEN 03
                CONTINUE
            WHEN OTHER
                MOVE "INCORRECT RECORD VERSION" TO WS-MSG-TEXT
                PERFORM 9900-ERROR-MESSAGE              *> CAN'T PROCESS THIS UNKNOWN VERSION
                EXIT
        END-EVALUATE
    END-IF.

    IF BKU-RECD-LENGTH IS NOT NUMERIC OR BKU-RECD-LENGTH <= 10 THEN
        MOVE "INCORRECT RECORD LENGTH" TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE                      *> BAD LENGTH
    END-IF.

    EVALUATE BKU-RECD-TYPE                              *> CHECK RECORD TYPE
        WHEN "SC"
            PERFORM 7010-WRITE-SC
        WHEN "FD"
            PERFORM 7020-WRITE-FD
        WHEN "BG"
            PERFORM 7030-WRITE-BG
        WHEN "FS"
            PERFORM 7040-WRITE-FS
        WHEN "CS"
            PERFORM 7050-WRITE-CS
        WHEN "MU"
            PERFORM 7060-WRITE-MU
        WHEN "IT"
            PERFORM 7070-WRITE-IT
        WHEN "MR"
            PERFORM 7080-WRITE-MR
        WHEN OTHER
            MOVE "INVALID RECORD TYPE" TO WS-MSG-TEXT
            PERFORM 9900-ERROR-MESSAGE
    END-EVALUATE.
    EXIT.

7000-READ-BACKUP.
    READ BKU-FILE
        AT END
            SET WS-EOF TO TRUE                          *> END-OF-FILE
        NOT AT END
            ADD 1 TO WS-BKU-RECDS                       *> COUNT RECORDS READ
            SET LOAD-THIS-RECD TO TRUE                  *> MARK THIS AS LOADABLE (AS FAR AS WE KNOW)
            MOVE WS-ERROR-COUNT TO WS-BEFORE-COUNT      *> SAVE ERROR COUNT
    END-READ.
    EXIT.

7010-WRITE-SC.
    PERFORM 8000-VALIDATE-SC.                           *> VALIDATE THE SCREEN DEFINITION CONTENT
    IF NOT SKIP-THIS-RECD THEN
        PERFORM 8000-POPULATE-SC                        *> PREP SCREEN RECD
        WRITE SC-RECORD
            INVALID KEY
                PERFORM 9910-DUP-KEY
            NOT INVALID KEY
                ADD 1 TO WS-SC-RECDS
                ADD 1 TO WS-TOTAL-COUNT
        END-WRITE
    END-IF.
    EXIT.

7020-WRITE-FD.
    PERFORM 8010-VALIDATE-FD.                           *> VALIDATE THE FIELD DEFN CONTENT
    IF NOT SKIP-THIS-RECD THEN
        PERFORM 8010-POPULATE-FD                        *> PREP THE FIELD DEFN RECD
        WRITE FD-RECORD
            INVALID KEY
                PERFORM 9910-DUP-KEY
            NOT INVALID KEY
                ADD 1 TO WS-FD-RECDS
                ADD 1 TO WS-TOTAL-COUNT
        END-WRITE
    END-IF.
    EXIT.

7030-WRITE-BG.
    PERFORM 8020-VALIDATE-BG                            *> SCREEN BACKGROUND SEGMENT VALIDATION
    IF NOT SKIP-THIS-RECD THEN
        PERFORM 8020-POPULATE-BG
        WRITE BG-RECORD
            INVALID KEY
                PERFORM 9910-DUP-KEY
            NOT INVALID KEY
                ADD 1 TO WS-BG-RECDS
                ADD 1 TO WS-TOTAL-COUNT
        END-WRITE
    END-IF.
    EXIT.

7040-WRITE-FS.
    PERFORM 8030-VALIDATE-FS.                           *> FIELD STATE VALIDATION
    IF NOT SKIP-THIS-RECD THEN
        PERFORM 8030-POPULATE-FS
        WRITE FS-RECORD
            INVALID KEY
                PERFORM 9910-DUP-KEY
            NOT INVALID KEY
                ADD 1 TO WS-FS-RECDS
                ADD 1 TO WS-TOTAL-COUNT
        END-WRITE
    END-IF.
    EXIT.

7050-WRITE-CS.
    PERFORM 8040-POPULATE-CS.                           *> CHARSET VALIDATION
    WRITE CS-RECORD
        INVALID KEY
            PERFORM 9910-DUP-KEY
        NOT INVALID KEY
            ADD 1 TO WS-CS-RECDS
            ADD 1 TO WS-TOTAL-COUNT
    END-WRITE.
    EXIT.

7060-WRITE-MU.
    PERFORM 8050-POPULATE-MU.                           *> MENU VALIDATION
    WRITE MU-RECORD
        INVALID KEY
            PERFORM 9910-DUP-KEY
        NOT INVALID KEY
            ADD 1 TO WS-MU-RECDS
            ADD 1 TO WS-TOTAL-COUNT
    END-WRITE.
    EXIT.

7070-WRITE-IT.
    PERFORM 8060-POPULATE-IT.                           *> MENU ITEM VALIDATION
    WRITE IT-RECORD
        INVALID KEY
            PERFORM 9910-DUP-KEY
        NOT INVALID KEY
            ADD 1 TO WS-IT-RECDS
            ADD 1 TO WS-TOTAL-COUNT
    END-WRITE.
    EXIT.

7080-WRITE-MR.
    PERFORM 8070-POPULATE-MR.                           *> MENU REFS
    WRITE MR-RECORD
        INVALID KEY
            PERFORM 9910-DUP-KEY
        NOT INVALID KEY
            ADD 1 TO WS-MR-RECDS
            ADD 1 TO WS-TOTAL-COUNT
    END-WRITE.
    EXIT.

7800-CHECK-VALIDATION.
    IF WS-ERROR-COUNT NOT = WS-BEFORE-COUNT THEN        *> DID THE ERROR COUNT INCREASE?
        SET SKIP-THIS-RECD TO TRUE                      *> YES, SO WE DON'T LOAD THIS RECORD
    END-IF.
    EXIT.

7900-FLAG-CHECK.
*>
*>  CHECK FOR LEGAL FLAG VALUES
*>
    IF WS-FLAG NOT = "Y" AND WS-FLAG NOT = "N" AND WS-FLAG NOT = SPACE THEN
        PERFORM 9900-ERROR-MESSAGE
    END-IF.            
    EXIT.

8000-VALIDATE-SC.
    MOVE BKU-SC-SHOW-DATE                       TO WS-FLAG.
    MOVE "BKU-SC-SHOW-DATE"                     TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    MOVE BKU-SC-SHOW-TIME                       TO WS-FLAG.
    MOVE "BKU-SC-SHOW-TIME"                     TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    MOVE BKU-SC-ACTION-REQUIRED                 TO WS-FLAG.
    MOVE "BKU-SC-ACTION-REQUIRED"               TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    IF BKU-SC-COLUMNS-MIN IS NOT NUMERIC THEN
        MOVE "BKU-SC-COLUMNS-MIN"               TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-SC-LINES-MIN IS NOT NUMERIC THEN
        MOVE "BKU-SC-LINES-MIN"                 TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-SC-PAIRS IS NOT NUMERIC THEN
        MOVE "BKU-SC-PAIRS"                     TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    PERFORM 7800-CHECK-VALIDATION.
    EXIT.

8000-POPULATE-SC.
    MOVE BKU-SC-NAME                            TO SCN-NAME.
    MOVE BKU-SC-DESCRIPTION                     TO SCN-DESCRIPTION.
    MOVE BKU-SC-AUTHOR                          TO SCN-AUTHOR.
    MOVE BKU-SC-NOTES                           TO SCN-NOTES.
    MOVE BKU-SC-TITLE                           TO SCN-TITLE.
    MOVE BKU-SC-SHOW-DATE                       TO SCN-SHOW-DATE.
    MOVE BKU-SC-SHOW-TIME                       TO SCN-SHOW-TIME.
    MOVE BKU-SC-ACTION-REQUIRED                 TO SCN-ACTION-REQUIRED.
    MOVE BKU-SC-COLUMNS-MIN                     TO SCN-COLUMNS-MIN.
    MOVE BKU-SC-LINES-MIN                       TO SCN-LINES-MIN.
    MOVE BKU-SC-PAIRS                           TO SCN-PAIRS.
    MOVE BKU-SC-WS-SECTION                      TO SCN-WS-SECTION.
    MOVE BKU-SC-LINKAGE-SECTION                 TO SCN-LINKAGE-SECTION.
    MOVE BKU-SC-PROCEDURE-DIVISION              TO SCN-PROCEDURE-DIVISION.
    IF BKU-RECD-VERSION >= 03 THEN
        MOVE BKU-SC-STRIP-CHARACTER             TO SCN-STRIP-CHARACTER      *> USUALLY '_' IS THE STRIP CHARACTER
    ELSE
        MOVE SPACE                              TO SCN-STRIP-CHARACTER      *> BLANK LEAVES SEGMENTS ASIS
    END-IF.
    EXIT.

8010-VALIDATE-FD.
    IF BKU-FD-FDEF-NO IS NOT NUMERIC
        MOVE "BKU-FD-FDEF-NO"                   TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-FD-LINE IS NOT NUMERIC
        MOVE "BKU-FD-LINE"                      TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-FD-COLUMN IS NOT NUMERIC
        MOVE "BKU-FD-COLUMN"                    TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-FD-BUFFER-LENGTH IS NOT NUMERIC
        MOVE "BKU-FD-BUFFER-LENGTH"             TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-FD-WINDOW-LENGTH IS NOT NUMERIC
        MOVE "BKU-FD-WINDOW-LENGTH"             TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-FD-DIGITS IS NOT NUMERIC
        MOVE "BKU-FD-DIGITS"                    TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-FD-DECIMALS IS NOT NUMERIC
        MOVE "BKU-FD-DECIMALS"                  TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    MOVE BKU-FD-CLEAR                           TO WS-FLAG.
    MOVE "BKU-FD-DECIMALS"                      TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    MOVE BKU-FD-UPPERCASE                       TO WS-FLAG.
    MOVE "BKU-FD-DECIMALS"                      TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    MOVE BKU-FD-PASSWORD                        TO WS-FLAG.
    MOVE "BKU-FD-DECIMALS"                      TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    MOVE BKU-FD-NOT-BLANK                       TO WS-FLAG.
    MOVE "BKU-FD-DECIMALS"                      TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    MOVE BKU-FD-YN                              TO WS-FLAG.
    MOVE "BKU-FD-DECIMALS"                      TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    MOVE BKU-FD-SIGNED                          TO WS-FLAG.
    MOVE "BKU-FD-DECIMALS"                      TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    MOVE BKU-FD-VERIFY                          TO WS-FLAG.
    MOVE "BKU-FD-DECIMALS"                      TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    MOVE BKU-FD-VISIBLE                         TO WS-FLAG.
    MOVE "BKU-FD-DECIMALS"                      TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    MOVE BKU-FD-IGNORE-CHANGES                  TO WS-FLAG.
    MOVE "BKU-FD-DECIMALS"                      TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    MOVE BKU-FD-INPUT-SEQ                       TO WS-FLAG.
    MOVE "BKU-FD-DECIMALS"                      TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    MOVE BKU-FD-ACTION                          TO WS-FLAG.
    MOVE "BKU-FD-DECIMALS"                      TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    MOVE BKU-FD-READ-ONLY                       TO WS-FLAG.
    MOVE "BKU-FD-DECIMALS"                      TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    PERFORM 7800-CHECK-VALIDATION.
    EXIT.

8010-POPULATE-FD.
    MOVE BKU-FD-SCREEN-NAME                     TO SCR-FDEF-SCREEN-NAME.
    MOVE BKU-FD-FDEF-NO                         TO SCR-FDEF-NO.
    MOVE BKU-FD-COBOL-NAME                      TO SCR-FDEF-COBOL-NAME.
    MOVE BKU-FD-DESCRIPTION                     TO SCR-FDEF-DESCRIPTION.
    MOVE BKU-FD-LINE                            TO SCR-FDEF-LINE.
    MOVE BKU-FD-COLUMN                          TO SCR-FDEF-COLUMN.
    MOVE BKU-FD-BUFFER-LENGTH                   TO SCR-FDEF-BUFFER-LENGTH.
    MOVE BKU-FD-WINDOW-LENGTH                   TO SCR-FDEF-WINDOW-LENGTH.
    MOVE BKU-FD-CLEAR                           TO SCR-FDEF-CLEAR.
    MOVE BKU-FD-UPPERCASE                       TO SCR-FDEF-UPPERCASE.
    MOVE BKU-FD-PASSWORD                        TO SCR-FDEF-PASSWORD.
    MOVE BKU-FD-NOT-BLANK                       TO SCR-FDEF-NOT-BLANK.
    MOVE BKU-FD-YN                              TO SCR-FDEF-YN.
    MOVE BKU-FD-RES-CHARSET                     TO SCR-FDEF-RES-CHARSET.
    MOVE BKU-FD-SIGNED                          TO SCR-FDEF-SIGNED.
    MOVE BKU-FD-DIGITS                          TO SCR-FDEF-DIGITS.
    MOVE BKU-FD-DECIMALS                        TO SCR-FDEF-DECIMALS.
    MOVE BKU-FD-VERIFY                          TO SCR-FDEF-VERIFY.
    MOVE BKU-FD-VISIBLE                         TO SCR-FDEF-VISIBLE.
    MOVE BKU-FD-IGNORE-CHANGES                  TO SCR-FDEF-IGNORE-CHANGES.
    MOVE BKU-FD-INPUT-SEQ                       TO SCR-FDEF-INPUT-SEQ.
    MOVE BKU-FD-ACTION                          TO SCR-FDEF-ACTION.
    MOVE BKU-FD-HELP                            TO SCR-FDEF-HELP.
    MOVE BKU-FD-READ-ONLY                       TO SCR-FDEF-READ-ONLY.

    IF NOT BKU-FD-COMP-TYPE NUMERIC THEN
        MOVE ZERO                               TO BKU-FD-COMP-TYPE
    END-IF.

    MOVE BKU-FD-COMP-TYPE                       TO SCR-FDEF-COMP-TYPE.

    IF BKU-RECD-VERSION >= 03 THEN
        MOVE BKU-FD-MENU-REF                    TO SCR-FDEF-MENU-REF
    ELSE
        MOVE SPACES                             TO SCR-FDEF-MENU-REF
    END-IF.

    IF BKU-FD-ACTION-EDIT = SPACE THEN                  *> EARLY RELEASES DID NOT HAVE THIS FLAG
        MOVE 'Y'                                TO SCR-FDEF-ACTION-EDIT
    ELSE
        MOVE BKU-FD-ACTION-EDIT                 TO SCR-FDEF-ACTION-EDIT
    END-IF.
    EXIT.

8020-VALIDATE-BG.
    IF BKU-BG-SEGMENT-NO IS NOT NUMERIC
        MOVE "BKU-BG-SEGMENT-NO"                TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-BG-LINE IS NOT NUMERIC
        MOVE "BKU-BG-LINE"                      TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-BG-COLUMN IS NOT NUMERIC
        MOVE "BKU-BG-COLUMN"                    TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-BG-LENGTH IS NOT NUMERIC
        MOVE "BKU-BG-LENGTH"                    TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-BG-ATTRIBUTE IS NOT NUMERIC
        MOVE "BKU-BG-ATTRIBUTE"                 TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-BG-COLOUR-PAIR NOT = SPACES THEN
        IF BKU-BG-COLOUR-PAIR IS NOT NUMERIC
            MOVE "BKU-BG-COLOUR-PAIR"           TO WS-MSG-TEXT
            PERFORM 9900-ERROR-MESSAGE
        END-IF
    END-IF.

    PERFORM 7800-CHECK-VALIDATION.
    EXIT.            

8020-POPULATE-BG.
    MOVE BKU-BG-NAME                            TO SCRBG-NAME.
    MOVE BKU-BG-SEGMENT-NO                      TO SCRBG-SEGMENT-NO.
    MOVE BKU-BG-LINE                            TO SCRBG-LINE.
    MOVE BKU-BG-COLUMN                          TO SCRBG-COLUMN.
    MOVE BKU-BG-LENGTH                          TO SCRBG-LENGTH.
    MOVE BKU-BG-SEGMENT                         TO SCRBG-SEGMENT.
    MOVE BKU-BG-ATTRIBUTE                       TO SCRBG-ATTRIBUTE.
    MOVE BKU-BG-COLOUR-PAIR                     TO SCRBG-COLOUR-PAIR.

    IF BKU-BG-HEX-SEGMENT NOT = SPACES THEN
        PERFORM 8025-POPULATE-BG-FROM-HEX
    END-IF.
    EXIT.

8025-POPULATE-BG-FROM-HEX.
    MOVE LENGTH OF BKU-BG-HEX-SEGMENT           TO WS-LEN-HEX-SEGMENT
    MOVE LENGTH OF SCRBG-SEGMENT                TO WS-LEN-SCRBG-SEGMENT
    MOVE SPACES                                 TO SCRBG-SEGMENT
    CALL "COBCURSES_HEX2ASCII"                  *> ASCII OUTPUT GOES TO SCRBG-SGMENT
        USING BKU-BG-HEX-SEGMENT, WS-LEN-HEX-SEGMENT, SCRBG-SEGMENT, WS-LEN-SCRBG-SEGMENT.
    EXIT.

8030-VALIDATE-FS.
    IF BKU-FS-STATE-NO IS NOT NUMERIC
        MOVE "BKU-FS-STATE-NO"                  TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-FS-FIELD-NO IS NOT NUMERIC
        MOVE "BKU-FS-FIELD-NO"                  TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-FS-BACK-TO IS NOT NUMERIC
        MOVE "BKU-FS-BACK-TO"                   TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-FS-FORWARD-TO IS NOT NUMERIC
        MOVE "BKU-FS-FORWARD-TO"                TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-FS-ESCAPE-TO IS NOT NUMERIC
        MOVE "BKU-FS-ESCAPE-TO"                 TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    IF BKU-FS-SLASH-TO IS NOT NUMERIC
        MOVE "BKU-FS-SLASH-TO"                  TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    END-IF.

    MOVE BKU-FS-GROUP-HEADER                    TO WS-FLAG.
    MOVE "BKU-FS-GROUP-HEADER"                  TO WS-MSG-TEXT.
    PERFORM 7900-FLAG-CHECK.

    PERFORM 7800-CHECK-VALIDATION.
    EXIT.

8030-POPULATE-FS.
    MOVE BKU-FS-SCREEN-NAME                     TO SCR-FST-SCREEN-NAME.
    MOVE BKU-FS-STATE-NO                        TO SCR-FST-STATE-NO.
    MOVE BKU-FS-FIELD-NO                        TO SCR-FST-FIELD-NO.
    MOVE BKU-FS-BACK-TO                         TO SCR-FST-BACK-TO.
    MOVE BKU-FS-FORWARD-TO                      TO SCR-FST-FORWARD-TO.
    MOVE BKU-FS-ESCAPE-TO                       TO SCR-FST-ESCAPE-TO.
    MOVE BKU-FS-SLASH-TO                        TO SCR-FST-SLASH-TO.
    MOVE BKU-FS-GROUP-HEADER                    TO SCR-FST-GROUP-HEADER.
    MOVE BKU-FS-STATE-COBOL-NAME                TO SCR-FST-STATE-COBOL-NAME.
    EXIT.

8040-POPULATE-CS.
    MOVE BKU-CS-NAME                            TO CHARSET-NAME.
    MOVE BKU-CS-DATA                            TO CHARSET-DATA.
    EXIT.

8050-POPULATE-MU.
    MOVE BKU-MU-MENU-NAME                       TO MNU-MENU-NAME.
    MOVE BKU-MU-TITLE                           TO MNU-TITLE.
    MOVE BKU-MU-TOP-LEFT-LINE-NO                TO MNU-TOP-LEFT-LINE-NO.
    MOVE BKU-MU-TOP-LEFT-COLUMN-NO              TO MNU-TOP-LEFT-COLUMN-NO.
    MOVE BKU-MU-OPT-ONEVALUE                    TO MNU-OPT-ONEVALUE.
    MOVE BKU-MU-OPT-ROWMAJOR                    TO MNU-OPT-ROWMAJOR.
    MOVE BKU-MU-OPT-IGNORECASE                  TO MNU-OPT-IGNORECASE.
    MOVE BKU-MU-OPT-SHOWDESC                    TO MNU-OPT-SHOWDESC.
    MOVE BKU-MU-OPT-NONCYCLIC                   TO MNU-OPT-NONCYCLIC.
    MOVE BKU-MU-OPT-SHOWMATCH                   TO MNU-OPT-SHOWMATCH.

    IF BKU-MU-OPT-ROWS IS NOT NUMERIC THEN
        MOVE "BKU-MU-OPT-ROWS" TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    ELSE
        MOVE BKU-MU-OPT-ROWS                    TO MNU-OPT-ROWS
    END-IF.

    IF BKU-MU-OPT-COLS IS NOT NUMERIC THEN
        MOVE "BKU-MU-OPT-COLS" TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    ELSE
        MOVE BKU-MU-OPT-COLS                    TO MNU-OPT-COLS
    END-IF.

    IF BKU-MU-MENU-TYPE IS NOT NUMERIC THEN
        MOVE "BKU-MU-MENU-TYPE" TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    ELSE
        MOVE BKU-MU-MENU-TYPE                   TO MNU-MENU-TYPE
    END-IF.

    MOVE BKU-MU-MODULE-NAME                     TO MNU-MODULE-NAME

    IF BKU-MU-ITEM-LIMIT IS NOT NUMERIC THEN
        MOVE "BKU-MU-ITEM-LIMIT" TO WS-MSG-TEXT
        PERFORM 9900-ERROR-MESSAGE
    ELSE
        MOVE BKU-MU-ITEM-LIMIT                  TO MNU-ITEM-LIMIT
    END-IF.
    EXIT.

8060-POPULATE-IT.
    MOVE BKU-IT-MENU-NAME                       TO ITM-MENU-NAME.
    MOVE BKU-IT-NUMBER                          TO ITM-NUMBER.
    MOVE BKU-IT-ITEM-NAME                       TO ITM-ITEM-NAME.
    MOVE BKU-IT-TEXT                            TO ITM-TEXT.
    MOVE BKU-IT-SELECTABLE                      TO ITM-SELECTABLE.
    EXIT.

8070-POPULATE-MR.
    MOVE BKU-MR-SCREEN-NAME                     TO MREF-SCREEN-NAME.
    MOVE BKU-MR-MENU-NAME                       TO MREF-MENU-NAME.
    EXIT.

9000-FINALIZE.
    PERFORM 9300-CLOSE-FILES.
    DISPLAY "READ   ", WS-BKU-RECDS, " BACKUP RECORDS.".
    DISPLAY "WROTE  ", WS-SC-RECDS, " SC RECORDS.".
    DISPLAY "WROTE  ", WS-FD-RECDS, " FD RECORDS.".
    DISPLAY "WROTE  ", WS-BG-RECDS, " BG RECORDS.".
    DISPLAY "WROTE  ", WS-FS-RECDS, " FS RECORDS.".
    DISPLAY "WROTE  ", WS-CS-RECDS, " CS RECORDS.".
    DISPLAY "WROTE  ", WS-MU-RECDS, " MU RECORDS.".
    DISPLAY "WROTE  ", WS-IT-RECDS, " IT RECORDS.".
    DISPLAY "WROTE  ", WS-MR-RECDS, " MR RECORDS.".
    DISPLAY "ERRORS ", WS-ERROR-COUNT.
    DISPLAY "TOTAL  ", WS-TOTAL-COUNT, " LOADED RECORDS.".
    IF WS-ERROR-COUNT > 0 THEN
        DISPLAY " "
        DISPLAY "*** WARNING *** "
            "THERE WERE ERRORS IN THIS LOAD ***"
    END-IF.
    EXIT.

9900-ERROR-MESSAGE.
    MOVE WS-BKU-RECDS TO WS-MSG-LINE-NO.
    DISPLAY WS-MESSAGE.
    ADD 1 TO WS-ERROR-COUNT.
    EXIT.

9910-DUP-KEY.
    MOVE WS-BKU-RECDS TO WS-MSG-LINE-NO.
    MOVE "DUPLICATE KEY" TO WS-MSG-TEXT.
    DISPLAY WS-MESSAGE.
    ADD 1 TO WS-ERROR-COUNT.
    EXIT.


9100-OPEN-FILES.
    OPEN INPUT BKU-FILE.
    OPEN OUTPUT SC-FILE FD-FILE BG-FILE FS-FILE CS-FILE MU-FILE IT-FILE MR-FILE.
    EXIT.

9200-CLOSE-FILES.
    EXIT.

9300-CLOSE-FILES.
    CLOSE SC-FILE FD-FILE BG-FILE FS-FILE CS-FILE MU-FILE IT-FILE MR-FILE.
    CLOSE BKU-FILE.
    EXIT.

END PROGRAM SDLOAD.
