#! /bin/ksh

#
# Name        : ${HOME}/bin/GetTLE.ksh
#
# Version     : 1.1
#
# Author      : Gerard van Wageningen
#
# Description : Read 'Tag Logical Element's from AFP file 
#
# 1.0   First Version
# 1.1	Allow for multiple Begin/End Document (BDT/EDT) records 
#

\whence set_debug 1>/dev/null 2>&1 && eval $( set_debug ${0##*/} )

typeset LINE=''

typeset TLEVarName=''
typeset TLEVarValue=''
typeset -i NumberOfPages=0
typeset -i NumberOfBxx=0
typeset FIRST_8_BYTES=''
typeset -i NumberOfBDT=0
typeset -i BNGSpotted=0

typeset BDT_STRING='BDT (Begin Document)'
typeset BNG_STRING='- BNG (Begin Named Page Group)'
typeset BNG_NAME_STRING='  Bxx: - name = '
typeset TLE_STRING='- - TLE (Tag Logical Element)'
typeset TLE_NAME_STRING='    TLE: - name = '
typeset TLE_VALUE_STRING='    TLE: - value = '
typeset BPG_STRING='- - BPG (Begin Page)'
typeset EPG_STRING='- - EPG (End Page)'
typeset ENG_STRING='- ENG (End Named Page Group)'
typeset EDT_STRING='EDT (End Document)'

if (( $# != 1 ))
then
  print -u2 "Usage: $0 <inputFile>"
  exit 1
fi

if [[ ! -r "$1" ]]
then
  print -u2 "Can not read file \"$1\", bailing out!"
  exit 1
fi

FIRST_8_BYTES=$( dd if="$1" bs=8 count=1 2>/dev/null )
if [[ "${FIRST_8_BYTES}" != Z* ]]
then
  print -u2 "File \"$1\" could not be a AFP file, bailing out!"
  exit 1
fi

if [[ ! -x /home/${PARENT_USER:-${USER}}/${UNAME:-$( uname -s )}/afpdmp ]]
then
  print -u2 "Can not read executable \"/home/${PARENT_USER:-${USER}}/${UNAME:-$( uname -s )}/afpdmp\", bailing out!"
  exit 1
fi

IFS='
'

# Skip the first 6 characters of each line

/home/${PARENT_USER:-${USER}}/${UNAME:-$( uname -s )}/afpdmp -d "$1" \
| cut -c7- \
| while read LINE
  do
    
    if (( NumberOfBDT == 0 ))
    then
      # Skip everything until first Begin DocumenT record
      if [[ "${LINE##${BDT_STRING}}" != "${LINE}" ]]
      then
      NumberOfBDT=1
        [[ -n "${DEBUG}" ]] && print -- "${BDT_STRING}"
      fi
      continue
    fi    

    case "${LINE}" in
      ${BDT_STRING}*)
        [[ -n "${DEBUG}" ]] && print -- "${BDT_STRING}"
	NumberOfBDT=NumberOfBDT+1
	print -- "----------- Document: ${NumberOfBDT} -----------"
        ;;

      ${BNG_STRING}*)
        [[ -n "${DEBUG}" ]] && print -- "${BNG_STRING}"
        NumberOfPages=0
        NumberOfBxx=0
	BNGSpotted=1
        ;;

      ${BNG_NAME_STRING}*)
        NumberOfBxx=NumberOfBxx+1
        if (( NumberOfBxx == 1 )) && (( BNGSpotted == 1 ))
        then
          print -- "PageGroup:"$( print -- "${LINE}" \
	    | sed -e "s/^[^\']*\'//g; s/\'$//g" )
        fi
	BNGSpotted=0
        ;;

      ${TLE_STRING}*)
	if (( BNGSpotted == 1 ))
        then
          BNGSpotted=0
          print -- "PageGroup:"
        fi

        #if [[ -n "${TLEVarName:-}" && -n "${TLEVarValue}" ]]
        if [[ -n "${TLEVarName:-}" ]]
        then
          print -- "- ${TLEVarName:-}='${TLEVarValue:-}'"
          TLEVarName=''
          TLEVarValue=''
        fi
        ;;

      ${TLE_VALUE_STRING}*)
        TLEVarValue="${TLEVarValue:+${TLEVarValue}}"$( print -- "${LINE}" \
	  | sed -e "s/^[^\']*\'//g; s/\'$//g" )
        ;;

      ${TLE_NAME_STRING}*)
        TLEVarName=$( print -- "${LINE}" | sed -e "s/^[^\']*\'//g; s/\'$//g" )
        ;;

      ${BPG_STRING}*)
        [[ -n "${DEBUG}" ]] && print -- "${BPG_STRING}"
        #if [[ -n "${TLEVarName:-}" && -n "${TLEVarValue}" ]]
        if [[ -n "${TLEVarName:-}" ]]
        then
          print -- "- ${TLEVarName:-}='${TLEVarValue:-}'"
          TLEVarName=''
          TLEVarValue=''
        fi
        NumberOfPages=NumberOfPages+1
        ;;

      ${EPG_STRING}*)
        [[ -n "${DEBUG}" ]] && print -- "${EPG_STRING}"
        ;;

      ${ENG_STRING}*)
        [[ -n "${DEBUG}" ]] && print -- "${ENG_STRING}"
        print -- "- NumberOfPages=${NumberOfPages}"
        ;;

      ${EDT_STRING}*)
        [[ -n "${DEBUG}" ]] && print -- "${EDT_STRING}"
        #exit 0
        ;;

      *)
        [[ -n "${DEBUG}" ]] && print -- "__${LINE}__"
        ;;
    esac

  done

exit 0

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

