#!/bin/sh

# Functions
setargs()
{
	case $2 in
	0)
		getfilename
		scope=5;
		;;
	1)
		getfilename
		scope=$3;
		;;
	2)
		fileid=$3
		scope=$4;
		;;
	*)
		echo "You can only use two arguments together with the script $1."
		exit;
		;;
	esac
	file="./$fileid"
}
getfilename()
{
	fileid=$(echo "$0" | sed -e s-^.*/-- -e s-\\..*--)
}
logoutput()
{
	exec 3>&1
	exec >$1.dtl
	$2
	exec 1>&3
	3>&-
}
echonewline()
{
	echo ""
	echo $1
}
docstrip()
{
	filename="$2"
	echonewline "$1"
	echo "\input docstrip" > $filename.tmp
	echo "\declarepreamble\dtmpre" >> $filename.tmp
	if [ -e $filename.pre ] ; then
		echo "    Using preamble from $filename.pre"
		cat $filename.pre >> $filename.tmp
	fi
	echo "\endpreamble" >> $filename.tmp
	echo "\declarepostamble\dtmpst" >> $filename.tmp
	if [ -e $filename.pst ] ; then
		echo "    Using postamble from $filename.pst"
		cat $filename.pst >> $filename.tmp
	fi
	echo "\endpostamble" >> $filename.tmp
	echo "$3" >> $filename.tmp
	echo "\endbatchfile" >> $filename.tmp
	logoutput $filename "tex -ini $filename.tmp"
	rm $filename.tmp
}
setversion()
{
	filename="$1"
	if [ $2 == $3 ] ; then
		echo "Processing $1.$2 (Pass No. $4 of $5) ..."	
	else
		echo "Replacing items in $1.$3 with information from $1.$2 ..."
	fi
	echo "#!/bin/sed -f" > $filename.sed
	cat $filename.$2 | while read ITEM
	do
		read CONTENT
		read BLANK
		ITEM=$(echo $ITEM | sed "s|:||g")
		echo "s|<N><<$ITEM>>|$ITEM:\\
<<$ITEM>>|g" >> $filename.sed
		echo "s|<<$ITEM>>|$CONTENT|g" >> $filename.sed
	done
	$filename.sed < $filename.$3 > $filename.tmp
	mv $filename.tmp $filename.$3
}
dolatex()
{
	filename="$1"
	echonewline "LaTeX pass No. $2 of $1 ..."
	latex $filename
}
doindex()
{
	filename="$1"
	echonewline "Creating index with makeindex -s gind.ist."
	sed 's\|hyperpage\\' <$filename.idx >$filename.idt
	mv $filename.idt $filename.idx
	makeindex -q -s gind.ist $filename.idx
}
dopsandpdf()
{
	filename="$1"
	echonewline "Creating $1.ps and $1.pdf ..."
	logoutput $filename "dvips -q -t a4 -Ppdf -D600 $filename.dvi"
	logoutput $filename "ps2pdf14 -q -r600 $filename.ps $filename.pdf"
}
rmfiles()
{
	echo ""
	for rmfile in $1 ; do
	filename="./$rmfile"
		if [ -e "$rmfile" ] ; then
			echo "Deleting file $rmfile in current directory."
			rm "$filename"
		fi
	done
}
deletelatex="
s|<+L>[^<]*<-L>||g
s|<[+-][T]>||g
s|<[+-]Q>|\"|g
s|§[+-]F§|\'|g
s|§[+-]P§||g
s|§PAR§[ ]*|\\
\\
|g
s|§<§|<|g
s|§>§|>|g
s|§VERB§||g
s|§ESC§||g
"
setlatex="
s|<+T>[^<]*<-T>||g
s|<[+]Q>|\`\`|g
s|<[-]Q>|\'\'|g
s|<[+-]L>||g
s|§+[FP]§|\\\\mbox\{\\\\fontfamily{cmss}\\\\selectfont |g
s|§-[FP]§|\}|g
s|§[<>]§||g
s|§PAR§[ ]*|\\\\par |g
s|§ESC§|\\\|g
s/§VERB§/|/g
s|\ä|\\\\\"\{a\}|g
s|\ö|\\\\\"\{o\}|g
s|\ü|\\\\\"\{u\}|g
s|\ß|\\\\ss|g
"
replaceinmodules()
{
	filename="$1"
	for module in $2
	do
	echo "Processing $1 (module: $module)."
	sed "/^%<\*$module>/,/^%<\/$module>/{
	$3
	}" < $filename > $filename.tmp
	mv $filename.tmp $filename
	done
}
commentinmodules()
{
	filename="$1"
	for module in $2
	do
	sed "/^%<\*$module>/,/^%<\/$module>/{
	s/^/% /
	s/^% %<\*$module>/%<\*$module>/
	s/^% %<\/$module>/%<\/$module>/
	}" < $filename > $filename.tmp
	mv $filename.tmp $filename
	done
}
folduncommentedlines()
{
	filename="$1"
	sed "/^[^%]/{
	s/§CR§/%\\
/
	}" < $filename > $filename.tmp
	mv $filename.tmp $filename
	sed "/^%/{
	s/§CR§//
	}" < $filename > $filename.tmp
	mv $filename.tmp $filename
}
removemoduletags()
{
	filename="$1"
	for module in $2
	do
	sed "s/^%<[\*\/]$module>/%/" < $filename > $filename.tmp
	mv $filename.tmp $filename
	done
}
hidetext()
{
	filename="$1"
	sed -e "s|^%<begin text>|% \\\\iffalse|" -e "s|^%<end text>|% \\\\fi|" < $filename > $filename.tmp
	mv $filename.tmp $filename
}
replacemoduletags $file.dtx "tex" "% \\iffalse" "% \\fi"
dofold()
{
	filename="$1"
	for foldfile in $2
	do
		fold -s -$filename < $foldfile > $foldfile.tmp
	mv $foldfile.tmp $foldfile
	done
}
# End Functions

# Variables
overwrite="\\askforoverwritefalse"
nopreorpostamble="\\usepreamble\\empty\\usepostamble\\empty"
dtmpreandpostamble="\\usepreamble\\dtmpre\\usepostamble\\dtmpst"
# End Variables

setargs $0 $# $1 $2

# Info about this script
	echonewline "This script extracts the derivative files of the"
	echo "package $file from the master file $file.dtm."
	echonewline "Script file: $0."
	echo "Process ID: $$."
	echo "Scope: $scope."
# End Info

filesindirectory=$(ls -1 | wc -l)
if [ $filesindirectory -ne 3 ] ; then
	echonewline "The number of files in this directory is not equal to 3."
       	echo "Please put the files"
        echo "'$fileid' (this script),"
	echo "'$fileid.dtm' and"
	echo "'$fileid.dts'"
        echo "in an empty directory before executing this script."
	exit
fi

cp $file.dtm $file.dtx

docstrip "Extracting version file from $file.dtx." $file "\
\\generate{$overwrite$nopreorpostamble\\file{$file.ver}{\\from{$file.dtx}{version}}}"

echonewline "Inserting information from the version file into $file.dtx."
if [ $scope -gt 0 ] ; then
	setversion $file ver ver 1 4
	setversion $file ver ver 2 4
	setversion $file ver ver 3 4
	setversion $file ver ver 4 4
	setversion $file ver dtx 1 1
fi

echonewline "Adapting files to LaTeX or ASCII environment."
replaceinmodules $file.dtx "version preamble postamble sty readme" "$deletelatex"
replaceinmodules $file.dtx "tex" "$setlatex"
commentinmodules $file.dtx "manual"
removemoduletags $file.dtx "tex manual"
folduncommentedlines $file.dtx
hidetext $file.dtx

docstrip "Extracting preamble and postamble from $file.dtx." $file "\
\\generate{$overwrite$nopreorpostamble\
	\\file{$file.pre}{\\from{$file.dtx}{preamble}}\
	\\file{$file.pst}{\\from{$file.dtx}{postamble}}\
}"

dofold 69 "$file.pre $file.pst"

docstrip "Extracting README file, style file and test file from $file.dtx." $file "\
\\generate{$overwrite$nopreorpostamble\\file{README.txt}{\\from{$file.dtx}{readme}}}\
\\generate{$overwrite$dtmpreandpostamble\
	\\file{$file.sty}{\\from{$file.dtx}{sty}}\
	\\file{$file-test.tex}{\\from{$file.dtx}{test}}\
}"

dofold 72 "README.txt"

if [ $scope -gt 1 ] ; then
	echonewline "Creating LaTeX file for processing documentation."
	echo "\documentclass[11pt,a4paper]{article}" > $file.tmp
	echo "\input $file.dts" >> $file.tmp
	echo "\begin{document}" >> $file.tmp
	echo "\DocInput{$file.dtx} \newpage \DOCindex" >> $file.tmp
	echo "\end{document}" >> $file.tmp
	dolatex $file.tmp 1
fi
if [ $scope -gt 2 ] ; then
	doindex $file
	dolatex $file.tmp 2
fi
if [ $scope -gt 3 ] ; then
	doindex $file
	dolatex $file.tmp 3
	doindex $file
	dolatex $file.tmp 4
fi
if [ $scope -gt 1 ] ; then
	dolatex $file-test.tex 1
fi
if [ $scope -gt 3 ] ; then
	dopsandpdf $file
	dopsandpdf $file-test
fi
if [ $scope -gt 4 ] ; then
	rmfiles "$file.dvi $file.ps $file.out.ps $file-test.dvi $file-test.ps"
fi
if [ $scope -gt 3 ] ; then
	rmfiles "$file.ilg $file.ind"
fi
if [ $scope -gt 2 ] ; then
	rmfiles "$file.aux $file.dtl $file.idx $file.log $file.tmp $file.toc $file.ver"
	rmfiles "$file.sed"
	rmfiles "$file-test.aux $file-test.dtl $file-test.log pexa-sam.tex"
fi
rmfiles "$file.pre $file.pst"
