#!/bin/sh

#
# This program use routines in texconfig of teTeX.
#

formats=/usr/share/texmf/web2c
append_db=/usr/share/texmf/web2c/mktexupd
texmfcnf=`kpsetool -w cnf texmf.cnf`
TEXMF=/usr/share/texmf

makeformat()
{
    FMT=$1
    cd $formats
    rm -f $formats/$FMT.fmt $formats/$FMT.log
    TEXFORMATS=
    export TEXFORMATS
    TEXINPUTS=
    if [ -x /usr/bin/multex ]; then
        TEXINPUTS="/usr/share/texmf/tex/multexini//:/usr/share/texmf/tex/mulplain//:`kpsepath -n $FMT tex`" \
        	multex -ini $FMT.ini </dev/null || return
        $append_db $formats $FMT.fmt
    fi
}

#
# config_replace is borrowed from texconfig.
#
config_replace()
{
	config=$1
	pattern=$2
	replacement=$3

#	require_binary ed

	test -f "$config" > /dev/null 2>&1
	if [ $? != 0 ]; then
		echo "config_replace: file '$config' not found." >&2
		return
	fi

	test -w "$config" > /dev/null 2>&1
	if [ $? != 0 ]; then
		echo "config_replace: cannot write to file '$config'." >&2
		return
	fi

	egrep -e "$pattern" "$config" > /dev/null 2>&1 
	if [ $? != 0 ]; then
		echo "$replacement" >> "$config"
	else
		ed $config >/dev/null 2>&1 <<-eof
			/$pattern/c
			$replacement
			.
			w
			q
		eof
		error=$?
		if [ $error != 0 ]; then
			echo "config_replace: ed returned error code '$error'." >&2
			return
		fi
	fi
}

#
# insert data before pattern
#
config_insert()
{
	config=$1
	pattern=$2
	before=$3
	data=$4

#	require_binary ed

	test -f "$config" > /dev/null 2>&1
	if [ $? != 0 ]; then
		echo "config_insert: file '$config' not found." >&2
		return
	fi

	test -w "$config" > /dev/null 2>&1
	if [ $? != 0 ]; then
		echo "config_insert: cannot write to file '$config'." >&2
		return
	fi

	egrep -e "$before" "$config" > /dev/null 2>&1 
	if [ $? != 0 ]; then
		echo "$data" >> "$config"
	else
		egrep -e "$pattern" "$config" > /dev/null 2>&1 
		if [ $? != 0 ]; then
			ed $config >/dev/null 2>&1 <<-eof
				/$before/i
				$data
				.
				w
				q
			eof
			error=$?
			if [ $error != 0 ]; then
				echo "config_insert: ed returned error code '$error'." >&2
				return
			fi
#		else
#			config_replace $config $pattern "$data"
		fi
	fi
}

#
# config_delete 
#
config_delete()
{
	config=$1
	pattern=$2

	test -f "$config" > /dev/null 2>&1
	if [ $? != 0 ]; then
		echo "config_replace: file '$config' not found." >&2
		return
	fi

	test -w "$config" > /dev/null 2>&1
	if [ $? != 0 ]; then
		echo "config_replace: cannot write to file '$config'." >&2
		return
	fi

	egrep -e "$pattern" "$config" > /dev/null 2>&1 
	if [ $? == 0 ]; then
		ed $config >/dev/null 2>&1 <<-eof
			/$pattern/d
			w
			q
		eof
		error=$?
		if [ $error != 0 ]; then
			echo "config_replace: ed returned error code '$error'." >&2
			return
		fi
	fi
}

init_tex_formats() {
    if [ -x /usr/bin/multex ]; then
        test -d $TEXMF/tex/amstex -a -d $TEXMF/tex/mulplain && makeformat amsmultex 
        test -d $TEXMF/tex/latex -a -d $TEXMF/tex/mullatex && makeformat mullatex 
        test -d $TEXMF/tex/generic -a -d $TEXMF/tex/mulplain && makeformat multex 
    fi
}

if [ -z "$texmfcnf" ]; then
   echo "texmf.cnf is badly installed"
   echo "Please purge and re-install tetex-bin"
   exit 1
fi

case $1 in
init)
    init_tex_formats ;
	;;
formats)
    init_tex_formats ;
	;;
multex)
    makeformat multex ;
	;;
mullatex)
    test -d $TEXMF/tex/latex -a -d $TEXMF/tex/mullatex && makeformat mullatex ;
	;;
amsmultex)
    test -d $TEXMF/tex/amstex && makeformat amsmultex ;
	;;
addpath|rempath)
	echo "${1}: obsolete argumetn."
	;;
*)	echo "Usage $0 [ init | formats | multex | mullatex |  amsmultex ]" ;
	exit 1
esac

