#!/bin/bash
#Name: lxprocess
#Task: Process the specified LaTeX file.
#Args: Optional:
#      	- None		    -> clean up intermediate files.
#		- a3
#		- debug
#		- landscape		-> generate output in landscape orientation
#		- letter		-> generate output in letter paper format
#		- noclear
#		- pdf
#		- pdfbyps
#		- postscript
#		- print			-> send output to default printer
#		- resolve		-> resolve cross-references and bibliographic entries
#		- slide			-> generate output in slide format
#		- view
#      1. Name of input TeX file
#Note: If you need both landscape and letter, you must put them in order.

PATH=~saied/bin:$PATH
PS2PDF=ps2pdf14

if(test -d $HOME/tmp) then
	TmpDir=$HOME/tmp
else
	TmpDir=/tmp
fi

if(test -z "$1") then
    echo Cleaning up intermediate files...
	if( test -f "/proc/version") then
		RMOptions="-vf"
	else
		RMOptions="-f"
	fi
	/bin/rm $RMOptions .fix
	/bin/rm $RMOptions *.aux
	/bin/rm $RMOptions *.bbl
	/bin/rm $RMOptions *.blg
	/bin/rm $RMOptions *.dvi
	/bin/rm $RMOptions *.idx
	/bin/rm $RMOptions *.lof
	/bin/rm $RMOptions *.log
	/bin/rm $RMOptions *.lot
	/bin/rm $RMOptions *.out
	/bin/rm $RMOptions *.snm
	/bin/rm $RMOptions *.nav
	/bin/rm $RMOptions *.tmp
	/bin/rm $RMOptions *.toc
    exit
fi

OutFile=$TmpDir/LatexOut.tmp.$USER
DumpFile=$TmpDir/LatexOut.dmp.$USER
/bin/rm -fR $OutFile
for Parameter in $*
do
	case $Parameter in
		"-debug")
			DumpFile="/dev/stdout"
			;;
		"-dvi")
			ViewCommand="kdvi -style windows -geometry 1400x1024+0+0 --unique file:"
			ViewExtension="dvi"
			;;
		"-landscape")
			Landscape=true
			;;
		"-letter")
			Letter=true
			;;
		"-noclear")
			CLEAR=false
			;;
		"-pdf")
			ViewCommand="kpdf -style windows -geometry 1400x1024+0+0 "
			ViewExtension="pdf"
			Resolve=true
			PDFLaTeX=true
			;;
		"-pdfbyps")
			PDF=true
			PostScript=true
			Resolve=true
			unset PDFLaTeX
			;;
		"-postscript")
			PostScript=true
			;;
		"-print")
			Print=true
			;;
		"-resolve")
			Resolve=true
			;;
		"-slide")
			Slide=true
			;;
		"-view")
			View=true
			;;
		*)
			Filename=$Parameter
			;;
	esac
    shift
done

if(! test "$CLEAR" = "false") then
	clear
fi

DVIPSOptions="-Ppdf -G0"  # Embed PS fonts, not bitmapped fonts, so that PDF looks good
if(! test -z $Landscape) then
    DVIPSOptions="$DVIPSOptions -t landscape"
fi

if(! test -z $Letter) then
    DVIPSOptions="$DVIPSOptions -t letter"
else
	DVIPSOptions="$DVIPSOptions -t a4"
fi

FilenameStem=`echo $Filename | sed 's/.tex$//'`

if(! test -z $PDFLaTeX) then
	pdflatex $Filename
else
	echo Calling LaTeX
	latex $Filename
	#latex \\nonstopmode \\input\{$Filename} > $DumpFile
fi

if(test "$Resolve" = "true") then
	echo Calling BibTeX
	bibtex                    $FilenameStem >> $DumpFile
	if(! test -z $PDFLaTeX) then
		pdflatex $Filename
		pdflatex $Filename
		lxprocess
	else
		echo Calling LaTeX
		latex $Filename
		latex $Filename
		latex $Filename
	fi
fi

if(! test -z "$View") then
	#Don't start viewer if there's one already running
	MatchingViewerProcesses=`ps awx | grep "$ViewCommand$FilenameStem" | grep -v grep | wc -l`
	if([ $MatchingViewerProcesses -gt 0 ]) then
		echo "$MatchingViewerProcesses viewer(s) already running"
	else
		exec $ViewCommand$FilenameStem.$ViewExtension >/dev/null 2>&1 &
	fi
elif(! test -z $PostScript) then
	echo Calling dvips
	dvips $DVIPSOptions -f < $FilenameStem.dvi > $FilenameStem.ps
	if(! test -z $PDF) then
		if(test -z $Letter) then
			$PS2PDF -sPAPERSIZE=a4 $FilenameStem.ps
			echo A4
		else
			$PS2PDF -sPAPERSIZE=letter $FilenameStem.ps
		fi
		lxprocess
		/bin/rm $FilenameStem.ps
	fi
elif(! test -z $Print) then
	dvips $DVIPSOptions -f < $Filename.dvi
fi

if(test -f $OutFile) then
	ErrorsList=`  grep Error   $OutFile | sort | uniq`
	WarningsList=`grep Warning $OutFile | sort | uniq`
	if(! test -z "$ErrorsList") then
		echo
		echo Errors:
		echo $ErrorsList
		echo
	fi
	if(! test -z "$WarningsList") then
		echo
		echo Warnings:
		grep Warning $OutFile | sort | uniq
		echo
	fi
	/bin/rm -f $OutFile
fi
/bin/rm -f $DumpFile
