#!/bin/bash
# Copyright (c) 2002 by Niklaus Giger
# Last modification, 24 february 2002
#
#
# A script to automate the generation of the output of LyX-DocBook files
#
#
#  ----------------- Built-in options ----------
#
export  LYX=lyx
export  PICT_FORMAT=.png
export CHARSET=EUC-KR
#
#
#  ----------------- Check-User inpurt ---------
#
if [ $# -ne 1 ]
then
   echo "useage:> treat <lyxfile>"
   exit
fi
#
#  ----------------- LYX -> SGML ---------------
#
for i in $@
do
  echo "Transforming $i.lyx  -> $i.sgml file"
  $LYX -e docbook $@
done
#
#  ----------------- SGML -> HTML ---------------
#
for i in $@
do
  echo "Transforming $i.smgl -> $i/*.html files"
  sgmltools -b html $@.sgml
done
#
#  ----------------- Patching HTML --------------
#

for i in $@/*html
do
   echo "Patching HTML-file $i "
   awk \
      '{ /.gif/    gsub( ".gif", ENVIRON["PICT_FORMAT"])} \
       { /\/HEAD>/ gsub( "/HEAD", "meta http-equiv=\"Content-Type\" \
         content=\"text/html; charset=\""ENVIRON["CHARSET"]"\"> </HEAD"); print } '\
       $i > $i.tmp
   mv $i.tmp $i
done

#
#  ----------------- Copy/Convert pictures --------------
#
for i in `grep '\<SRC=' $1/*html | cut -d\"  -f2`
do
    if  test -f ${i/.eps/$PICT_FORMAT}
    then
	# if there is a newer $PICT_FORMAT in our directory 
        # then we copy it into the new directory
        echo "copying ${i/.eps/$PICT_FORMAT} -> $1/${i/.eps/$PICT_FORMAT}"
	cp ${i/.eps/$PICT_FORMAT} $1/${i/.eps/$PICT_FORMAT}
    else
        # we convert it
        echo "convert $i -> $1/${i/.eps/$PICT_FORMAT}"
        convert $i $1/${i/.eps/$PICT_FORMAT}
    fi
done 
