#!/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
multexpath='TEXINPUTS.multex		= .:$TEXMF/tex/{mulplain,jplain,plain,generic,}//'
mullatexpath='TEXINPUTS.mullatex	= .:$TEXMF/tex/{mullatex,latex,latex209,generic,}//'
jmullatexpath='TEXINPUTS.jmullatex	= .:$TEXMF/tex/{mullatex,latex,latex209,generic,}//'
mullatex2epath='TEXINPUTS.mullatex2e	= .:$TEXMF/tex/{mullatex,latex,latex209,generic,}//'
mullatex209path='TEXINPUTS.mullatex209	= .:$TEXMF/tex/{latex209,mullatex,latex,generic,}//'
amsmultexpath='TEXINPUTS.amsmultex	= .:$TEXMF/tex/{amstex,mullatex,plain,generic,}//'

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
}

add_path() {
    config_insert $texmfcnf '^TEXINPUTS.multex[[:space:]=]' \
			    '^TEXINPUTS' "$multexpath"
    config_insert $texmfcnf '^TEXINPUTS.mullatex[[:space:]=]' \
			    '^TEXINPUTS' "$mullatexpath"
#    config_insert $texmfcnf '^TEXINPUTS.jmullatex[[:space:]=]' \
#			    '^TEXINPUTS' "$jmullatexpath"
    config_insert $texmfcnf '^TEXINPUTS.mullatex2e[[:space:]=]' \
			    '^TEXINPUTS' "$mullatex2epath"
#    config_insert $texmfcnf '^TEXINPUTS.mullatex209[[:space:]=]' \
#			    '^TEXINPUTS' "$mullatex209path"
    config_insert $texmfcnf '^TEXINPUTS.amsmultex[[:space:]=]' \
			    '^TEXINPUTS' "$amsmultexpath"
}

add_bibpath() {
    config_insert $texmfcnf '^BIBINPUTS.jbibtex[[:space:]=]' \
			    '^BIBINPUTS' "$jbibtexbibpath"
    config_insert $texmfcnf '^BSTINPUTS.jbibtex[[:space:]=]' \
			    '^BSTINPUTS' "$jbibtexbstpath"
}

delete_path() {
    config_delete $texmfcnf '^TEXINPUTS.multex[[:space:]=]'
    config_delete $texmfcnf '^TEXINPUTS.mullatex[[:space:]=]'
    config_delete $texmfcnf '^TEXINPUTS.mullatex2e[[:space:]=]'
#    config_delete $texmfcnf '^TEXINPUTS.mullatex209[[:space:]=]'
    config_delete $texmfcnf '^TEXINPUTS.amsmultex[[:space:]=]'
}

delete_bibpath() {
    config_delete $texmfcnf '^BIBINPUTS.jbibtex[[:space:]=]'
    config_delete $texmfcnf '^BSTINPUTS.jbibtex[[:space:]=]'
}

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)
    add_path ;
    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)
    add_path ;
	;;
rempath)
    delete_path ;
	;;
*)	echo "Usage $0 [ init | formats | multex | mullatex |  amsmultex | addpath | rempath ]" ;
	exit 1
esac

