IDENTIFICATION DIVISION.
PROGRAM-ID. UIFMOVE.
AUTHOR. Barry L. Wallis
DATE-WRITTEN. 15-Jul-1986
*
*   This program will move a menu structure from one version of the UIF to 
*   another.
*
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SOURCE-COMPUTER.
    VAX.
OBJECT-COMPUTER.
    VAX.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT old-master-file
	ASSIGN TO "old_master_file"
	ORGANIZATION IS INDEXED
	ACCESS MODE IS DYNAMIC
	RECORD KEY IS master-primary-key of old-master-file.
    SELECT new-master-file
	ASSIGN TO "new_master_file"
	ORGANIZATION IS INDEXED
	ACCESS MODE IS RANDOM
	RECORD KEY IS master-primary-key of new-master-file.

DATA DIVISION.
FILE SECTION.
FD old-master-file.
COPY "cdd_uif.master_record" FROM DICTIONARY.
FD new-master-file.
copy "cdd_uif.master_record" FROM DICTIONARY.
WORKING-STORAGE SECTION.
01.
    03	ws-MASTER_RECORD occurs 1 to 20 times depending on depth.
	05  MASTER_PRIMARY_KEY.
	    10  MASTER_RECORD_NAME		PIC X(9).
	    10  MASTER_RECORD_TYPE		PIC X(1).
	05  MASTER_RECORD_ACCESS_COUNT	COMP	PIC S9(4).
	05  MASTER_TRANSACTION.
	    10  MASTER_TRANSACTION_DESCRIPTION	PIC X(60).
	    10  MASTER_TRANSACTION_TYPE		PIC X(1).
	    10  MASTER_TRANSACTION_FORM		PIC X(31).
	    10  FILLER REDEFINES MASTER_TRANSACTION_FORM.
		15  FILLER			PIC X(1).
	    10  MASTER_TRANSACTION_SWITCH	PIC X(31).
	    10  FILLER				PIC X(139).
	05  MASTER_MENU REDEFINES MASTER_TRANSACTION.
	    10  MASTER_MENU_TITLE		PIC X(20).
	    10  MASTER_MENU_DESCRIPTION		PIC X(60).
	    10  MASTER_MENU_NODE_CNT	COMP	PIC S9(4).
	    10  MASTER_MENU_NODE		PIC X(9)
		OCCURS 20 TIMES.
01 depth
    PIC s9(4)
    USAGE IS COMP.
01  invalid-key-sw
    PIC x(1).
    88 invalid-key VALUE IS "T".
    88 not-invalid-key VALUE IS "F".
01.
    05 cnt
	PIC s9(4)
	USAGE IS COMP
	OCCURS 1 TO 20 TIMES DEPENDING ON depth.
01 hold-cnt
    PIC s9(4)
    USAGE IS COMP.
01  replace-sw
    PIC x(1).
    88 replace-it VALUE IS "Y" "y".

PROCEDURE DIVISION.
in-the-begining.
    OPEN INPUT old-master-file
    OPEN I-O new-master-file.
do-it.
    DISPLAY "Top level menu at plant: " WITH NO ADVANCING
    ACCEPT master-record-name of old-master-file
	AT END
	    GO TO and-in-the-end
    	END-ACCEPT 
    IF master-record-name of old-master-file = spaces
    THEN
    	GO TO and-in-the-end
    END-IF.
    MOVE "M" TO master-record-type of old-master-file 
    MOVE 1 TO depth
    READ old-master-file RECORD INTO ws-master-record(depth) 
	INVALID KEY
	    DISPLAY "Top level menu not found"
	    GO TO do-it
	END-READ
    DISPLAY "Replace existing items [N]? " WITH NO ADVANCING
    ACCEPT replace-sw
    PERFORM process-menu-record THRU process-menu-exit
    GO TO do-it.

and-in-the-end.
    CLOSE old-master-file
    CLOSE new-master-file.
    STOP RUN.

process-menu-record.
    PERFORM write-master-record
    IF invalid-key
    THEN
	IF depth > 1
	THEN
	    ADD 1 TO cnt(depth - 1)
	ELSE
	    GO TO process-menu-exit
	END-IF
    ELSE
	ADD 1 TO depth
	MOVE 1 TO cnt(depth - 1)
    END-IF.
process-nodes.
    IF cnt(depth - 1) > master-menu-node-cnt of ws-master-record(depth - 1)
    THEN
        SUBTRACT 1 FROM depth
        IF depth > 1
        THEN
	    ADD 1 TO cnt(depth - 1)
	    GO TO process-nodes
	END-IF
    ELSE
	MOVE cnt(depth - 1) TO hold-cnt
	MOVE master-menu-node of ws-master-record(depth - 1,hold-cnt)
	    TO master-record-name of old-master-file
	MOVE spaces TO master-record-type of old-master-file
	START old-master-file KEY > master-primary-key of old-master-file
	    INVALID KEY
		STOP "Internal error: Key not on file"
	    END-START
	READ old-master-file NEXT RECORD 
	    INTO ws-master-record(depth)
	    AT END
		STOP "Internal error: Unexpected EOF"
	    END-READ
	IF master-record-type of ws-master-record(depth) = "T"
	THEN
	    PERFORM write-master-record
	    ADD 1 TO cnt(depth - 1)
	    GO TO process-nodes
	ELSE
	    GO TO process-menu-record
	END-IF
    END-IF.
process-menu-exit.
    EXIT.

write-master-record.
    SET not-invalid-key TO TRUE
    WRITE master-record of new-master-file FROM ws-master-record(depth)
	INVALID KEY
	    IF replace-it
	    THEN
		REWRITE master-record of new-master-file 
		    FROM ws-master-record(depth)
		    INVALID KEY
			DISPLAY "Internal error in rewrite"
			SET invalid-key TO TRUE
		    END-REWRITE
	    ELSE
		DISPLAY master-record-name of ws-master-record(depth) 
		    " already on file (ignoring)"
		SET invalid-key TO TRUE
	    END-IF
	END-WRITE.

END PROGRAM uifmove.
