#! /bin/ksh

#
# Name        : ${HOME}/bin/GetMediumMaps.ksh
#
# Version     : 1.0
#
# Author      : Gerard van Wageningen
#
# Description : Read AFP records produced by afpdmp -d and extract 
#               Medium Maps. Start their names with 5 to 
#
# 1.0   First Version
#

set -e	# Go exit in case of error

typeset INPUT="$1"
typeset LINE=''
typeset NAME='0START'
typeset -i teller=0

AFPSplit "${INPUT}"	# Split the AFP file in invidual records  

# Read the type of each record and skip all record till the first
# BMM record. Stop saving the records after reading the EFM record.   
afpdmp "${INPUT}" \
| while read LINE
  do
    case "${LINE}" in
      *\(Begin\ Medium\ Map\)*)
        NAME=$( print ${LINE} | awk '{print $10}' )
        print "Found: ${NAME}"
        NAME="5${NAME}"
        ;;
      *\(End\ Form\ Map\)*)
        NAME='9END'
        ;;
      *) ;;
    esac

    if [[ "${NAME}" = 5* ]]
    then
      [[ ! -d "${NAME}" ]] && mkdir "${NAME}"
      mv *0000${teller} "${NAME}"
    else
      rm *0000${teller}
    fi

    teller=teller+1
  done

# cat 5*/* | afpdmp -d -x

exit 0

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