#!/bin/sh

pkgname=$1

BASENAME=${BASENAME:-/usr/bin/basename}
CP=${CP:-/bin/cp}
ECHO=${ECHO:-}
GREP=${GREP:-/usr/bin/grep}
SED=${SED:-/usr/bin/sed}
TOUCH=${TOUCH:-/usr/bin/touch}
CUT=${CUT:-%%CUT%%}
DO_NADA=${DO_NADA:-/usr/bin/true}
CUT=/usr/bin/cut
EXPR=/bin/expr

infodir=${INFODIR:-/usr/local/lib/xemacs/site-packages/info}
infofiles=${INFOFILES:-yatexj:yatexe:yahtmlj:yahtmle}
infonodes=${INFONODES:-YaTeX-jp:YaTeX:yahtml-jp:yahtml}
infonodeexps=${INFONODEEXPS:-Yet Another tex-mode for Emacs. (Japanese):Yet Another tex-mode for Emacs.:Yet Another HTML mode. (Japanese):Yet Another HTML mode.}
dirsection=${DIRSECTION:-The Emacs editor and associated tools}

if [ "X$2" = X"POST-INSTALL" ]; then
    count=1
    # For example, the result of `cut ttt -d : -f 2` is ttt. Why?
    infofiles=${infofiles}:
    while ${DO_NADA}; do
	if [ X`${ECHO} ${infofiles} | ${CUT} -d : -f $count` = X ]; then
	    break
	fi
	file=`${ECHO} ${infofiles} | ${CUT} -d : -f $count`
	if [ Xxemacs = Xxemacs ]; then
	    file=${file}.info
	fi
	nodename=`${ECHO} ${infonodes} | ${CUT} -d : -f $count`
	nodeexp=`${ECHO} ${infonodeexps} | ${CUT} -d : -f $count`
	if [ ! "`${GREP} \"START-INFO-DIR-ENTRY\" ${infodir}/${file}`" ]; then
	    ${ECHO} "INFO-DIR-SECTION ${dirsection}" \
					    >> ${infodir}/${file}
	    ${ECHO} "START-INFO-DIR-ENTRY"	>> ${infodir}/${file}
	    ${ECHO} "* ${nodename}: (`${BASENAME} ${file}`).		${nodeexp}" \
					    >> ${infodir}/${file}
	    ${ECHO} "END-INFO-DIR-ENTRY"	>> ${infodir}/${file}
	fi
	count=`${EXPR} $count + 1`
    done
    if [ Xxemacs != Xxemacs ]; then
	${ECHO} "Adding entry for \"${pkgname}\" to ${infodir}/dir"
	for file in `${ECHO}  ${infofiles} | ${SED} "s,:, ,g"`; do
		install-info ${infodir}/${file} ${infodir}/dir
	done
    fi
    exit 0
else
    exit 0
fi
