IDENTIFICATION DIVISION.
PROGRAM-ID. SDBACKUP.
*>
*> THIS PROGRAM BACKS UP THE COBCURSES DATABASE TO A
*> SINGLE TEXT FILE, BY RECORD TYPE. THIS FILE
*> CAN BE "LOADED" BY SDLOAD.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.

    SELECT SC-FILE                                      *> SCREEN DEFINITIONS FILE
        ASSIGN TO SC-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS SCN-NAME.

    SELECT FD-FILE                                      *> FIELD DEFINITIONS FILE
        ASSIGN TO FD-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS SCR-FDEF-KEY.
        
    SELECT BG-FILE                                      *> SCREEN BACKGROUND FILE
        ASSIGN TO BG-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS SCRBG-KEY.
        
    SELECT FS-FILE                                      *> FIELD STATES FILE
        ASSIGN TO FS-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS SCR-FST-KEY.

    SELECT CS-FILE                                      *> CHARACTER SETS AND UNIT SPECS
        ASSIGN TO CS-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS CHARSET-NAME.

    SELECT MU-FILE                                      *> MENU DEFINITIONS
        ASSIGN TO MU-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS MNU-KEY.

    SELECT IT-FILE                                      *> MENU ITEM DEFINITIONS
        ASSIGN TO IT-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS ITM-KEY.

    SELECT MR-FILE                                      *> SCREEN REFS TO MENUS
        ASSIGN TO MR-FILE-NAME
        ORGANIZATION IS INDEXED
        ACCESS IS DYNAMIC
        RECORD KEY IS MREF-KEY.

    SELECT BKU-FILE                                     *> THE OUTPUT BACKUP FILE
        ASSIGN TO "backup.txt"
        ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.

    FD  SC-FILE.                                        *> SCREEN DEFINITIONS
    01  SC-RECORD.
        COPY SCREEN-01.

    FD  FD-FILE.                                        *> FIELD DEFINITIONS
    01  FD-RECORD.
        COPY SCREEN-FD.

    FD  BG-FILE.                                        *> SCREEN BACKGROUNDS
    01  BG-RECORD.
        COPY SCREEN-BG.

    FD  FS-FILE.                                        *> FIELD STATES
    01  FS-RECORD.
        COPY SCREEN-FS.

    FD  CS-FILE.                                        *> CHARACTER SETS
    01  CS-RECORD.
        COPY SCREEN-CS.

    FD  MU-FILE.                                        *> MENUS
    01  MU-RECORD.
        COPY MENURECD.

    FD  IT-FILE.                                        *> MENU ITEMS
    01  IT-RECORD.
        COPY ITEMRECD.

    FD  MR-FILE.                                        *> MENU REFS
    01  MR-RECORD.
        COPY MENUREF.

    FD  BKU-FILE.                                       *> BACKUP FILE
    01  BKU-RECORD.
        COPY BKU-RECD.

WORKING-STORAGE SECTION.

    01  WS-BACKUP-FILE-VERSION          PIC 99 VALUE 03.    *> STARTING RELEASE 0.95

    01  WS-PATHNAMES.
        10  FILE-NAME-LENGTH            PIC 9999.       *> LENGTH FOR ALL FILE NAMES 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".

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

    01  WS-COUNTERS.
        10  WS-SC-RECDS                 PIC 9(9) VALUE 0.   *> SCREEN DEFINITION COUNT
        10  WS-FD-RECDS                 PIC 9(9) VALUE 0.   *> FIELD DEFINITION COUNT
        10  WS-BG-RECDS                 PIC 9(9) VALUE 0.   *> BACKGROUND SEGMENT COUNT
        10  WS-FS-RECDS                 PIC 9(9) VALUE 0.   *> FIELD STATE COUNT
        10  WS-CS-RECDS                 PIC 9(9) VALUE 0.   *> CHARSET RECORD COUNT
        10  WS-MU-RECDS                 PIC 9(9) VALUE 0.   *> MENU RECORD COUNT
        10  WS-IT-RECDS                 PIC 9(9) VALUE 0.   *> MENU ITEM RECORD COUNT
        10  WS-MR-RECDS                 PIC 9(9) VALUE 0.   *> MENU REF RECORD COUNT
        10  WS-BKU-RECDS                PIC 9(9) VALUE 0.   *> COUNT OF WRITTEN BACKUP RECORDS

    01  WS-LEN-BG-SEGMENT               PIC 9999 COMP-5.    *> LENGTH OF A BACKGROUND SCREEN SEGMENT
    01  WS-LEN-BG-HEX-SEGMENT           PIC 9999 COMP-5.    *> LENGTH OF A SEGMENT IN HEXADECIMAL
    01  WS-X                            PIC 9999 COMP-5.    *> WORK VARIABLE

PROCEDURE DIVISION.

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

1000-INITIALIZE.
    PERFORM 1010-EDIT-PATHNAMES.                            *> SUBSTITUTE ENVIRONMENT VARIABLES IN PATHNAMES
    PERFORM 9010-OPEN-FILES.                                *> 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 6000-UNLOAD-SC-FILE.                            *> UNLOAD SCREEN DEFINITIONS
    PERFORM 6010-UNLOAD-FD-FILE.                            *> UNLOAD FIELD DEFINITIONS
    PERFORM 6020-UNLOAD-BG-FILE.                            *> UNLOAD BACKGROUND SEGMENTS
    PERFORM 6030-UNLOAD-FS-FILE.                            *> UNLOAD FIELD STATES
    PERFORM 6040-UNLOAD-CS-FILE.                            *> UNLOAD CHARSETS
    PERFORM 6050-UNLOAD-MU-FILE.                            *> UNLOAD MENUS
    PERFORM 6060-UNLOAD-IT-FILE.                            *> UNLOAD MENU ITEMS
    PERFORM 6070-UNLOAD-MR-FILE.                            *> UNLOAD MENU REFS
    EXIT.

5010-INIT-SDBACKUP-HEADER.
    INITIALIZE BKU-RECORD.                                  *> INITIALIZE THE BACKUP RECORD
    MOVE "SD" TO BKU-FILE-TYPE.                             *> EVERY RECD HAS "SD" IN IT FOR FILE TYPE
    MOVE WS-BACKUP-FILE-VERSION TO BKU-RECD-VERSION.        *> INDICATE THE VERSION OF THIS BACKUP FORMAT
    MOVE LENGTH OF BKU-HEADER TO BKU-RECD-LENGTH.           *> THIS IS THE SMALLEST COMMON SIZE FOR BACKUP RECD
    EXIT.

5020-INIT-SC-BKU-RECORD.
    PERFORM 5010-INIT-SDBACKUP-HEADER.                      *> START A BACKUP RECORD
    MOVE "SC" TO BKU-RECD-TYPE.                             *> MARK AS SCREEN DEFINITION RECD
    ADD LENGTH OF BKU-SC-DETAIL TO BKU-RECD-LENGTH.         *> UPDATE THE ACTUAL SIZE
    INITIALIZE BKU-SC-DETAIL.
    EXIT.

5030-INIT-FD-BKU-RECORD.
    PERFORM 5010-INIT-SDBACKUP-HEADER.
    MOVE "FD" TO BKU-RECD-TYPE.                             *> FIELD DEFINITION RECD
    ADD LENGTH OF BKU-FD-DETAIL TO BKU-RECD-LENGTH.
    INITIALIZE BKU-FD-DETAIL.
    EXIT.

5040-INIT-BG-BKU-RECORD.
    PERFORM 5010-INIT-SDBACKUP-HEADER.
    MOVE "BG" TO BKU-RECD-TYPE.                             *> BACKGROUND SCREEN SEGMENT RECORD
    ADD LENGTH OF BKU-BG-DETAIL TO BKU-RECD-LENGTH.
    INITIALIZE BKU-BG-DETAIL.
    EXIT.

5050-INIT-FS-BKU-RECORD.
    PERFORM 5010-INIT-SDBACKUP-HEADER.
    MOVE "FS" TO BKU-RECD-TYPE.                             *> FIELD STATE RECORD
    ADD LENGTH OF BKU-FS-DETAIL TO BKU-RECD-LENGTH.
    INITIALIZE BKU-FS-DETAIL.
    EXIT.

5060-INIT-CS-BKU-RECORD.
    PERFORM 5010-INIT-SDBACKUP-HEADER.
    MOVE "CS" TO BKU-RECD-TYPE.                             *> CHARSET RECORD
    ADD LENGTH OF BKU-CS-DETAIL TO BKU-RECD-LENGTH.
    INITIALIZE BKU-CS-DETAIL.
    EXIT.

5070-INIT-MU-BKU-RECORD.
    PERFORM 5010-INIT-SDBACKUP-HEADER.
    MOVE "MU" TO BKU-RECD-TYPE.                             *> MENU RECORD
    ADD LENGTH OF BKU-MU-DETAIL TO BKU-RECD-LENGTH.
    INITIALIZE BKU-MU-DETAIL.
    EXIT.

5080-INIT-IT-BKU-RECORD.
    PERFORM 5010-INIT-SDBACKUP-HEADER.
    MOVE "IT" TO BKU-RECD-TYPE.                             *> MENU ITEM RECORD
    ADD LENGTH OF BKU-IT-DETAIL TO BKU-RECD-LENGTH.
    INITIALIZE BKU-IT-DETAIL.
    EXIT.

5090-INIT-MR-BKU-RECORD.
    PERFORM 5010-INIT-SDBACKUP-HEADER.
    MOVE "MR" TO BKU-RECD-TYPE.                             *> MENU REF RECORD
    ADD LENGTH OF BKU-MR-DETAIL TO BKU-RECD-LENGTH.
    INITIALIZE BKU-MR-DETAIL.
    EXIT.

6000-UNLOAD-SC-FILE.
    INITIALIZE SC-RECORD.
    START SC-FILE KEY IS >= SCN-NAME
        INVALID KEY
            SET WS-EOF TO TRUE
        NOT INVALID KEY
            SET WS-HAVE-INPUT TO TRUE
    END-START.
    PERFORM UNTIL WS-EOF
        PERFORM 7000-READ-SC-FILE
        IF NOT WS-EOF THEN
            PERFORM 5020-INIT-SC-BKU-RECORD
            PERFORM 8000-POPULATE-SC
            PERFORM 7900-WRITE-BKU
        END-IF
    END-PERFORM.
    EXIT.

6010-UNLOAD-FD-FILE.
    INITIALIZE FD-RECORD.
    START FD-FILE KEY IS >= SCR-FDEF-KEY
        INVALID KEY
            SET WS-EOF TO TRUE
        NOT INVALID KEY
            SET WS-HAVE-INPUT TO TRUE
    END-START.
    PERFORM UNTIL WS-EOF
        PERFORM 7010-READ-FD-FILE
        IF NOT WS-EOF THEN
            PERFORM 5030-INIT-FD-BKU-RECORD
            PERFORM 8010-POPULATE-FD
            PERFORM 7900-WRITE-BKU
        END-IF
    END-PERFORM.
    EXIT.

6020-UNLOAD-BG-FILE.
    INITIALIZE BG-RECORD.
    START BG-FILE KEY IS >= SCRBG-KEY
        INVALID KEY
            SET WS-EOF TO TRUE
        NOT INVALID KEY
            SET WS-HAVE-INPUT TO TRUE
    END-START.
    PERFORM UNTIL WS-EOF
        PERFORM 7020-READ-BG-FILE
        IF NOT WS-EOF THEN
            PERFORM 5040-INIT-BG-BKU-RECORD
            PERFORM 8020-POPULATE-BG
            PERFORM 7900-WRITE-BKU
        END-IF
    END-PERFORM.
    EXIT.

6030-UNLOAD-FS-FILE.
    INITIALIZE FS-RECORD.
    START FS-FILE KEY IS >= SCR-FST-KEY
        INVALID KEY
            SET WS-EOF TO TRUE
        NOT INVALID KEY
            SET WS-HAVE-INPUT TO TRUE
    END-START.
    PERFORM UNTIL WS-EOF
        PERFORM 7030-READ-FS-FILE
        IF NOT WS-EOF THEN
            PERFORM 5050-INIT-FS-BKU-RECORD
            PERFORM 8030-POPULATE-FS
            PERFORM 7900-WRITE-BKU
        END-IF
    END-PERFORM.
    EXIT.

6040-UNLOAD-CS-FILE.
    INITIALIZE CS-RECORD.
    START CS-FILE KEY IS >= CHARSET-NAME
        INVALID KEY
            SET WS-EOF TO TRUE
        NOT INVALID KEY
            SET WS-HAVE-INPUT TO TRUE
    END-START.
    PERFORM UNTIL WS-EOF
       PERFORM 7040-READ-CS-FILE
        IF NOT WS-EOF THEN
            PERFORM 5060-INIT-CS-BKU-RECORD
            PERFORM 8040-POPULATE-CS
            PERFORM 7900-WRITE-BKU
        END-IF
    END-PERFORM.
    EXIT.

6050-UNLOAD-MU-FILE.
    INITIALIZE MU-RECORD.
    START MU-FILE KEY IS >= MNU-KEY
        INVALID KEY
            SET WS-EOF TO TRUE
        NOT INVALID KEY
            SET WS-HAVE-INPUT TO TRUE
    END-START.
    PERFORM UNTIL WS-EOF
        PERFORM 7050-READ-MU-FILE
        IF NOT WS-EOF THEN
            PERFORM 5070-INIT-MU-BKU-RECORD
            PERFORM 8050-POPULATE-MU
            PERFORM 7900-WRITE-BKU
        END-IF
    END-PERFORM.
    EXIT.

6060-UNLOAD-IT-FILE.
    INITIALIZE IT-RECORD.
    START IT-FILE KEY IS >= ITM-KEY
        INVALID KEY
            SET WS-EOF TO TRUE
        NOT INVALID KEY
            SET WS-HAVE-INPUT TO TRUE
    END-START.
    PERFORM UNTIL WS-EOF
        PERFORM 7060-READ-IT-FILE
        IF NOT WS-EOF THEN
            PERFORM 5080-INIT-IT-BKU-RECORD
            PERFORM 8060-POPULATE-IT
            PERFORM 7900-WRITE-BKU
        END-IF
    END-PERFORM.
    EXIT.

6070-UNLOAD-MR-FILE.
    INITIALIZE MR-RECORD.
    START MR-FILE KEY IS >= MREF-KEY
        INVALID KEY
            SET WS-EOF TO TRUE
        NOT INVALID KEY
            SET WS-HAVE-INPUT TO TRUE
    END-START.
    PERFORM UNTIL WS-EOF
        PERFORM 7060-READ-MR-FILE
        IF NOT WS-EOF THEN
            PERFORM 5090-INIT-MR-BKU-RECORD
            PERFORM 8070-POPULATE-MR
            PERFORM 7900-WRITE-BKU
        END-IF
    END-PERFORM.
    EXIT.

7000-READ-SC-FILE.
    READ SC-FILE NEXT RECORD
        AT END
            SET WS-EOF TO TRUE
        NOT AT END
            ADD 1 TO WS-SC-RECDS
    END-READ.
    EXIT.

7010-READ-FD-FILE.
    READ FD-FILE NEXT RECORD
        AT END
            SET WS-EOF TO TRUE
        NOT AT END
            ADD 1 TO WS-FD-RECDS
    END-READ.
    EXIT.

7020-READ-BG-FILE.
    READ BG-FILE NEXT RECORD
        AT END
            SET WS-EOF TO TRUE
        NOT AT END
            ADD 1 TO WS-BG-RECDS
    END-READ.
    EXIT.

7030-READ-FS-FILE.
    READ FS-FILE NEXT RECORD
        AT END
            SET WS-EOF TO TRUE
        NOT AT END
            ADD 1 TO WS-FS-RECDS
    END-READ.
    EXIT.

7040-READ-CS-FILE.
    READ CS-FILE NEXT RECORD
        AT END
            SET WS-EOF TO TRUE
        NOT AT END
            ADD 1 TO WS-CS-RECDS
    END-READ.
    EXIT.

7050-READ-MU-FILE.
    READ MU-FILE NEXT RECORD
        AT END
            SET WS-EOF TO TRUE
        NOT AT END
            ADD 1 TO WS-MU-RECDS
    END-READ.
    EXIT.

7060-READ-IT-FILE.
    READ IT-FILE NEXT RECORD
        AT END
            SET WS-EOF TO TRUE
        NOT AT END
            ADD 1 TO WS-IT-RECDS
    END-READ.
    EXIT.

7060-READ-MR-FILE.
    READ MR-FILE NEXT RECORD
        AT END
            SET WS-EOF TO TRUE
        NOT AT END
            ADD 1 TO WS-MR-RECDS
    END-READ.
    EXIT.

7900-WRITE-BKU.
    WRITE BKU-RECORD.
    ADD 1 TO WS-BKU-RECDS.
    EXIT.

8000-POPULATE-SC.
    MOVE SCN-NAME                           TO BKU-SC-NAME.
    MOVE SCN-DESCRIPTION                    TO BKU-SC-DESCRIPTION.
    MOVE SCN-AUTHOR                         TO BKU-SC-AUTHOR.
    MOVE SCN-NOTES                          TO BKU-SC-NOTES.
    MOVE SCN-TITLE                          TO BKU-SC-TITLE.
    MOVE SCN-SHOW-DATE                      TO BKU-SC-SHOW-DATE.
    MOVE SCN-SHOW-TIME                      TO BKU-SC-SHOW-TIME.
    MOVE SCN-ACTION-REQUIRED                TO BKU-SC-ACTION-REQUIRED.
    MOVE SCN-COLUMNS-MIN                    TO BKU-SC-COLUMNS-MIN.
    MOVE SCN-LINES-MIN                      TO BKU-SC-LINES-MIN.
    MOVE SCN-PAIRS                          TO BKU-SC-PAIRS.
    MOVE SCN-WS-SECTION                     TO BKU-SC-WS-SECTION.
    MOVE SCN-LINKAGE-SECTION                TO BKU-SC-LINKAGE-SECTION.
    MOVE SCN-PROCEDURE-DIVISION             TO BKU-SC-PROCEDURE-DIVISION.
    MOVE SCN-STRIP-CHARACTER                TO BKU-SC-STRIP-CHARACTER.
    EXIT.

8010-POPULATE-FD.
    MOVE SCR-FDEF-SCREEN-NAME               TO BKU-FD-SCREEN-NAME.
    MOVE SCR-FDEF-NO                        TO BKU-FD-FDEF-NO.
    MOVE SCR-FDEF-COBOL-NAME                TO BKU-FD-COBOL-NAME.
    MOVE SCR-FDEF-DESCRIPTION               TO BKU-FD-DESCRIPTION.
    MOVE SCR-FDEF-LINE                      TO BKU-FD-LINE.
    MOVE SCR-FDEF-COLUMN                    TO BKU-FD-COLUMN.
    MOVE SCR-FDEF-BUFFER-LENGTH             TO BKU-FD-BUFFER-LENGTH.
    MOVE SCR-FDEF-WINDOW-LENGTH             TO BKU-FD-WINDOW-LENGTH.
    MOVE SCR-FDEF-CLEAR                     TO BKU-FD-CLEAR.
    MOVE SCR-FDEF-UPPERCASE                 TO BKU-FD-UPPERCASE.
    MOVE SCR-FDEF-PASSWORD                  TO BKU-FD-PASSWORD.
    MOVE SCR-FDEF-NOT-BLANK                 TO BKU-FD-NOT-BLANK.
    MOVE SCR-FDEF-YN                        TO BKU-FD-YN.
    MOVE SCR-FDEF-RES-CHARSET               TO BKU-FD-RES-CHARSET.
    MOVE SCR-FDEF-SIGNED                    TO BKU-FD-SIGNED.
    MOVE SCR-FDEF-DIGITS                    TO BKU-FD-DIGITS.
    MOVE SCR-FDEF-DECIMALS                  TO BKU-FD-DECIMALS.
    MOVE SCR-FDEF-VERIFY                    TO BKU-FD-VERIFY.
    MOVE SCR-FDEF-VISIBLE                   TO BKU-FD-VISIBLE.
    MOVE SCR-FDEF-IGNORE-CHANGES            TO BKU-FD-IGNORE-CHANGES.
    MOVE SCR-FDEF-INPUT-SEQ                 TO BKU-FD-INPUT-SEQ.
    MOVE SCR-FDEF-ACTION                    TO BKU-FD-ACTION.
    MOVE SCR-FDEF-HELP                      TO BKU-FD-HELP.
    MOVE SCR-FDEF-READ-ONLY TO BKU-FD-READ-ONLY.
    IF NOT SCR-FDEF-COMP-TYPE NUMERIC THEN
        MOVE ZERO                           TO SCR-FDEF-COMP-TYPE
    END-IF.
    MOVE SCR-FDEF-COMP-TYPE                 TO BKU-FD-COMP-TYPE.
    MOVE SCR-FDEF-MENU-REF                  TO BKU-FD-MENU-REF.
    MOVE SCR-FDEF-ACTION-EDIT               TO BKU-FD-ACTION-EDIT.
    EXIT.

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

    CALL "COBCURSES_SPECIAL_CHARS" USING BKU-BG-SEGMENT, WS-LEN-BG-SEGMENT, BKU-BG-HEX-SEGMENT, WS-LEN-BG-HEX-SEGMENT.
    IF RETURN-CODE = ZERO THEN
        MOVE SPACES                         TO BKU-BG-HEX-SEGMENT
    ELSE
        PERFORM VARYING WS-X FROM 1 BY 1 UNTIL WS-X > WS-LEN-BG-SEGMENT
            IF BKU-BG-SEGMENT(WS-X:1) < SPACE THEN
                MOVE '@'                    TO BKU-BG-SEGMENT(WS-X:1)
            END-IF
        END-PERFORM
    END-IF.
    EXIT.

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

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

8050-POPULATE-MU.
    MOVE MNU-MENU-NAME                      TO BKU-MU-MENU-NAME.
    MOVE MNU-MENU-TYPE                      TO BKU-MU-MENU-TYPE.
    MOVE MNU-TITLE                          TO BKU-MU-TITLE.
    MOVE MNU-TOP-LEFT-LINE-NO               TO BKU-MU-TOP-LEFT-LINE-NO.
    MOVE MNU-TOP-LEFT-COLUMN-NO             TO BKU-MU-TOP-LEFT-COLUMN-NO.
    MOVE MNU-OPT-ONEVALUE                   TO BKU-MU-OPT-ONEVALUE.
    MOVE MNU-OPT-ROWMAJOR                   TO BKU-MU-OPT-ROWMAJOR.
    MOVE MNU-OPT-IGNORECASE                 TO BKU-MU-OPT-IGNORECASE.
    MOVE MNU-OPT-SHOWDESC                   TO BKU-MU-OPT-SHOWDESC.
    MOVE MNU-OPT-NONCYCLIC                  TO BKU-MU-OPT-NONCYCLIC.
    MOVE MNU-OPT-SHOWMATCH                  TO BKU-MU-OPT-SHOWMATCH.
    MOVE MNU-OPT-ROWS                       TO BKU-MU-OPT-ROWS.
    MOVE MNU-OPT-COLS                       TO BKU-MU-OPT-COLS.
    MOVE MNU-MODULE-NAME                    TO BKU-MU-MODULE-NAME.
    MOVE MNU-ITEM-LIMIT                     TO BKU-MU-ITEM-LIMIT.
    EXIT.

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

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

9000-FINALIZE.
    PERFORM 9020-CLOSE-FILES.
    DISPLAY "READ  ", WS-SC-RECDS, " SC RECORDS.".
    DISPLAY "READ  ", WS-FD-RECDS, " FD RECORDS.".
    DISPLAY "READ  ", WS-BG-RECDS, " BG RECORDS.".
    DISPLAY "READ  ", WS-FS-RECDS, " FS RECORDS.".
    DISPLAY "READ  ", WS-CS-RECDS, " CS RECORDS.".
    DISPLAY "READ  ", WS-MU-RECDS, " MU RECORDS.".
    DISPLAY "READ  ", WS-IT-RECDS, " IT RECORDS.".
    DISPLAY "READ  ", WS-MR-RECDS, " MR RECORDS.".
    DISPLAY "WROTE ", WS-BKU-RECDS, " BACKUP RECORDS.".
    EXIT.

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

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

END PROGRAM SDBACKUP.
