#!/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
jtexpath='TEXINPUTS.jtex		= .:$TEXMF/tex/{jplain,plain,generic,}//'
jlatexpath='TEXINPUTS.jlatex	= .:$TEXMF/tex/{jlatex,latex,jlatex209,latex209,generic,}//'
jlatex2epath='TEXINPUTS.jlatex2e	= .:$TEXMF/tex/{jlatex,latex,jlatex209,latex209,generic,}//'
jlatex209path='TEXINPUTS.jlatex209	= .:$TEXMF/tex/{jlatex209,latex209,jlatex,latex,generic,}//'
amsjtexpath='TEXINPUTS.amsjtex	= .:$TEXMF/tex/{amstex,jplain,plain,generic,}//'
jbibtexbibpath='BIBINPUTS.jbibtex	= .:$TEXMF/bibtex/bib/jbib/:$TEXMF/jbibtex/bib/:$TEXMF/bibtex/{bib,}//'
jbibtexbstpath='BSTINPUTS.jbibtex	= .:$TEXMF/bibtex/bst/jbst/:$TEXMF/jbibtex/bst/:$TEXMF/bibtex/{bst,}//'

makeformat()
{
    FMT=$1
    cd $formats
    rm -f $formats/$FMT.fmt $formats/$FMT.log
    TEXFORMATS=
    export TEXFORMATS
    TEXINPUTS=
    if [ -x /usr/bin/inijtex ]; then
        TEXINPUTS="/usr/share/texmf/tex/jtexini:`kpsepath -n $FMT tex`" \
        	inijtex $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.jtex[[:space:]=]' \
			    '^TEXINPUTS' "$jtexpath"
    config_insert $texmfcnf '^TEXINPUTS.jlatex[[:space:]=]' \
			    '^TEXINPUTS' "$jlatexpath"
    config_insert $texmfcnf '^TEXINPUTS.jlatex2e[[:space:]=]' \
			    '^TEXINPUTS' "$jlatex2epath"
    config_insert $texmfcnf '^TEXINPUTS.jlatex209[[:space:]=]' \
			    '^TEXINPUTS' "$jlatex209path"
    config_insert $texmfcnf '^TEXINPUTS.amsjtex[[:space:]=]' \
			    '^TEXINPUTS' "$amsjtexpath"
}

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

delete_path() {
    config_delete $texmfcnf '^TEXINPUTS.jtex[[:space:]=]'
    config_delete $texmfcnf '^TEXINPUTS.jlatex[[:space:]=]'
    config_delete $texmfcnf '^TEXINPUTS.jlatex2e[[:space:]=]'
    config_delete $texmfcnf '^TEXINPUTS.jlatex209[[:space:]=]'
    config_delete $texmfcnf '^TEXINPUTS.amsjtex[[:space:]=]'
#   config_delete $texmfcnf '^BIBINPUTS.jbibtex[[:space:]=]'
#   config_delete $texmfcnf '^BSTINPUTS.jbibtex[[:space:]=]'
}

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

init_tex_formats() {
    if [ -x /usr/bin/inijtex ]; then
        test -d $TEXMF/tex/latex209 -a -d $TEXMF/tex/jlatex209 && makeformat jlatex209 
        test -d $TEXMF/tex/amstex && makeformat amsjtex 
        test -d $TEXMF/tex/latex -a -d $TEXMF/tex/jlatex && makeformat jlatex 
        test -d $TEXMF/tex/generic -a -d $TEXMF/tex/jtexini && makeformat jtex 
    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 ;
	;;
jtex)
    makeformat jtex ;
	;;
jlatex)
    test -d $TEXMF/tex/latex -a -d $TEXMF/tex/jlatex && makeformat jlatex ;
	;;
jlatex209)
    test -d $TEXMF/tex/latex -a -d $TEXMF/tex/jlatex209 && makeformat jlatex209 ;
	;;
amsjtex)
    test -d $TEXMF/tex/amstex && makeformat amsjtex ;
	;;
addpath)
    add_path ;
	;;
rempath)
    delete_path ;
	;;
#addbpath)
#    add_bibpath ;
#	;;
#rembpath)
#    delete_bibpath ;
#	;;
*)	echo "Usage $0 [ init | formats | jtex | jlatex | jlatex209 | amsjtex | addpath | rempath ]" ;
	exit 1
esac

