#!/bin/ksh93

typeset -r FPROG=${.sh.file}
typeset -r PROG=${FPROG##*/}
SDIR=${FPROG%/*}

function showUsage {
	[[ -z $1 ]] && X='--man' || X='-?'
	getopts -a ${PROG} "${ print ${USAGE} ; }" OPT $X
}

USAGE='[-?1.0 ]
[-author?Thomas Esser <te@dbs.uni-hannover.de> (Mar 1997)]
[-author?Jens Elkner (2015 ksh93 port)]
[-license?Public Domain]
[+NAME?'"${PROG}"' - teTeX-style kpsetool (kpsewhich, kpsexpand and kpsepath)]
[+DESCRIPTION?Script to make the old teTeX-style versions of kpsewhich, kpsexpand and kpsepath available. All the real work is done inside Web2C'"'"'s kpsewhich, which offers a superset of the required functionality.]
[+?Valid pathtypes are:]{
	[+gf?generic font bitmap]
	[+pk?packed bitmap font]
	[+base?Metafont memory dump]
	[+bib?BibTeX bibliography source]
	[+bst?BibTeX style files]
	[+cnf?Kpathsea runtime configuration files]
	[+fmt?TeX memory dump]
	[+mem?MetaPost memory dump]
	[+mf?Metafont source]
	[+mfpool?Metafont program strings]
	[+mp?MetaPost source]
	[+mppool?MetaPost program strings]
	[+mpsupport?MetaPost support files]
	[+pict?Other kinds of figures]
	[+tex?TeX source]
	[+texpool?TeX program strings]
	[+tfm?TeX font metrics]
	[+vf?virtual font]
	[+dvips_config?dvips config files]
	[+dvips_header?dvips header files]
	[+troff_font?troff fonts]
}
[h:help?Print this help and exit.]
[F:functions?Print a list of all functions available.]
[T:trace]:[fnList?A comma separated list of functions of this script to trace (convinience for troubleshooting).]
[+?]
[n]:[progname?Pretend to be \aprogname\a to kpathsea.]
[m]:[mode?Set Metafont \amode\a.]
[w?Locate files (similar to kpsewhich).]
[p?Act like kpsepath.]
[v?Act like kpsexpand.]
'
if [[ ${PROG} == 'kpsewhich' ]]; then
	USAGE+='\n\n-w \apathtype\a \afilename\a'
	ACTION=${PROG}
elif [[ ${PROG} == 'kpsepath' ]]; then
	USAGE+='\n\n\apathtype\a'
	ACTION=${PROG}
else # [[ ${PROG} == 'kpsexpand' ]]; then
	USAGE+='\n\n\astring\a'
	ACTION='kpsexpand'
fi
unset FLAGS ; FLAGS=

X="${ print ${USAGE} ; }"
while getopts "${X}" OPT ; do
	case ${OPT} in
		h) showUsage ; exit 0 ;;
		T)	if [[ ${OPTARG} == 'ALL' ]]; then
				typeset -ft ${ typeset +f ; }
			else
				typeset -ft ${OPTARG//,/ }
			fi
			;;
		F)
			typeset +f && exit 0
			;;
		n)	FLAGS+=( "-progname=${OPTARG}" ) ;;
		m)	FLAGS+=( "-mode=${OPTARG}" ) ;;
		w)	ACTION='kpsewhich' ;;
		p)	ACTION='kpsepath' ;;
		v)	ACTION='kpsexpand' ;;
	esac
done
X=$((OPTIND-1))
shift $X && OPTIND=1
unset X

PT2FMT=(
	['gf']='gf' ['pk']='pk' ['mfpool']='.pool' ['mppool']='.pool'
	['mpsupport']='MetaPost support' ['pict']='.eps' ['texpool']='.pool'
	['dvips_config']='dvips config' ['dvips_header']='.pro'
	['troff_font']='Troff fonts'
)
for X in base bib bst cnf fmt mem mf mp tex tfm vf ; do
	PT2FMT["$X"]=".$X"
done

if [[ ${ACTION} == 'kpsewhich' || ${ACTION} == 'kpsepath' ]]; then
	FMT="$1"
	[[ -z ${PT2FMT["${FMT}"]} ]] && \
		print -u2 "${PROG}: unknown pathtype '${FMT}'" && showUsage 1 && exit 1
	shift
fi

if [[ ${ACTION} == 'kpsewhich' ]]; then
	[[ -z $1 ]] && \
		print -u2 "${PROG}: missing filename" && showUsage 1 && exit 1
	kpsewhich "${FLAGS[@]}" -format="${FMT}" "$1"
elif [[ ${ACTION} == 'kpsepath' ]]; then
	kpsewhich "${FLAGS[@]}" -show-path="${FMT}"
elif [[ ${ACTION} == 'kpsexpand' ]]; then
	[[ -z $1 ]] && \
		print -u2 "${PROG}: missing string" && showUsage 1 && exit 1
	kpsewhich "${FLAGS[@]}" -expand-var="$1"
fi
