#!/bin/sh

# generate sed file for filename replaces
echo "# Don't edit this file is generated" > htex.sed
for i in *.tex
do
    j=${i/.tex/.htex}
    echo "s/$i/$j/" >> htex.sed
done

for i in *.tex
do
    htex=${i/.tex/.htex}

    echo "$i -> $htex"
    
    # retrieve exampledir
    exdir=`grep FPCexampledir $i | sed "s+\(.*\)FPCexampledir{\(.*\)}+\2+"`

    # replace FPCexample calls
    if [ "$exdir" != "" ]
    then
        j=${i/.tex/.tmp}
        sed "s/FPCexample{\(.*\)}/input{$exdir\/\1.tex}/" $i > $j
        k=$j
    else
	j=$i
        k=""
    fi
    sed -f htex.sed $j > $htex

    # remove temp
    [ $k ] && [ -e $k ] && rm $k
done

rm htex.sed