$ ! -----------------------------------------------------
$ ! Script   : Readfdyn.com
$ ! Function : Script to read input CPST file entered via command line
$ !            & records extracted on the basis on conditions & 
$ !            transaction types that are pre-defined. 
$ ! Date     : 11/05/2004
$ ! Author   : Ramki
$ ! -----------------------------------------------------
$ if (P1 .eqs. "")
$ then
$  write sys$output "''esc'[1mProcedure SCRIPT to Read INPUT CPST 
$  write sys$output "file and Extract Fields based on Inputs entered
$  write sys$output "''esc'[m
$  write sys$output "USAGE:''esc'[4m@readfdyn <Input CPST File Name>
$  write sys$output "''esc'[m
$  exit
$ ENDIF

$ OPEN/READ INPUT_FILE &P1

$ ! First Check if INPUT file is a valid CPST file or not.
$ ! If not exit, else continue.

$ SW = 0

$ ifilecheck:
$ read/end_of_file=reopenfile input_file NUM
$ str = f$extract(15,4,NUM)
$ !show symbol str
$ if ( SW .EQS. 0)
$ then
$ if ( str .EQS. "AA00" ) .AND. ( f$extract(19,1,NUM) .EQS. " ")
$ then
$     SW = 1
$     goto ifilecheck
$ else
$     write sys$output "Not a valid CPST file ..no header record"
$     close input_file
$     exit
$ endif
$ endif
$ if ( str .EQS. "ZZ99" ) .AND. ( f$extract(19,1,NUM) .EQS. " ")
$ then
$     SW = 2
$     goto ifilecheck
$ endif
$ goto ifilecheck
$ reopenfile:
$ close input_file
$ if ( SW .eqs. 0 )
$ then
$     write sys$output "Input File does not have any records
$     exit
$ else
$ if ( SW .eqs. 1 )
$ then
$     write sys$output "Input CPST file does not have trailer record
$     write sys$output "hence cannot proceed ahead
$     exit
$ else
$ if ( SW .eqs. 2 )
$ then
$     SW = 0
$ endif

$ !Accept Record Transaction types : RA40,IC43 etc....

$ Write sys$output "Enter 'ALL' for extracting all Transaction types for a particular customer
$ Write sys$output "Valid Transaction types are : RA40,RA41 etc....
$ INQUIRE/GLOBAL RT "Enter Transaction Type :

$ IF ( RT .eqs. "ALL" )
$ then
$ GET_CUSTOMER:
$     INQUIRE/GLOBAL CUSTOMER_NUMBER "Enter Customer # : 
$     IF ( CUSTOMER_NUMBER .EQS. "") .OR. ( CUSTOMER_NUMBER .EQS. 0 ) .OR. ( f$length(CUSTOMER_NUMBER) .lt. 10 )
$     then
$         goto GET_CUSTOMER
$     endif
$ endif

$ Open/read input_file &P1 
 
$ INQUIRE/GLOBAL SP "Start Position-1 :
$ INQUIRE/GLOBAL SP_LENGTH "Field Length-1 :

$ INQUIRE/GLOBAL EP "Start Position-2 :
$ INQUIRE/GLOBAL EP_LENGTH "Field Length-2 :

$ OPEN/WRITE OUTPUT_FILE Temp.txt

$ count = 0

$ READ_LOOP:
$ READ/END_OF_FILE=ENDIT INPUT_FILE NUM
$ ! First get to the first transaction record, by bypassing the header.

$ str = f$extract(15,4,NUM)
$ if ( SW .EQS. 0)
$ then
$ if ( str .EQS. "AA00" ) .AND. ( f$extract(19,1,NUM) .EQS. " ")
$ then
$     SW = 1
$     goto READ_LOOP
$ endif
$ endif
$ if ( str .EQS. "ZZ99" ) .AND. ( f$extract(19,1,NUM) .EQS. " ")
$ then
$     goto READ_LOOP
$ endif

$ ! NOW Start the check for the transaction type here
$ if ( RT .eqs. "ALL" ) .OR. ( RT .eqs. "all" )
$ then
$     if ( f$extract(82,10,NUM) .eqs. CUSTOMER_NUMBER )
$     then
$         goto Record_Process
$     else
$         goto READ_LOOP
$     endif
$ endif

$ if ( str .EQS. RT )
$ then
$     if ( f$extract(15,4,NUM) .EQS. str )
$     then
$        goto Record_Process
$     else
$        goto READ_LOOP
$     endif
$ else
$     goto READ_LOOP
$ endif

$ Record_Process: 
$ FIRST_CHAR = F$EXTRACT(&SP,&SP_LENGTH,NUM)
$ ! WRITE SYS$OUTPUT FIRST_CHAR - If one field in each line

$ if ( EP .EQS. "" )
$ then
$      write OUTPUT_FILE FIRST_CHAR
$ else
$      SECOND_CHAR = F$EXTRACT(&EP,&EP_LENGTH,NUM)
$      WRITE OUTPUT_FILE FIRST_CHAR," | ", SECOND_CHAR
$ endif

$ count = count + 1
$ GOTO READ_LOOP
$ IFILE_ERROR:
$    WRITE SYS$OUTPUT "Input File Cannot be Opened
$    EXIT
$ ENDIT:
$    CLOSE INPUT_FILE
$    CLOSE OUTPUT_FILE
$    write sys$output "No of Records ",count
$    write sys$output "''esc'[1mCheck Output in File : 'temp.txt'"
$    write sys$output "''esc'[m
$ EXIT
