IDENTIFICATION DIVISION.
PROGRAM-ID. INIT-COBC.
*>
*> WARREN W. GAY   Thu Sep 6, 2007
*>
*> INIT-COBC REPLACES THE ORIGINAL init-cobcurses SCRIPT THAT
*> WAS PROVIDED. HAVING THE SCRIPT REPLACED WITH THIS EXECUTABLE
*> ALLOWS IT TO WORK IN MINGW ENVIRONMENTS, IN ADDITION TO POSIX
*> ENVIRONMENTS.
*>
*> THIS PROGRAM TAKES CARE OF ALL THE NASTIES OF DELETING/RECREATING
*> NECESSARY DIRECTORIES, LOCATING THE LOAD FILE(S), AND LOADING
*> THE INDEXED FILES WITH THE HELP OF SDLOAD AND LOADTM PROGRAMS.
*>
DATA DIVISION.
WORKING-STORAGE SECTION.

    COPY COBCRETC.

    01  CMD-LINE.
        05  CMD-ARGC                        PIC 9(4).
        05  CMD-N                           PIC 9(4).
        05  CMD-EOF-FLAG                    PIC X.
            88  CMD-EOF                     VALUE 'Y' FALSE IS 'N'.
        05  CMD-ARG.
            10  CMD-OPTION-FLAG             PIC X.
                88  CMD-OPTION              VALUE '-'.
            10  CMD-OPTION-NAME             PIC X(32).
                88  CMD-OPT-F               VALUE 'f'.
                88  CMD-OPT-I               VALUE 'i'.
                88  CMD-OPT-V               VALUE 'v'.
                88  CMD-OPT-H               VALUE 'h'.
                88  CMD-OPT-HELP            VALUE '-help'.
                88  CMD-OPT-D               VALUE 'd'.
                88  CMD-OPT-B               VALUE 'b'.
                88  CMD-OPT-T               VALUE 't'.
                88  CMD-OPT-EOF             VALUE '-'.
            10  FILLER                      PIC X(479).            
        05  CMD-ARG-2 REDEFINES CMD-ARG.
            10  CMD-ARGUMENT                PIC X(512).
        05  CMD-ARG-LENGTH                  PIC 9(4).
        05  CMD-ARGLEN-5                    PIC 9(4) COMP-5.

    01  CMD-RETURN-CODE                     PIC S9(9)   VALUE 0.

    01  CMD-OPTIONS.
        05  OPT-VALID-FLAG                  PIC X.
            88  OPT-VALID                   VALUE 'Y' FALSE IS 'N'.
            88  OPT-ARG-INVALID             VALUE 'X'.
        05  OPT-CURRENT                     PIC X(32).
        05  OPT-CURRENT-LENGTH              PIC 9(4).
        05  OPT-F-FLAG                      PIC X.
            88  OPT-F                       VALUE 'Y' FALSE IS 'N'.
        05  OPT-I-FLAG                      PIC X.
            88  OPT-I                       VALUE 'Y' FALSE IS 'N'.
        05  OPT-V-FLAG                      PIC X.
            88  OPT-V                       VALUE 'Y' FALSE IS 'N'.
        05  OPT-H-FLAG                      PIC X.
            88  OPT-H                       VALUE 'Y' FALSE IS 'N'.
        05  OPT-B-FLAG                      PIC X.
            88  OPT-B                       VALUE 'Y' FALSE IS 'N'.
        05  OPT-B-ARG                       PIC X(512).
        05  OPT-D-FLAG                      PIC X.
            88  OPT-D                       VALUE 'Y' FALSE IS 'N'.
        05  OPT-D-ARG                       PIC X(512).
        05  OPT-T-FLAG                      PIC X.
            88  OPT-T                       VALUE 'Y' FALSE IS 'N'.
        05  OPT-T-ARG                       PIC X(512).

    01  WS-PATHNAME-INFO.
        05  WS-PATHNAME                     PIC X(512).
        05  WS-PATHNAME-LENGTH              PIC 9(4).
        05  WS-PATHNAME-LEN5                PIC 9(4) COMP-5.
        05  WS-PATHNAME-TYPE                PIC X.
        05  WS-PATHNAME-RC                  PIC S9(9).

    01  WS-FILENAME-INFO.
        05  WS-FILENAME                     PIC X(32).
        05  WS-FILENAME-LEN5                PIC 9(4) COMP-5.

    01  WS-SETENV.
        05  WS-ENV-COBCURSES-DATADIR        PIC X(32)      VALUE "COBCURSES_DATADIR".
        05  WS-ENV-COBCURSES-SDLOAD-FILE    PIC X(32)      VALUE "COBCURSES_SDLOAD_FILE".
        05  WS-ENV-LEN5                     PIC 9(4) COMP-5.

    01  WS-DATADIR                          PIC X(512)      VALUE "${COBCURSES_DATADIR}".
    01  WS-SHARE-BAK-DIR                    PIC X(512)      VALUE "${COBCURSES_SHAREDIR}/bak".
    01  WS-BAK-FILE                         PIC X(512)      VALUE "backup.bak".
    01  WS-BAK-TEMPLATE                     PIC X(512)      VALUE "data_tm.bak".
    01  WS-TEMPLATE-PATH                    PIC X(512).
    01  WS-BAK-EXISTS-FLAG                  PIC X.
        88  WS-BAK-EXISTS                   VALUE 'Y' FALSE IS 'N'.
    01  WS-BAK-TEMPLATE-EXISTS-FLAG         PIC X.
        88  WS-BAK-TEMPLATE-EXISTS          VALUE 'Y' FALSE IS 'N'.

    01  WS-SWITCHES.
        05  WS-DATADIR-EXISTS-FLAG          PIC X.
            88  WS-DATADIR-EXISTS           VALUE 'Y' FALSE IS 'N'.
        05  WS-DATABASE-EXISTS-FLAG         PIC X.
            88  WS-DATABASE-EXISTS          VALUE 'Y' FALSE IS 'N'.
        05  WS-LOAD-TEMPLATE-FLAG           PIC X.
            88  WS-LOAD-TEMPLATE            VALUE 'Y' FALSE IS 'N'.

    01  WS-INDEXED-FILES.
        05  WS-X                            PIC 99.
        05  WS-INDEXED-COUNT                PIC 99 VALUE 9.
        05  WS-INDEXED.
            10  PIC X(32)                   VALUE "SCREENS.X".
            10  PIC X(32)                   VALUE "SCRFDEF.X".
            10  PIC X(32)                   VALUE "SCRFSTA.X".
            10  PIC X(32)                   VALUE "SCRNBG.X".
            10  PIC X(32)                   VALUE "SCRCHRSET.X".
            10  PIC X(32)                   VALUE "MENUS.X".
            10  PIC X(32)                   VALUE "ITEMS.X".
            10  PIC X(32)                   VALUE "MENUREFS.X".
            10  WS-TEMPL-FILENAME PIC X(32) VALUE "TEMPLATE.X".
        05  FILLER                          REDEFINES WS-INDEXED OCCURS 1 TO 9 TIMES.
            10  WS-INDEXED-FILE-NAME        PIC X(32).

    01  WS-MISC.
        05  QUERY-YNQ-FLAG                  PIC X.
            88  QUERY-Y                     VALUE 'Y'.
            88  QUERY-N                     VALUE 'N'.
            88  QUERY-Q                     VALUE 'Q'.
        05  QUERY-VALID-FLAG                PIC X.
            88  QUERY-VALID                 VALUE 'Y' FALSE IS 'N'.

    01  WS-TODO-FLAGS.
        05  WS-TODO-CREATE-DATADIR-FLAG     PIC X.
            88  WS-TODO-CREATE-DATADIR      VALUE 'Y' FALSE IS 'N'.
        05  WS-TODO-DELETE-DB-FLAG          PIC X.
            88  WS-TODO-DELETE-DB           VALUE 'Y' FALSE IS 'N'.
        05  WS-TODO-DEL-TEMPLATE-FLAG       PIC X.
            88  WS-TODO-DEL-TEMPLATE        VALUE 'Y' FALSE IS 'N'.

PROCEDURE DIVISION.

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

1000-INITIALIZE.
    PERFORM 1100-INIT-CMD-OPTIONS.
    PERFORM 7000-OPEN-CMDLINE.
    PERFORM 6000-PROCESS-OPTIONS.

    IF OPT-H THEN
        PERFORM 8000-ISSUE-HELP
    END-IF.

    IF CMD-RETURN-CODE NOT = 0 OR OPT-H THEN
        MOVE CMD-RETURN-CODE TO RETURN-CODE
        GO TO 9999-STOP
    END-IF.
*>
*>     ALLOW -d OPTION TO OVERRIDE DEFAULT DATA DIR
*>
    IF OPT-D THEN
        MOVE OPT-D-ARG TO WS-DATADIR
    END-IF.
*>
*>     DETERMINE WHERE THE DATA DIRECTORY IS:
*>
    MOVE LENGTH OF WS-DATADIR TO WS-PATHNAME-LENGTH.
    CALL "COBCURSES-INIT-PATHNAME" USING WS-DATADIR, WS-PATHNAME-LENGTH.

    IF OPT-V THEN
        MOVE WS-DATADIR TO WS-PATHNAME
        PERFORM 7300-PATH-LENGTH
        DISPLAY "Data Directory:    '", WS-DATADIR(1:WS-PATHNAME-LENGTH), "'"
    END-IF.
*>
*>     SEE IF THE DATA DIRECTORY EXISTS :
*>
    MOVE "D" TO WS-PATHNAME-TYPE.
    MOVE WS-DATADIR TO WS-PATHNAME.
    PERFORM 6300-CHECK-EXISTS.
    IF WS-PATHNAME-RC = NC-RET-OK THEN
        SET WS-DATADIR-EXISTS TO TRUE
    ELSE
        SET WS-DATADIR-EXISTS TO FALSE
    END-IF.

    IF OPT-V THEN
        IF NOT WS-DATADIR-EXISTS THEN
            DISPLAY "  *** which does not exist ***"
        END-IF
    END-IF.
*>
*>     SEE IF ANY DATABASE FILES EXIST THERE :
*>
    IF WS-DATADIR-EXISTS THEN
        PERFORM 1200-CHECK-EXISTENCE
        IF OPT-V THEN
            IF WS-DATABASE-EXISTS THEN
                DISPLAY "There is a database there."
            ELSE
                DISPLAY "No database currently exists there."
            END-IF
        END-IF
    ELSE
        SET WS-DATABASE-EXISTS TO FALSE
    END-IF.

*>
*>     THIS IS THE DEFAULT DATA LOAD FILE IF NONE IS GIVEN
*>
    IF OPT-B THEN
        MOVE OPT-B-ARG TO WS-SHARE-BAK-DIR
    ELSE
        MOVE LENGTH OF WS-SHARE-BAK-DIR TO WS-PATHNAME-LENGTH
        CALL "COBCURSES-INIT-PATHNAME" USING WS-SHARE-BAK-DIR, WS-PATHNAME-LENGTH
    END-IF.

*>
*>     CREATE FULL PATHNAME OF backup.bak IN WS-BAK-FILE
*>
    MOVE LENGTH OF WS-SHARE-BAK-DIR TO WS-PATHNAME-LEN5.
    MOVE LENGTH OF WS-BAK-FILE TO WS-FILENAME-LEN5.
    CALL "NC_mk_path"
        USING WS-SHARE-BAK-DIR, WS-PATHNAME-LEN5, WS-BAK-FILE, WS-FILENAME-LEN5, WS-BAK-FILE, WS-FILENAME-LEN5.

*>
*>     CREATE FULL PATHNAME OF data_tm.bak IN WS-BAK-TEMPLATE
*>
    IF OPT-T THEN
        IF OPT-T-ARG = "-" THEN
            PERFORM 1400-SET-TEMPLATE-BAK
        ELSE
            MOVE OPT-T-ARG TO WS-BAK-TEMPLATE
        END-IF
        SET WS-LOAD-TEMPLATE TO TRUE
    ELSE
        PERFORM 1400-SET-TEMPLATE-BAK
    END-IF.

*>
*>     SPIT OUT PATHNAMES IF VERBOSE OPTION USED
*>
    IF OPT-V THEN
        MOVE WS-BAK-FILE TO WS-PATHNAME
        PERFORM 7300-PATH-LENGTH
        DISPLAY "Default load file:     '", WS-PATHNAME(1:WS-PATHNAME-LENGTH), "'"
    END-IF.

*>
*>     SEE IF THE DEFAULT LOAD FILE EXISTS.
*>
    MOVE "F" TO WS-PATHNAME-TYPE.
    MOVE WS-BAK-FILE TO WS-PATHNAME.
    PERFORM 6300-CHECK-EXISTS.
    IF RETURN-CODE = NC-RET-OK THEN
        SET WS-BAK-EXISTS TO TRUE
    ELSE
        SET WS-BAK-EXISTS TO FALSE
    END-IF.

    IF OPT-V THEN
        IF NOT WS-BAK-EXISTS THEN
            DISPLAY "  *** which does NOT exist ***"
        END-IF
    END-IF.

*>
*>     REPORT IF THE TEMPLATE FILE EXISTS.
*>
    IF OPT-V THEN
        MOVE WS-BAK-TEMPLATE TO WS-PATHNAME
        PERFORM 7300-PATH-LENGTH
        DISPLAY "Default template file: '", WS-PATHNAME(1:WS-PATHNAME-LENGTH), "'"
    END-IF.

    CALL "NC_check_path" USING WS-BAK-TEMPLATE, WS-PATHNAME-LEN5, "F".
    IF RETURN-CODE = NC-RET-OK THEN
        SET WS-BAK-TEMPLATE-EXISTS TO TRUE
    ELSE
    SET WS-BAK-TEMPLATE-EXISTS TO FALSE
    END-IF.

    IF OPT-V THEN
        IF NOT WS-BAK-TEMPLATE-EXISTS THEN
            DISPLAY "  *** which does NOT exist ***"
        END-IF
    END-IF.

*>
*>     USE THE DEFAULT LOAD FILE IF NONE SPECIFIED :
*>
    IF CMD-EOF AND WS-BAK-EXISTS THEN
        MOVE WS-BAK-FILE TO CMD-ARGUMENT
        PERFORM 7200-FIND-LENGTH
        SET CMD-EOF TO FALSE
    END-IF.

*>
*>     WE'RE UNDONE IF THERE ARE NO FILE NAMES GIVEN AS
*>     ARGUMENTS AND THERE IS NO DEFAULT INPUT FILE :
*>
    IF CMD-EOF THEN
        DISPLAY "SINCE THERE ARE NO COMMAND LINE SPECIFIED"
        DISPLAY "INPUT FILES AND THERE ISN'T ANY INSTALLED"
        DISPLAY "DEFAULT BACKUP FILE, THE INITIALIZATION"
        DISPLAY "CANNOT PROCEED."
        GO TO 9999-STOP
    END-IF.
EXIT.

1100-INIT-CMD-OPTIONS.
    INITIALIZE CMD-OPTIONS.
    SET OPT-F TO FALSE.
    SET OPT-I TO FALSE.
    SET OPT-V TO FALSE.
    SET OPT-H TO FALSE.
    SET OPT-B TO FALSE.
    SET OPT-D TO FALSE.
    SET WS-LOAD-TEMPLATE TO FALSE.
EXIT.

1200-CHECK-EXISTENCE.
    SET WS-DATABASE-EXISTS TO FALSE.
    SET WS-TODO-DEL-TEMPLATE TO FALSE.
    PERFORM VARYING WS-X FROM 1 BY 1 UNTIL WS-X > WS-INDEXED-COUNT
        MOVE WS-DATADIR TO WS-PATHNAME
        MOVE LENGTH OF WS-PATHNAME TO WS-PATHNAME-LEN5
        MOVE WS-INDEXED-FILE-NAME(WS-X) TO WS-FILENAME
        MOVE LENGTH OF WS-FILENAME TO WS-FILENAME-LEN5
    *>
    *>     CREATE A PATHNAME FROM DIR + FILE NAME
    *>
        CALL "NC_mk_path"
            USING WS-PATHNAME, WS-PATHNAME-LEN5, WS-FILENAME, WS-FILENAME-LEN5, WS-PATHNAME, WS-PATHNAME-LEN5

    *>
    *>     CHECK IF INDEXED FILE EXISTS AND IS A REGULAR FILE
    *>
        CALL "NC_check_path" USING WS-PATHNAME, WS-PATHNAME-LEN5, "F"
        IF RETURN-CODE = NC-RET-OK THEN
            SET WS-DATABASE-EXISTS TO TRUE
            IF OPT-V THEN
                MOVE WS-FILENAME TO WS-PATHNAME
                PERFORM 7300-PATH-LENGTH
                DISPLAY "  Found: ", WS-PATHNAME(1:WS-PATHNAME-LENGTH)
            END-IF
            IF WS-FILENAME = "TEMPLATE.X" THEN
                SET WS-TODO-DEL-TEMPLATE TO TRUE
                MOVE WS-PATHNAME TO WS-TEMPLATE-PATH
            END-IF
        END-IF
    END-PERFORM.
    EXIT.

1400-SET-TEMPLATE-BAK.
    MOVE LENGTH OF WS-SHARE-BAK-DIR TO WS-PATHNAME-LEN5
    MOVE LENGTH OF WS-BAK-TEMPLATE TO WS-FILENAME-LEN5
    CALL "NC_mk_path"
        USING WS-SHARE-BAK-DIR, WS-PATHNAME-LEN5, WS-BAK-TEMPLATE, WS-FILENAME-LEN5, WS-BAK-TEMPLATE, WS-FILENAME-LEN5.
    EXIT.

5000-PROCESS.
*>
*>     ASK ABOUT MISSING DATA DIRECTORY :
*>
    IF NOT WS-DATADIR-EXISTS THEN
        IF NOT OPT-F THEN
            MOVE WS-DATADIR TO WS-PATHNAME
            PERFORM 7300-PATH-LENGTH
            DISPLAY "THERE IS NO DIRECTORY HERE: ", WS-PATHNAME(1:WS-PATHNAME-LENGTH)
            DISPLAY "DO YOU WISH TO CREATE IT? "
            PERFORM 7400-GET-YNQ
            IF QUERY-Q OR QUERY-N THEN
                GO TO 9999-STOP
            END-IF
            MOVE 'Y' TO WS-TODO-CREATE-DATADIR-FLAG
        ELSE
            SET WS-TODO-CREATE-DATADIR TO TRUE
        END-IF
    ELSE
        SET WS-TODO-CREATE-DATADIR TO FALSE
    END-IF.

*>
*>     ASK ABOUT THE EXISTING COBCURSES DATABASE :
*>
    IF WS-DATABASE-EXISTS THEN
        PERFORM 5200-QUERY-DELETE
    END-IF

*>
*>     ASK ABOUT THE TEMPLATE INDEXED FILE :
*>
    IF WS-TODO-DEL-TEMPLATE THEN
        IF NOT OPT-F THEN
            MOVE WS-TEMPLATE-PATH TO WS-PATHNAME
            PERFORM 7300-PATH-LENGTH
            DISPLAY "DO WISH TO RECREATE THE TEMPLATE FILE: ", WS-PATHNAME(1:WS-PATHNAME-LENGTH)
            PERFORM 7400-GET-YNQ
            IF QUERY-Q THEN
                GO TO 9999-STOP
            END-IF
            IF QUERY-N THEN
                SET WS-TODO-DEL-TEMPLATE TO FALSE
            END-IF
        END-IF                    
    END-IF.
    PERFORM 5600-CONFIRM
*>
*>     CONFIRMATION HAVE BEEN MADE-- LET'S DO IT!
*>
    PERFORM 5100-LETS-DO-IT.
    EXIT.

5100-LETS-DO-IT.
*>
*>     SET THE COBCURSES_DATADIR ENVIRONMENT VARIABLE
*>
    MOVE LENGTH OF WS-ENV-COBCURSES-DATADIR TO WS-ENV-LEN5.
    MOVE WS-DATADIR TO WS-PATHNAME.
    PERFORM 7300-PATH-LENGTH.
    MOVE WS-PATHNAME-LENGTH TO WS-PATHNAME-LEN5.
    CALL "NC_setenv" USING WS-ENV-COBCURSES-DATADIR, WS-ENV-LEN5, WS-DATADIR, WS-PATHNAME-LEN5.

*>
*>     CREATE DATA DIRECTORY IF NECESSARY
*>
    IF WS-TODO-CREATE-DATADIR THEN
        PERFORM 5700-CREATE-DATADIR
    END-IF.

*>
*>     PROCESS COMMAND LINE FILE NAMES :
*>
    PERFORM 6100-PROCESS-ARGS

*>
*>     LOAD THE TEMPLATE INDEXED FILE IF WE HAVE
*>     DELETED IT AND HAVE THE INPUT FILE TO LOAD IT WITH.
*>
    IF WS-TODO-DEL-TEMPLATE OR WS-LOAD-TEMPLATE THEN
        IF WS-BAK-TEMPLATE-EXISTS THEN
            PERFORM 5800-LOAD-TEMPLATE-X
        END-IF
    END-IF.
    EXIT.

5200-QUERY-DELETE.
    IF OPT-V THEN
        DISPLAY " "
    END-IF.
    IF NOT OPT-F THEN
        MOVE WS-DATADIR TO WS-PATHNAME
        PERFORM 7300-PATH-LENGTH
        DISPLAY "THERE IS A DATABASE AT: ", WS-PATHNAME(1:WS-PATHNAME-LENGTH)
        DISPLAY "DO YOU WISH TO DELETE AND RECREATE IT? "
        PERFORM 7400-GET-YNQ
        IF QUERY-Q THEN
            GO TO 9999-STOP
        END-IF
        MOVE QUERY-YNQ-FLAG TO WS-TODO-DELETE-DB-FLAG
    ELSE
        SET WS-TODO-DELETE-DB TO TRUE
    END-IF.
    EXIT.

5410-DELETE-INDEXED.
    PERFORM VARYING WS-X FROM 1 BY 1 UNTIL WS-X > WS-INDEXED-COUNT
        MOVE WS-INDEXED-FILE-NAME(WS-X) TO WS-FILENAME
        MOVE LENGTH OF WS-FILENAME TO WS-FILENAME-LEN5
        CALL "NC_rm_file" USING WS-DATADIR, WS-PATHNAME-LEN5, WS-FILENAME, WS-FILENAME-LEN5
    END-PERFORM.
    EXIT.

5500-PROCESS-ARG.
*>
*>     CHECK THAT THE INPUT FILE EXISTS :
*>
    MOVE CMD-ARGUMENT TO WS-PATHNAME.
    MOVE LENGTH OF WS-PATHNAME TO WS-PATHNAME-LEN5.
    CALL "NC_check_path" USING WS-PATHNAME, WS-PATHNAME-LEN5, "F".
    IF RETURN-CODE = NC-RET-OK THEN
        IF WS-TODO-DELETE-DB THEN
            PERFORM 5510-DEL-DATABASE
        END-IF
        IF WS-TODO-DEL-TEMPLATE THEN
            PERFORM 5520-DEL-TEMPLATE
        END-IF
    ELSE
        PERFORM 7300-PATH-LENGTH
        DISPLAY "ERROR: Unable to open ", WS-PATHNAME(1:WS-PATHNAME-LENGTH)
        DISPLAY "ALL FURTHER PROCESSING SUSPENDED."
        SET CMD-EOF TO TRUE
    END-IF.

*>
*>     TELL SDLOAD ABOUT THE FILE BY ENV VARIABLE,
*>     AND ISSUE THE SDLOAD COMMAND :
*>
    PERFORM 7500-SET-INPUT-FILENAME.
    DISPLAY " ".
    DISPLAY "LOADING sdload ", CMD-ARGUMENT(1:CMD-ARG-LENGTH).
    CALL "SYSTEM" USING "sdload".
    EXIT.

5510-DEL-DATABASE.
    SET WS-TODO-DELETE-DB TO FALSE.
    PERFORM VARYING WS-X FROM 1 BY 1 UNTIL WS-X > 5
        MOVE WS-INDEXED-FILE-NAME(WS-X) TO WS-FILENAME
        PERFORM 5515-DEL-INDEXED-FILE
    END-PERFORM.
    EXIT.

5515-DEL-INDEXED-FILE.
    MOVE LENGTH OF WS-FILENAME TO WS-FILENAME-LEN5.
    MOVE LENGTH OF WS-DATADIR TO WS-PATHNAME-LEN5.
    CALL "NC_mk_path"
        USING WS-DATADIR, WS-PATHNAME-LEN5, WS-FILENAME, WS-FILENAME-LEN5, WS-PATHNAME, WS-PATHNAME-LEN5.
    CALL "NC_rm_file" USING WS-PATHNAME, WS-PATHNAME-LEN5.

    PERFORM 7300-PATH-LENGTH
    IF RETURN-CODE = NC-RET-OK AND OPT-V THEN
        DISPLAY "DELETED INDEXED FILE: ", WS-PATHNAME(1:WS-PATHNAME-LENGTH)
    END-IF.

    IF RETURN-CODE NOT = NC-RET-OK AND RETURN-CODE NOT = NC-RET-NOTFOUND THEN
        DISPLAY "WARNING: unable to delete ", WS-PATHNAME(1:WS-PATHNAME-LENGTH)
    END-IF.
    EXIT.

5520-DEL-TEMPLATE.
    SET WS-TODO-DEL-TEMPLATE TO FALSE
    MOVE WS-TEMPL-FILENAME TO WS-FILENAME.
    PERFORM 5515-DEL-INDEXED-FILE.
    EXIT.

5600-CONFIRM.
*>
*>     CONFIRM OPERATIONS TO BE PERFORMED :
*>
    IF NOT OPT-F AND ( WS-TODO-CREATE-DATADIR OR WS-TODO-DELETE-DB OR WS-TODO-DEL-TEMPLATE ) THEN
    *>
    *>     DON'T BOTHER WITH CONFIRM IF ONLY CONFIRMING THE
    *>     CREATE OF THE DIRECTORY, WHICH WAS ALREADY AGREED
    *>     TO EARLIER.
    *>
        IF WS-TODO-DELETE-DB OR WS-TODO-DEL-TEMPLATE THEN
            PERFORM 5610-CONFIRM-2
        END-IF
    END-IF.
    EXIT.

5610-CONFIRM-2.
    DISPLAY "-----------------------------".
    DISPLAY "PLEASE CONFIRM THE FOLLOWING:".
    DISPLAY "-----------------------------".
    IF WS-TODO-CREATE-DATADIR THEN
        DISPLAY "THE DATA DIRECTORY IS TO BE CREATED."
    END-IF.

    IF WS-TODO-DELETE-DB THEN
        DISPLAY "THE DATABASE IS TO BE DELETED AND RECREATED."
    END-IF.

    IF WS-TODO-DEL-TEMPLATE THEN
        DISPLAY "THE TEMPLATE INDEXED FILE IS TO BE DELETED AND RECREATED."
    END-IF.

    DISPLAY " ".
    DISPLAY "IS THIS CORRECT? ".
    PERFORM 7400-GET-YNQ.
    IF QUERY-Q OR QUERY-N THEN
        GO TO 9999-STOP
    END-IF.
    EXIT.

5700-CREATE-DATADIR.
*>
*>     CREATE THE MISSING DATA DIRECTORY (AND SUBDIRS) :
*>
    MOVE LENGTH OF WS-DATADIR TO WS-PATHNAME-LEN5.
    CALL "NC_mkdir_p" USING WS-DATADIR, WS-PATHNAME-LEN5.
    EXIT.

5800-LOAD-TEMPLATE-X.
    MOVE WS-BAK-TEMPLATE TO CMD-ARGUMENT.
    PERFORM 7200-FIND-LENGTH.
    PERFORM 7500-SET-INPUT-FILENAME.
    DISPLAY " ".
    DISPLAY "LOADING loadtm ", CMD-ARGUMENT(1:CMD-ARG-LENGTH)
    CALL "SYSTEM" USING "loadtm".
    EXIT.

6000-PROCESS-OPTIONS.
    PERFORM 7100-GET-ARG.
    PERFORM UNTIL CMD-EOF OR CMD-OPT-EOF OR NOT CMD-OPTION
        PERFORM 6200-PROCESS-OPTION
        IF NOT OPT-VALID THEN
            DISPLAY "ERROR: Unsupported command option -",OPT-CURRENT(1:OPT-CURRENT-LENGTH)
            MOVE 1 TO CMD-RETURN-CODE
        ELSE
            IF OPT-ARG-INVALID THEN
                DISPLAY "ERROR: Missing or invalid argument to -", OPT-CURRENT(1:OPT-CURRENT-LENGTH), " option."
                MOVE 1 TO CMD-RETURN-CODE
            END-IF
        END-IF
        PERFORM 7100-GET-ARG
    END-PERFORM.

*>
*>     IF PROCESSING ENDED ON A '--' TOKEN, THEN PREFETCH
*>     THE NEXT COMMAND LINE ARGUMENT
*>
    IF CMD-OPT-EOF THEN
        PERFORM 7100-GET-ARG
    END-IF.
    EXIT.

6100-PROCESS-ARGS.
    PERFORM UNTIL CMD-EOF
        PERFORM 5500-PROCESS-ARG
        PERFORM 7100-GET-ARG
    END-PERFORM.
    EXIT.

6200-PROCESS-OPTION.
    SET OPT-VALID TO FALSE.
    MOVE CMD-OPTION-NAME TO OPT-CURRENT.
    MOVE LENGTH OF OPT-CURRENT TO OPT-CURRENT-LENGTH.
    PERFORM UNTIL OPT-CURRENT-LENGTH = ZERO OR OPT-CURRENT(OPT-CURRENT-LENGTH:1) NOT = SPACE
        SUBTRACT 1 FROM OPT-CURRENT-LENGTH
    END-PERFORM.

    IF CMD-OPT-F THEN
        SET OPT-VALID TO TRUE
        SET OPT-F TO TRUE
        SET OPT-I TO FALSE
    END-IF.

    IF CMD-OPT-I THEN
        SET OPT-VALID TO TRUE
        SET OPT-I TO TRUE
        SET OPT-F TO FALSE
    END-IF.

    IF CMD-OPT-V THEN
        SET OPT-VALID TO TRUE
        SET OPT-V TO TRUE
    END-IF.

    IF CMD-OPT-H OR CMD-OPT-HELP THEN
        SET OPT-VALID TO TRUE
        SET OPT-H TO TRUE
    END-IF.

    IF CMD-OPT-B THEN
        SET OPT-VALID TO TRUE
        SET OPT-B TO TRUE
        PERFORM 7100-GET-ARG
        IF CMD-EOF OR CMD-ARGUMENT = SPACES THEN
            MOVE SPACES TO OPT-B-ARG
            SET OPT-ARG-INVALID TO TRUE
        ELSE
            MOVE CMD-ARGUMENT TO OPT-B-ARG
        END-IF
    END-IF.

    IF CMD-OPT-D THEN
        SET OPT-VALID TO TRUE
        SET OPT-D TO TRUE
        PERFORM 7100-GET-ARG
        IF CMD-EOF OR CMD-ARGUMENT = SPACES THEN
            MOVE SPACES TO OPT-D-ARG
            SET OPT-ARG-INVALID TO TRUE
        ELSE
            MOVE CMD-ARGUMENT TO OPT-D-ARG
        END-IF
    END-IF.

    IF CMD-OPT-T THEN
        SET OPT-VALID TO TRUE
        SET OPT-T TO TRUE
        PERFORM 7100-GET-ARG
        IF CMD-EOF OR CMD-ARGUMENT = SPACES THEN
            MOVE SPACES TO OPT-T-ARG
            SET OPT-ARG-INVALID TO TRUE
        ELSE
            MOVE CMD-ARGUMENT TO OPT-T-ARG
        END-IF
    END-IF.
    EXIT.

*>
*>     CHECK IF WS-PATHNAME EXISTS, OF WS-PATHNAME-TYPE
*>     ("D" OR "F"). RETURNS:
*>
*>     WS-PATHNAME-RC          RETURN-CODE
*>     WS-PATHNAME-LENGTH      ACTUAL PATH LENGTH
*>
6300-CHECK-EXISTS.
    MOVE LENGTH OF WS-PATHNAME TO WS-PATHNAME-LEN5.
    CALL "NC_check_path" USING WS-PATHNAME, WS-PATHNAME-LEN5, WS-PATHNAME-TYPE.
    MOVE RETURN-CODE TO WS-PATHNAME-RC.
    PERFORM 7300-PATH-LENGTH
    EXIT.

7000-OPEN-CMDLINE.
    INITIALIZE CMD-LINE.
    ACCEPT CMD-ARGC FROM ARGUMENT-NUMBER.
    MOVE ZERO TO CMD-N.
    SET CMD-EOF TO FALSE.
    EXIT.

7100-GET-ARG.
    IF NOT CMD-EOF THEN
        IF CMD-N >= CMD-ARGC THEN
            INITIALIZE CMD-ARG
            SET CMD-EOF TO TRUE
        ELSE
            ACCEPT CMD-ARG FROM ARGUMENT-VALUE
            ADD 1 TO CMD-N
            SET CMD-EOF TO FALSE
            PERFORM 7200-FIND-LENGTH
        END-IF
    END-IF.
    EXIT.

7200-FIND-LENGTH.
    MOVE LENGTH OF CMD-ARG TO CMD-ARG-LENGTH.
    PERFORM UNTIL CMD-ARG-LENGTH = ZERO OR CMD-ARGUMENT(CMD-ARG-LENGTH:1) NOT = SPACE
        SUBTRACT 1 FROM CMD-ARG-LENGTH
    END-PERFORM.
    EXIT.

7300-PATH-LENGTH.
    MOVE LENGTH OF WS-PATHNAME TO WS-PATHNAME-LENGTH
    PERFORM UNTIL WS-PATHNAME-LENGTH = ZERO OR WS-PATHNAME(WS-PATHNAME-LENGTH:1) NOT = SPACE
        SUBTRACT 1 FROM WS-PATHNAME-LENGTH
    END-PERFORM.
    EXIT.

7400-GET-YNQ.
    SET QUERY-VALID TO FALSE.
    PERFORM UNTIL QUERY-VALID
        DISPLAY "Y/N/Q? "
        ACCEPT QUERY-YNQ-FLAG
        MOVE FUNCTION UPPER-CASE(QUERY-YNQ-FLAG) TO QUERY-YNQ-FLAG
        IF QUERY-Y OR QUERY-N OR QUERY-Q THEN
            SET QUERY-VALID TO TRUE
        ELSE
            DISPLAY "PLEASE ANSWER WITH Y OR N OR Q BELOW:"
        END-IF
    END-PERFORM.
    EXIT.

7500-SET-INPUT-FILENAME.
    MOVE LENGTH OF WS-ENV-COBCURSES-SDLOAD-FILE TO WS-ENV-LEN5.
    MOVE CMD-ARG-LENGTH TO WS-PATHNAME-LEN5.
    CALL "NC_setenv" USING WS-ENV-COBCURSES-SDLOAD-FILE, WS-ENV-LEN5, CMD-ARGUMENT, WS-PATHNAME-LEN5.
    EXIT.            

8000-ISSUE-HELP.
    DISPLAY "init-cobc [{ -f | -i }] [-v] [-b dir] [-d dir] [-t file] [{ -h | --help }] files..".
    DISPLAY "where:".
    DISPLAY "        -i      Inquire about deleting existing data (default)".
    DISPLAY "        -f      Force: don't query about deleting existing data".
    DISPLAY "        -v      Verbose: produce verbose information messages".
    DISPLAY "        -b dir  Use dir instead of:".

    MOVE LENGTH OF WS-SHARE-BAK-DIR TO WS-PATHNAME-LENGTH.
    CALL "COBCURSES-INIT-PATHNAME" USING WS-SHARE-BAK-DIR, WS-PATHNAME-LENGTH. MOVE WS-SHARE-BAK-DIR TO WS-PATHNAME.
    PERFORM 7300-PATH-LENGTH.
    DISPLAY "                '", WS-SHARE-BAK-DIR(1:WS-PATHNAME-LENGTH), "'".

    DISPLAY "        -d dir  Use dir as the directory for the Indexed data files".
    DISPLAY "                instead of the default directory:".

    MOVE LENGTH OF WS-DATADIR TO WS-PATHNAME-LENGTH.
    CALL "COBCURSES-INIT-PATHNAME" USING WS-DATADIR, WS-PATHNAME-LENGTH. MOVE WS-DATADIR TO WS-PATHNAME.

    PERFORM 7300-PATH-LENGTH.
    DISPLAY "                '", WS-DATADIR(1:WS-PATHNAME-LENGTH), "'".

    DISPLAY "        -t file Use file as the input for the template file".
    DISPLAY "                instead of the default input.".

    DISPLAY "        -h      This help info.".
    DISPLAY " ".
    DISPLAY "        files.. One or more files created by the sdbackup utility.".
    DISPLAY " ".
    EXIT.

9000-FINALIZE.
    MOVE CMD-RETURN-CODE TO RETURN-CODE.
    EXIT.

9999-STOP.
    MOVE 1 TO RETURN-CODE.
    STOP RUN.

END PROGRAM INIT-COBC.
