#! /bin/ksh

#
# Name        : ${HOME}/bin/AddDrawer5And6.ksh
#
# Version     : 1.0
#
# Author      : Gerard van Wageningen
#
# Description : Script to add 8 AFP Medium Maps to f1eurver file.
#
# 1.0   First Version
#

set -e # Go exit when an error occurs

typeset INPUT='f1eurver'
typeset LINE=''
typeset NAME='0START'
typeset -i teller=0
typeset DIR=''
typeset FILE=''
typeset i=''
typeset j=''

# Add directory to PATH for AFPSplit, afpdmp, x2b and b2x executables
case "${PATH}" in
  */home/gerardvw/AIX*) ;;
  *) [[ -d '/home/gerardvw/AIX' ]] && PATH="${PATH}:/home/gerardvw/AIX" ;;
esac

# Remove all underlying directories
ls | while read DIR
do
  [[ -d $DIR ]] && rm -rf $DIR
done

# Remove everiting else
> 5.$$
rm -rf 5* *000000* 9* 1>/dev/null 2>&1

#  We will add the following Medium Maps:
#  5F2DUPL5  5F2DUPL6
#  5F2DPJOG5 5F2DPJOG6
#  5F2SIMPL5 5F2SIMPL6
#  5F2SPJOG5 5F2SPJOG6

# Split the f1euver file in 1 file a record. The output files are named like:
# "${INPUT}.0000000000000000"
# "${INPUT}.0000000000000001"
# ...
AFPSplit "${INPUT}"

# Put every Medium Map in its own directory. Start with the directory 0START
# to save all files before the first BMM (Begin Medium Map) record and save
# all trailing record in 9END. The IBM program afpdump produces the following
# output:
#...1  BFM (Begin Form Map)                               SFI 0008 D3A8CD 000001
#...7  - BMM (Begin Medium Map)             NAME F2DPJOG1 SFI 0010 D3A8CC 000059
#...8  - - BFG (Begin Form Environ Group)                 SFI 0008 D3A8C5 00005A
#...9  - - - FGD (Form Environ Group Descrip)             SFI 000C D3A6C5 00005B
#..10  - - - PGP1 (Page Position - 1)                     SFI 000E D3ACAF 00005C
#..11  - - - MDD (Medium Descriptor)                      SFI 0018 D3A688 00005D
#..12  - - - MCC (Medium Copy Count)                      SFI 0014 D3A288 00005E
#..13  - - - MMC (Medium Modification Cntl)               SFI 0016 D3A788 00005F
#..14  - - - MMC (Medium Modification Cntl)               SFI 0016 D3A788 000060
#..15  - - EFG (End Form Environ Group)                   SFI 0008 D3A9C5 000061
#..16  - EMM (End Medium Map)               NAME F2DPJOG1 SFI 0010 D3A9CC 000062
#..17  - BMM (Begin Medium Map)             NAME F2DPJOG2 SFI 0010 D3A8CC 00006D
#...
#..89  - BMM (Begin Medium Map)             NAME F2SIMPL1 SFI 0010 D3A8CC 00005A
#..97  - EMM (End Medium Map)               NAME F2SIMPL1 SFI 0010 D3A9CC 000062
#...
#1148  - EMM (End Medium Map)               NAME FVCFVA   SFI 0010 D3A9CC 00001D
#1149  EFM (End Form Map)                                 SFI 0008 D3A9CD 00009F
afpdmp "${INPUT}" | while read LINE
do
  case "${LINE}" in
    *\(Begin\ Medium\ Map\)*)
      NAME=$( print ${LINE} | awk '{print $8}' )
      print "Found: ${NAME}"
      NAME="5${NAME}"
      ;;
    *\(End\ Form\ Map\)*)
      NAME='9END'
      ;;
    *) ;;
  esac

  # Create the target directory to move the AFP records to.
  [[ ! -d "${NAME}" ]] && mkdir "${NAME}"

  mv *0000${teller} "${NAME}"
  teller=teller+1
done

# Add drawers 5 and 6
for j in 5 6
do
  # First the ones with an 8 characters name
  for i in 5F2SIMPL 5F2SPJOG 5F2DPJOG
  do
    # Copy records from the drawer 4 record to the new directories
    [[ -d $i$j ]] && rm -rf $i$j
    mkdir $i$j
    cp ${i}4/* $i$j

    # Change the name in the Begin Medium Map record
    # The 17th character is the EBCDIC representation of "4"
    #...1  BMM (Begin Medium Map)               NAME F2DUPL4  SFI 0010 D3A8CC 00008B
    #              .....0 5A00 10D3 A8CC 0000 8BC6 F2C4 E4D7 D3F4 |!..Ly...{F2DUPL4|
    #              ....10 40                                      |                |
    FILE=$( ls $i$j | head -1 )
    b2x $i$j/$FILE | sed -e "17s/f4/F${j}/" | x2b > $i$j/$FILE.$$
    mv $i$j/$FILE.$$ $i$j/$FILE

    # Change the name in the End Medium Map record
    #...1EMM (End Medium Map)                   NAME F2DUPL4  SFI 0010 D3A9CC 000094
    #              .....0 5A00 10D3 A9CC 0000 94C6 F2C4 E4D7 D3F4 |!..Lz...mF2DUPL4|
    #              ....10 40                                      |                |
    FILE=$( ls $i$j | tail -1 )
    b2x $i$j/$FILE | sed -e "17s/f4/F${j}/" | x2b > $i$j/$FILE.$$
    mv $i$j/$FILE.$$ $i$j/$FILE
  done

  for i in 5F2DUPL
  do
    # Copy records from the drawer 4 record to the new directories
    [[ -d $i$j ]] && rm -rf $i$j
    mkdir $i$j
    cp ${i}4/* $i$j

    # Change the name in the Begin Medium Map record, the 16th character,
    # the 17th is a space character.
    FILE=$( ls $i$j | head -1 )
    b2x $i$j/$FILE | sed -e "16s/f4/F${j}/" | x2b > $i$j/$FILE.$$
    mv $i$j/$FILE.$$ $i$j/$FILE

    # Change the name in the End Medium Map record
    FILE=$( ls $i$j | tail -1 )
    b2x $i$j/$FILE | sed -e "16s/f4/F${j}/" | x2b > $i$j/$FILE.$$
    mv $i$j/$FILE.$$ $i$j/$FILE
  done

  # Change the MMC drawer
  for i in 5F2SIMPL 5F2SPJOG
  do
    # The length of 16 bytes is set to 18 bytes, the 16 byte is the drawer
    # tuple, prepend the format record and change the original 17th byte to
    # the new drawer number.
    #...1  MMC (Medium Modification Cntl)                     SFI 0016 D3A788 000092
    #              .....0 5A00 16D3 A788 0000 9202 FF0E 00D1 00E1 |!..Lxh..k....J..|
    #              ....10 04F1 00F4 02F8 FF                       |.1.4.8.         |
    #      MMC: identifier 2 (02)
    #      MMC: max horizontal adjustment 0 (00)
    #      MMC: do not do offset stacking or change edge marks
    #      MMC: >invalid medium source selector (04)<
    #      MMC: forms flash is not active
    #      MMC: normal duplex
    #      MMC: print quality control 255 (FF)
    FILE=$( ls $i$j | tail -3 | head -1 )
    b2x $i$j/$FILE \
    | sed -e "3s/16/18/g; 16s/e1/E0 02 E1/g; 17s/04/0${j}/g" \
    | x2b > $i$j/$FILE.$$
    mv $i$j/$FILE.$$ $i$j/$FILE
    # Different record layout f1eurver and f1eurver.fde (Xenos)
    #| sed -e "15s/01/02/; 17s/04/0${j}/g" \
  done

  # Change the 2 MMC drawer in duplex Medium Maps
  for i in 5F2DPJOG 5F2DUPL
  do
    for FILE in $( ls $i$j | tail -4 | head -2 )
    do
      b2x $i$j/$FILE \
      | sed -e "3s/16/18/g; 16s/e1/E0 02 E1/g; 17s/04/0${j}/g" \
      | x2b > $i$j/$FILE.$$
      mv $i$j/$FILE.$$ $i$j/$FILE
      # Different record layout f1eurver and f1eurver.fde (Xenos)
      #| sed -e "15s/01/02/; 17s/04/0${j}/g" \
   done
  done
done

# Concat all records (sorts the Medium Maps) 
cat */* > "${INPUT}.new"

exit 0

################ EOF ################
