#!/usr/bin/sh
# @(#$Id: rcllyx,v 1.6 2007-06-08 13:51:09 dockes Exp $  (C) 2004 J.F.Dockes
# There may still be code from Estraier in here:
#================================================================
# Estraier: a personal full-text search system
# Copyright (C) 2003-2004 Mikio Hirabayashi
#================================================================
#================================================================
# Convert a lyx file to recoll HTML.
#
# We use lyx --export. It was suggested to use untex, but it doesn't give 
# good results on raw lyx (of course, this is not TeX), and exporting to
# LaTex then using untex doesn't look nice when we can use the native  lyx
# text export.
# Modern lyx versions always export to utf-8. This file use to include code
# for handling character set issues, this has been removed.
#
# As there is unfortunately no way to define the output file name, we have
# to use a temporary directory and link the input file in there.

# set variables
#LANG=C ; export LANG
#LC_ALL=C ; export LC_ALL
progname="rcllyx"
filetype=lyx





#RECFILTCOMMONCODE
##############################################################################
# !! Leave the previous line unmodified!! Code imported from the
# recfiltcommon file

# Utility code common to all shell filters. This could be sourced at run
# time, but it's slightly more efficient to include the code in the
# filters at build time (with a sed script).

# Describe error in a way that can be interpreted by our caller
senderror()
{
    echo RECFILTERROR $*
    # Also alert on stderr just in case
    echo ":2:$progname::: $*" 1>&2
    exit 1
}

iscmd()
{
    cmd=$1
    case $cmd in
    */*)
	if test -x $cmd -a ! -d $cmd ; then return 0; else return 1; fi ;;
    *)
      oldifs=$IFS; IFS=":"; set -- $PATH; IFS=$oldifs
      for d in $*;do test -x $d/$cmd -a ! -d $d/$cmd && return 0;done
      return 1 ;;
    esac
}

checkcmds()
{
    for cmd in $*;do
      if iscmd $cmd 
      then 
        a=1
      else 
        senderror HELPERNOTFOUND $cmd
      fi
    done
}

# show help message
if test $# -ne 1 -o "$1" = "--help" 
then
  echo "Convert a $filetype file to HTML text for Recoll indexing."
  echo "Usage: $progname [infile]"
  exit 1
fi

infile="$1"

# check the input file existence (may be '-' for stdin)
if test "X$infile" != X- -a ! -f "$infile"
then
  senderror INPUTNOSUCHFILE "$infile"
fi

# protect access to our temp files and directories
umask 77

makemytmpdir()
{
    test -n "$progname" || senderror INTERNAL NO PROGNAME
    if test z"$RECOLL_TMPDIR" != z; then
        ttdir=$RECOLL_TMPDIR
    elif test z"$TMPDIR" != z ; then
        ttdir=$TMPDIR
    else
        ttdir=/tmp
    fi
    tmpdir=$ttdir/${progname}_tmp$$
    mkdir $tmpdir || exit 1
    mkdir $tmpdir/${progname}tmp || exit 1
}

cleanup()
{
    test -n "$progname" || senderror INTERNAL NO PROGNAME
    test -n "$ttdir" || senderror INTERNAL NO TTDIR
    # Note that we're using a constant part (rcllyxtmp), that hopefully
    # guarantees that we can't do big mistakes here.
    rm -rf $ttdir/${progname}_tmp$$/${progname}tmp
    rmdir $ttdir/${progname}_tmp$$
}

##############################################################################
# !! Leave the following line unmodified !
#ENDRECFILTCOMMONCODE

checkcmds lyx iconv

# We need a temporary directory. Note that modern lyx versions support a
# -export-to command for specifying the output file, which could simplify
# this script a bit, but it does not appear to allow writing to stdout.
makemytmpdir
trap cleanup EXIT HUP QUIT INT TERM

workdir=$tmpdir/rcllyxtmp
infile=`realpath "$infile"`

ln -s "$infile" "$workdir/input.lyx" || exit 1
lyxfile=$workdir/input.lyx
textfile=$workdir/input.txt

#echo lyxfile: $lyxfile ; ls -l $lyxfile; echo textfile: $textfile

# Run lyx --export
lyx --export text "$lyxfile" || senderror "lyx --export text "$lyxfile" failed"

cat <<EOF
<html>
<head>
    <title>$title</title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>
<body>
<pre>
EOF

cat "$textfile"

cat <<EOF
</pre>
</body>
</html>
EOF
