#! /bin/bash
#
#  This script converts the ASCII version of the manual saved by AmiPro
#  into a reasonably acceptable form of Texinfo.  The output of this program
#  is fed into another program which inserts texinfo node and menu infomation.
#

#set -x

#rm -f *.txt
orig=/usr1/home/joel/tmp/doc/relnotes
inputfiles=`cd $orig ; echo *.txt`

for i in $inputfiles
do
    echo $i
    out=`echo $i | sed -e 's/\.txt$/.texi/'`
    # 1.  Remove <ctl>-Z and <ctl>-M
    # 2.  Tackle paragraph style issues
    # 3.  Directive status code lines

    tr -d '\032\015' <${orig}/$i |
      sed -e 's/<Topic Lvl 0>/@chapter /'  |
      sed -e 's/<Topic Lvl 1>/@section /'  |
      sed -e 's/<Topic Lvl 2>/@subsection /'  |
      sed -e 's/<Topic Lvl 3>/@subsection /'  |
      sed -e 's/<Body Text>//'  |
      sed -e 's/<Directive Tbl>/@item /'  |
      sed -e 's/<Table Title>/@itemize /'  |
      sed -e 's/<Bullet>/@item /'  |
      sed -e 's/<Bullet 2>/@item /'  |
      sed -e 's/<Table Text>/@item /'  |
      sed -e 's/<Number List>/@item /'  |
      sed -e 's/<Time Desc>/@item /'  |
      while read line 
      do 
        case $line in
          "<C Code Exampl>"*"{") echo "@example"; echo "$line" ; read line;;
          "<C Code Exampl>"*"(") echo "@example"; echo "$line" ; read line;;
          "<C Code Exampl>"*");") echo "$line" ; echo "@end example" ;;
          "<C Code Exampl>"*"}"*";") echo "$line" ; echo "@end example" ;;
          "<C Code Exampl>"*",") echo "$line" ; read line ;;
          "<C Code Exampl>"*";") echo "$line" ; read line ;;
          *) echo "$line" ;;
        esac
      done | 
      sed -e 's/<C Code Exampl>//'  |
      sed -e 's/<Directive Tbl>/@item /'  |
      sed -e 's/<Topic>/@subheading /'  |
      sed -e 's/<Directive>/@page\
@subsection /'  |
      sed -e 's/<Status Codes>//'  |
      sed -e 's/^\(SUCCESSFUL\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(TASK_EXITTED\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(MP_NOT_CONFIGURED\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(INVALID_NAME\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(INVALID_ID\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(TOO_MANY\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(TIMEOUT\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(OBJECT_WAS_DELETED\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(INVALID_SIZE\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(INVALID_ADDRESS\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(INVALID_NUMBER\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(NOT_DEFINED\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(RESOURCE_IN_USE\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(UNSATISFIED\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(INCORRRECT_STATE\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(ALREADY_SUSPENDED\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(ILLEGAL_ON_SELF\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(ILLEGAL_ON_REMOTE_OBJECT\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(CALLED_FROM_ISR\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(INVALID_PRIORITY\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(INVALID_TIME_OF_DAY\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(INVALID_NODE\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(NOT_CONFIGURED\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(NOT_OWNER_OF_RESOURCE\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(NOT_IMPLEMENTED\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(INTERNAL_ERROR\) - \(.*\)/@code{\1} - \2@*/' |
      sed -e 's/^\(NO_MEMORY\) - \(.*\)/@code{\1} - \2@*/'  |
      cat >$out
done
