process_tapes (extract files from tape and FTP them)

Description
#!/bin/ksh
#*************************************************************************
#* Script Name:      process_tapes 
#* Creation Date:    110398
#* Location:
#* Calling Routine:  NONE
#* Input Parameters: NONE
#* Description:      Process Property Tax Division tapes.  This involves
#*                   reading 8mm and QIC tapes, doing any needed data type 
#*                   conversions and then FTPing the resulting file to 
#*                   the mainframe for file server. 
#*                
#* Change Log
#* Name            Date   Change
#* --------------- ------ ------------------------------------------------
#* Paul McKinney   110398 Creation 
#* Paul McKinney   021401 Add logic to read and FTP Audit tapes.
#*
#*************************************************************************

typeset -u selection
typeset -u selection_yn
typeset -u label_yn
typeset -u fixed_block_yn
typeset -u ebcdic_yn
typeset -u ascii_yn
typeset -u node_suffix

initialization() {
   if [ $# -gt 0 ]
   then
      output_dir=$1
   else
      output_dir=/usr/tapes
   fi

   if [ ! -d "$output_dir" ]
   then
      print "$output_dir does not exist... exiting program."
      exit
   fi

   mvs_ip_address=mvs1.cpa.state.tx.us
   server_ip_address=prosrv1
   server_ftp_data_to="/vol2/users/public/sales_wd"
   eds_mvs_ftp_data_to="PROD.PTS.FTP.C"
   audit_mvs_ftp_data_to="USER.W30.CAS"
   number_of_tapes=1
   file_number=1
   fixed_block_yn=N
   label_yn=N
   media_type="8mm"
   device_name="/dev/rmt0"
}

main_menu() {
   clear
   print "\nMain Menu"
   print -- "---------\n"
   print "1) Sales Warantee Deed"
   print "2) Electronic Data Submission (EDS)"
   print "3) Audit"
   print "4) Utilities\n"
   print "0) Quit\n"

   read selection?"Enter selection: "
   
   case $selection in
      1) swd_menu;;
      2) eds_menu;;
      3) audit_menu;;
      4) util_menu;;
      0) exit;;
   esac
   main_menu
}

swd_menu() {
   process_type=SWD
   clear
   print "\nSWD Tape Menu"
   print --  "-------------\n"
   print "A)    Media Type: $media_type"
   print "B) Num. of Tapes: $number_of_tapes"
   print "C)    CAD Number: $CAD_number\n"

   print "1)     Record Length: $record_length"
   print "2)        Block Size: $input_blk_size"
   print "3) Fixed Block (y/n): $fixed_block_yn"
   print "4)       ASCII (y/n): $ascii_yn"
   print "5)     Labeled (y/n): $label_yn\n"

   print "7) Process Tape Using Parameters" 
   print "8) $media_type tar -xvf"
   print "9) FTP to Server\n"

   print "0) Main Menu\n"

   read selection?"Enter in selection: "

   case $selection in
      A) select_media_type;;
      B) print "\n";
         read number_of_tapes?"Enter number of tapes: ";;
      C) print "\n";
         read CAD_number?"Enter CAD number: ";;
      1) print "\n";
         read record_length?"Enter record length: ";;
      2) print "\n";
         read input_blk_size?"Enter input block size: ";;
      3) print "\n";
         read fixed_block_yn?"Fixed Block (y/n): ";;
      4) print "\n";
         read ascii_yn?"ASCII (y/n): ";
         case $ascii_yn in
            Y) conversion_type="";;
            *) conversion_type="ascii";;
         esac;;
      5) print "\n";
         read label_yn?"Labeled (y/n): ";;
      7) set_dd_params;
         last_menu=swd_menu;
         process_tapes;;
      8) last_menu=swd_menu;
         process_tar_tapes;;
      9) ftp_to_server;;
      0) main_menu;;
   esac
   swd_menu
}


eds_menu() {
   process_type=EDS
   clear
   print "\nEDS Tape Menu"
   print --  "-------------\n"
   print "A)    Media Type: $media_type"
   print "B) Num. of Tapes: $number_of_tapes"
   print "C)   Member Name: ${eds_mvs_ftp_data_to}${CAD_number}"
   print "D)    CAD Number: $CAD_number"
   print "                                   Form Selection"
   print "                                   --------------"
   print "1) $media_type EBCDIC labeled (ibs=31872)  #1"
   print "2) $media_type ASCII unlabeled (ibs=31872) #2" 
   print "3) $media_type ASCII unlabeled (ibs=7680)  #3"
   print "4) $media_type tar -xvf                    #4"
   print "5) OTHER\n"

   print "9) FTP to Mainframe\n"
   print "0) Main Menu\n"

   read selection?"Enter in selection: "

   case $selection in
      A) select_media_type;;
      B) print "\n";
         read number_of_tapes?"Enter number of tapes: ";;
      C) select_member_name;;
      D) print "\n";
         read CAD_number?"Enter CAD number: ";;
      1) input_blk_size=31872;
         record_length="";
         conversion_type="";
         label_yn=Y;
         set_dd_params;
         last_menu=eds_menu;
         process_tapes;;
      2) input_blk_size=31872;
         record_length="";
         conversion_type=ebcdic;
         label_yn=N;
         set_dd_params;
         last_menu=eds_menu;
         process_tapes;;
      3) input_blk_size=7680;
         record_length="";
         conversion_type=ebcdic;
         label_yn=N;
         set_dd_params;
         last_menu=eds_menu;
         process_tapes;;
      4) last_menu=eds_menu; 
         process_tar_tapes;;
      5) eds_special_menu;;
      9) mvs_ftp_data_to=${eds_mvs_ftp_data_to};
         ftp_to_mainframe;;
      0) main_menu;;
   esac
   eds_menu
}


eds_special_menu() {
   process_type=EDS
   clear
   print "\nEDS Special Tape Menu"
   print --  "---------------------\n"
   print "A)    Media Type: $media_type"
   print "B) Num. of Tapes: $number_of_tapes"
   print "C)    CAD Number: $CAD_number\n"

   print "1)     Record Length: $record_length"
   print "2)        Block Size: $input_blk_size"
   print "3) Fixed Block (y/n): $fixed_block_yn"
   print "4)      EBCDIC (y/n): $ebcdic_yn"
   print "5)     Labeled (y/n): $label_yn\n"

   print "8) Process Tape Using Parameters" 
   print "9) FTP to Mainframe\n"

   print "0) Main Menu\n"

   read selection?"Enter in selection: "

   case $selection in
      A) select_media_type;;
      B) print "\n";
         read number_of_tapes?"Enter number of tapes: ";;
      C) print "\n";
         read CAD_number?"Enter CAD number: ";;
      1) print "\n";
         read record_length?"Enter record length: ";;
      2) print "\n";
         read input_blk_size?"Enter input block size: ";;
      3) print "\n";
         read fixed_block_yn?"Fixed Block (y/n): ";;
      4) print "\n";
         read ebcdic_yn?"EBCDIC (y/n): ";
         case $ebcdic_yn in
            Y) conversion_type="";;
            *) conversion_type="ebcdic";;
         esac;;
      5) print "\n";
         read label_yn?"Labeled (y/n): ";;
      8) set_dd_params;
         last_menu=eds_special_menu;
         process_tapes;;
      9) ftp_to_mainframe;;
      0) main_menu;;
   esac
   eds_special_menu
}


audit_menu() {
   process_type=Audit
   clear
   print "\nAudit Tape Menu"
   print --  "---------------\n"
   print "A)    Media Type: $media_type"
   print "B) Num. of Tapes: $number_of_tapes"
   print "C)   Node Suffix: $node_suffix"
   print "     Member Name: ${audit_mvs_ftp_data_to}${node_suffix}\n"

   print "1)  Block Size: $input_blk_size"
   print "2) File Number: $file_number\n"

   print "3) Process Tape Using Parameters" 
   print "4) FTP to Mainframe\n"

   print "0) Main Menu\n"

   read selection?"Enter in selection: "

   case $selection in
      A) select_media_type;;
      B) print "\n";
         read number_of_tapes?"Enter number of tapes: ";;
      C) print "\n";
         read node_suffix?"Enter node suffix: ";;
      1) print "\n";
         read input_blk_size?"Enter input block size: ";;
      2) print "\n";
         read file_number?"File Number: ";;
      3) set_dd_params;
         last_menu=audit_menu;
         process_tapes;;
      4) last_menu=audit_menu; 
         mvs_ftp_data_to=${audit_mvs_ftp_data_to}${node_suffix};
         ftp_to_mainframe;;
      0) main_menu;;
   esac
   audit_menu
}


util_menu() {
   clear
   print "\nUtilities"
   print --  "---------\n"
   print "A) Media Type: $media_type\n"
   print "1) List contents of $output_dir"
   print "2) List available space on $output_dir"
   print "3) Clean out $output_dir"
   print "4) Perform head on TAPEDATA"
   print "5) Convert to ASCII"
   print "6) Chop into records"
   print "7) Perform tcopy on $media_type \n"
   print "0) Main Menu\n"

   read selection?"Enter in selection: "

   case $selection in
      A) select_media_type;;
      1) print;
         cd $output_dir;
         ls -l;
         print
         read selection?"Press [Enter] to return to menu: ";;
      2) print;
         cd $output_dir;
         df -k .;
         print;
         read selection?"Press [Enter] to return to menu: ";;
      3) print;
         cd $output_dir;
         if [ $? != 0 ]
         then
            exit
         fi;
         rm -i -r *;
         read selection?"File and subdirectory removal up done.  Press [Enter] to return to menu: ";;
      4) print;
         head -c 800 $output_dir/TAPEDATA;
         read selection?"Press [Enter] to return to menu: ";;
      5) print;
         dd if=$output_dir/TAPEDATA of=$output_dir/TAPEDATA.conv conv=ascii
         mv $output_dir/TAPEDATA.conv $output_dir/TAPEDATA
         read selection?"Press [Enter] to return to menu: ";;
      6) print;
         read chop_length?"Enter record length: ";
         dd if=$output_dir/TAPEDATA of=$output_dir/TAPEDATA.conv \
            cbs=$chop_length conv=unblock;
         mv $output_dir/TAPEDATA.conv $output_dir/TAPEDATA;
         read selection?"Press [Enter] to return to menu: ";;
      7) print;
         tcopy $device_name;
         read selection?"Press [Enter] to return to menu: ";;
      0) main_menu;;
   esac
   util_menu
}

set_dd_params() {

   case $record_length in
      "") dd_cbs="";;
      *) dd_cbs="cbs="$record_length;
         conversion_type=unblock;;
   esac

   if [ $fixed_block_yn = Y ]
   then
      conversion_type=unblock
   fi

   #conversion type
   case $conversion_type in
      ascii)    dd_conv="conv=ascii";;
      ebcdic)   dd_conv="conv=ebcdic";;
      unblock)  dd_conv="conv=unblock";;
      *)        dd_conv="";;
   esac

   #ibs
   case $input_blk_size in
      "") dd_ibs="";;
       *) dd_ibs="ibs="$input_blk_size;;
   esac

   #label
   if [[ $process_type = SWD || $process_type = EDS ]]
   then
      case $label_yn in
         Y) file_number=2;;
         *) file_number=1;;
      esac
   fi
   let fskip_file_number=file_number-1
   dd_fskip="fskip="${fskip_file_number}

}


process_tapes() {
   clear
   print "\nProcess $process_type Tapes"
   print -- "-----------------\n"

   let i=1
   while (($i <= $number_of_tapes))
   do

      read selection?"Insert tape #$i and press [Enter]: "
      output_file="$process_type""tape""$i"".dat" 
      print "Processing tape #$i ..."

      if [ number_of_tapes -eq 1 ] 
      then
         dd if=$device_name of=$output_dir/TAPEDATA $dd_ibs \
            $dd_cbs $dd_fskip $dd_conv
         rc=$?
         if [ $rc -ne 0 ]
         then
            print "ERROR in dd: Return Code: $rc"
            exit
         fi
         let i=i+1
      else
         dd if=$device_name of=$output_dir/$output_file $dd_ibs \
            $dd_cbs $dd_fskip $dd_conv
         rc=$?
         if [ $rc -ne 0 ]
         then
            print "ERROR in dd: Return Code: $rc"
            exit
         fi
         if [ $i -eq 1 ]
         then
            print "moving $output_file to file TAPEDATA ..."
            mv $output_dir/$output_file $output_dir/TAPEDATA
         else
            print "Concatinating file $output_file to file TAPEDATA ..."
            cat $output_dir/$output_file >> $output_dir/TAPEDATA 
            print "Removing file $output_file ...\n"
            cd $output_dir
            rm -f $output_file
         fi
         let i=i+1
      fi

   done

   read selection?"Processing done.  Press [Enter] to return to menu: "
   $last_menu
}


process_tar_tapes() {
   clear
   print "Process TAR Tapes"
   print -- "-----------------\n"
   print "Output Directory: $output_dir"
   cd $output_dir

   read selection_yn?"Need to clear output directory... continue (y/n): "
   if [ $selection_yn = Y ]
   then
      rm -i -r -v *;
   else
      $last_menu 
   fi

   let i=1
   while (($i <= $number_of_tapes))
   do
      read selection?"Place tape$i in drive and press [Enter]: "
      print "unTARing tape$i from $device_name ..."
      tar -xvf $device_name
      let i=i+1
   done

   for file in $(ls)
   do
      if [ -f "$file" -a ! "$file" = TARTAPEDATA ]
      then
         print "Concatinating $file to TARTAPEDATA..."
         cat $file >> TARTAPEDATA
         rm -f -v $file
      fi
   done

   print
   
   if [ $last_menu = "eds_menu" ]
   then
      print "Converting TAPEDATA to ebcdic and renaming file to TAPEDATA\n"
      dd if=$output_dir/TARTAPEDATA of=$output_dir/TAPEDATA conv=ebcdic
      rm -f -v TARTAPEDATA
   else
      mv $output_dir/TARTAPEDATA  $output_dir/TAPEDATA
   fi

   print
   read selection?"Tar processing done.  Press [Enter] to return to menu: "
   $last_menu
}

ftp_to_server() {
   clear
   print "\nFTP To Server"
   print -- "-------------\n"
   read userid?"What is your server user ID?: "
   read selection_yn?"Are you ready to FTP the TAPEDATA file (y/n)?: "
   if [ $selection_yn = Y ]
   then
      put_statement="put ""$output_dir""/TAPEDATA ""$server_ftp_data_to""/SWD$CAD_number"".txt"
      ftp -n $server_ip_address <<- EOF
	user $userid
        hash
        $put_statement
        bye
	EOF
      print "FTP file transfer complete. If there was a problem with the ftp"
      print "the FTP process can be run again.\n"
   fi
   read selection?"Press [Enter] to return to menu: "
   swd_menu
}

ftp_to_mainframe() {
   clear
   print "\nFTP To Mainframe"
   print -- "----------------\n"
   read userid?"What is your mainframe user ID?: "
   read selection_yn?"Are you ready to FTP the TAPEDATA file (y/n)?:"
   if [ $selection_yn = Y ]
   then
      put_statement="put ""$output_dir""/TAPEDATA ""$mvs_ftp_data_to""$CAD_number"
      ftp -vn $mvs_ip_address <<- EOF
	user $userid
	binary
        hash
        cd ..
        $put_statement
        bye
	EOF
      print "FTP file transfer complete. If there was a problem with the ftp"
      print "the FTP process can be run again.\n"
   fi
   read selection?"Press [Enter] to return to menu: "
   $last_menu
}

select_media_type() {
   clear
   print "\nMedia Type"
   print -- "----------\n"
   print "2) Quarter Inch Cartrige (QIC)\n"
   read selection?"Enter choice: "
   case $selection in
     1) media_type="8mm";
        device_name="/dev/rmt0";;
     2) media_type="QIC";
        device_name="/dev/rmt1";;
   esac
}


select_member_name() {
   clear
   print "\nMember Name"
   print -- "----------\n"
   print "1) PROD.PTS.FTP.C"
   print "2) TEST.PTS.FTP.C\n"
   read selection?"Enter choice: "
   case $selection in
      1) eds_mvs_ftp_data_to="PROD.PTS.FTP.C";;
      2) eds_mvs_ftp_data_to="TEST.PTS.FTP.C";;
   esac
}

select_conversion_type() {
   clear
   print "\nConversion Type"
   print -- "---------------\n"
   print "1) ascii"
   print "2) ebcdic"
   print "3) unblock"
   print "4) none\n"

   read selection?"Enter choice: "

   case $selection in
      1) conversion_type=ascii;;
      2) conversion_type=ebcdic;;
      3) conversion_type=unblock;;
      4) conversion_type=none;;
   esac
}

initialization $1
main_menu