#! /bin/ksh

#
# Name        : ${HOME}/bin/ReadInd.ksh
#
# Version     : 1.0
#
# Author      : Gerard van Wageningen
#
# Description : Interpret ascii .ind files generated by OnDemand 
#		"arsdoc get" command.
#
# 1.0   First Version
#

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

typeset LINE=''
typeset NAME=''
typeset FIRST_8_BYTES=''

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 be a AFP file, bailing out!"
  exit 1
fi

IFS='
'

while read LINE
do
  case "${LINE}" in
    CODEPAGE:*) print "CodePage: ${LINE#*:}" ;;
    GROUP_FIELD_NAME:*) NAME="${LINE#*:}" ;;
    GROUP_FIELD_VALUE:*) print -- "-${NAME}='${LINE#*:}'" ;;
    GROUP_OFFSET:*) print "FileOffset: ${LINE#*:}" ;;
    GROUP_LENGTH:*) print "RecordLength: ${LINE#*:}" ;;
    GROUP_FILENAME:*) print "File: ${LINE#*:}" ;;
    *) ;;
  esac
done < "$1"

exit 0

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

