#!/bin/sh
#
# ptexconfig - configuration script for ptex-bin
#
# Public domain.
#

TEXMF=/usr/share/texmf
formats=$TEXMF/web2c
append_db=$TEXMF/web2c/mktexupd
texmfcnf=/etc/texmf/texmf.cnf

# Paths to add
ptexpath='TEXINPUTS.ptex = .;$TEXMF/{ptex,tex}/{plain,generic,}//'
platexpath='TEXINPUTS.platex = .;$TEXMF/{ptex,tex}/{platex,latex,generic,}//'
iniptexpath='TEXINPUTS.iniptex = .;$TEXMF/{ptex,tex}/{plain,generic,}//'
amsptexpath='TEXINPUTS.amsptex = .;$TEXMF/{ptex,tex}/{amstex,plain,generic,}//'

makeformat()
{
    FMT=$1
#    cd $formats
    TEXFORMATS=
    export TEXFORMATS
    TEXINPUTS=
    export TEXINPUTS
    case $FMT in
    ptex|platex)
    fmtutil --cnffile /etc/texmf/fmtutil-ptex.cnf --byfmt $FMT
    $append_db $formats $FMT.fmt ;;
    amsptex)
    TEXINPUTS="$TEXMF/tex/amstex//:$TEXMF/ptex/plain//:" \
    fmtutil --cnffile /etc/texmf/fmtutil-ptex.cnf --byfmt amsptex ;;
    *)
    echo "Bad Usage!!" ;;
    esac
}

#
# 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=$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.ptex[[:space:]=]' \
			    '^TEXINPUTS' "$ptexpath"
    config_insert $texmfcnf '^TEXINPUTS.platex[[:space:]=]' \
			    '^TEXINPUTS' "$platexpath"
    config_insert $texmfcnf '^TEXINPUTS.iniptex[[:space:]=]' \
			    '^TEXINPUTS' "$iniptexpath"
    config_insert $texmfcnf '^TEXINPUTS.amsptex[[:space:]=]' \
			    '^TEXINPUTS' "$amsptexpath"
}

delete_path() {
    config_delete $texmfcnf '^TEXINPUTS.ptex[[:space:]=]'
    config_delete $texmfcnf '^TEXINPUTS.platex[[:space:]=]'
    config_delete $texmfcnf '^TEXINPUTS.iniptex[[:space:]=]'
    config_delete $texmfcnf '^TEXINPUTS.amsptex[[:space:]=]'
}

init_tex_formats() {
    if [ -x /usr/bin/ptex ]; then
        makeformat ptex
        makeformat platex
        makeformat amsptex
    fi
}

case $1 in
init)
    add_path ;
    init_tex_formats ;
	;;
formats)
    init_tex_formats ;
	;;
ptex)
    makeformat ptex ;
	 ;;
platex)
    makeformat platex ;
	 ;;
amsptex)
    makeformat amsptex ;
	 ;;
addpath)
    add_path ;
	;;
rmpath)
    delete_path ;
	;;
cleanpath)
    delete_path ;
	;;
*)	echo "Usage $0 [ init | formats | ptex | platex | rmpath | addpath ]" ;
	exit 1
esac
