#!/bin/sh
##################################################################

tempfn=$1
cp -pf index_style.ist $tempfn.ist
StyleFile=$tempfn.ist
LyxCmd=lyx

### REMOVE ALL INTERMEDIATE FILES
rm -f $tempfn.aux
rm -f $tempfn.dvi
rm -f $tempfn.ps
rm -f $tempfn.pdf
rm -f $tempfn.idx
rm -f $tempfn.ilg
rm -f $tempfn.ind
rm -f $tempfn.log
rm -f $tempfn.tex
rm -f $tempfn.toc

### EXPORT TO LATEX AND COMPILE
$LyxCmd --export latex $tempfn.lyx
latex $tempfn.tex

### RUN makeindex TO CONSTRUCT INDEX INTERMEDIATE FILES
if test -f $StyleFile; then
	makeindex -s $StyleFile $tempfn.idx
else
	makeindex $tempfn.idx
fi

### AT THIS POINT, MANUALLY INSPECT $tempfn.ilg FOR ERRORS.
### IF THERE ARE ERRORS, USE $tempfn.ilg AND $tempfn.ind TO
### TROUBLESHOOT THOSE ERRORS. DO NOT GO FARTHER UNTIL
### makeindex RUNS CLEANLY WITHOUT ERRORS.

### ONCE AGAIN COMPILE WITH LATEX TO INCORPORATE INDEXING FILES 
### LATEX TWICE TO INCORPORATE ALL INDEX STUFF. ONCE IS NOT ENOUGH
latex $tempfn.tex
latex $tempfn.tex

### CHECK TO MAKE SURE THE PRECEDING COMPILE WORKED,
### IF NOT TROUBLESHOOT

### CONVERT TO PDF. CAREFUL, FOLLOWING LINE WRAPS IN EMAIL
dvips -o $tempfn.ps $tempfn.dvi
ps2pdf12   -dPDFSETTINGS=/prepress -dMaxSubsetPct=100 -dSubsetFonts=true -dEmbedAllFonts=true     $tempfn.ps

### VIEW THE RESULTS
#xpdf $tempfn.pdf
##################################################################
