#!/bin/sh
# @(#$Id: rclrtf,v 1.2 2005/11/23 10:17:53 dockes Exp $  (C) 2004 J.F.Dockes
# Some inspiration from estraier
#================================================================
# rclrtf
# convert rtf to html, by  executing the unrtf program:
#    http://www.gnu.org/software/unrtf/unrtf.html
#================================================================


# set variables
LANG=C ; export LANG
LC_ALL=C ; export LC_ALL
progname="rclrtl"

# show help message
if test $# -ne 1 -o "$1" = "--help" 
then
  printf 'Convert RTF to HTML for Recoll input.\n'
  printf 'Usage: %s [infile]\n' "$progname"
  exit 1
fi

infile="$1"

# check the input file existence
if test ! -f "$infile"
then
  printf '%s: %s: no such file\n' "$progname" "$infile"
  exit 1
fi

iscmd()
{
    cmd=$1
    case $cmd in
    */*)
	if test -x $cmd ; then return 0; else return 1; fi ;;
    *)
      oldifs=$IFS; IFS=":"; set -- $PATH; IFS=$oldifs
      for d in $*;do test -x $d/$cmd && return 0;done
      return 1 ;;
    esac
}
checkcmds()
{
    for cmd in $*;do
      if iscmd $cmd 
      then 
        a=1
      else 
        echo $cmd not found 1>&2 
	exit 1
      fi
    done
}
checkcmds awk unrtf

# output the result
unrtf --nopict --html "$infile" 2> /dev/null |
awk '
BEGIN {
  gothead = 0
}
/<\/head>/{
    if (gothead == 0) {
        printf("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n")
	gothead = 1
    }
}
{
  print
}
' 
