# $NetBSD: INSTALL,v 1.4 2006/04/07 16:19:15 minskim Exp $

case "${STAGE}" in
POST-INSTALL)
	${TEST} ! -x ./+TEXCONFIG || ./+TEXCONFIG PAPER 
	${TEST} ! -x ./+TETEX || ./+TETEX ADD
	;;
esac

#
# Generate a +TETEX script that updates the teTeX installation
# based on user-modifiable configuration files.
#
case "${STAGE},$1" in
UNPACK,|UNPACK,+TETEX)
	${CAT} > ./+TETEX << 'EOF'
#!@SH@
#
# +TETEX - teTeX installation update script
#
# Usage: ./+TETEX ADD|REMOVE

CAT="@CAT@"
ECHO="@ECHO@"
GREP="@GREP@"
RM="@RM@"
RMDIR="@RMDIR@"
TEST="@TEST@"
TRUE="@TRUE@"

SELF=$0
ACTION=$1
: ${PKG_PREFIX=@PREFIX@}

TEXFONTS_DIR="@TEXFONTSDIR@"
TEXMF_DISTDATADIR="${PKG_PREFIX}/share/texmf-dist"
TEXMF_DATADIR="${PKG_PREFIX}/share/texmf"
TEXMF_LOCALDATADIR="${PKG_PREFIX}/share/texmf-local"
TEXMF_VARDIR="${PKG_PREFIX}/share/texmf-var"

exitcode=0
case $ACTION in
ADD)
	# Create ls-R databases in all of the usual TeX directories.
	${TEST} ! -x ${PKG_PREFIX}/bin/mktexlsr ||
		${PKG_PREFIX}/bin/mktexlsr

	# Update teTeX installation to support different TeX formats.
	${TEST} ! -x ${PKG_PREFIX}/bin/fmtutil-sys ||
		${PKG_PREFIX}/bin/fmtutil-sys --all
	${TEST} ! -x ${PKG_PREFIX}/bin/texlinks ||
		${PKG_PREFIX}/bin/texlinks

	# Update the font map files for TeX output drivers.
	${TEST} ! -x ${PKG_PREFIX}/bin/updmap-sys ||
		${PKG_PREFIX}/bin/updmap-sys
	;;

REMOVE)
	# Remove the ls-R databases from the usual TeX directories
	# generated by mktexlsr.
	#
	${RM} -f ${TEXFONTS_DIR}/ls-R
	${RM} -f ${TEXMF_DISTDATADIR}/ls-R
	${RM} -f ${TEXMF_DATADIR}/ls-R
	${RM} -f ${TEXMF_LOCALDATADIR}/ls-R
	${RM} -f ${TEXMF_VARDIR}/ls-R

	# We inspect both the user-modified and distributed fmtutil.cnf
	# files because we want to ensure we remove bits corresponding
	# to both files.
	#
	FMTUTIL_CNF=
	for cnf in fmtutil.cnf.dist fmtutil.cnf; do
		file="${TEXMF_DATADIR}/web2c/$cnf"
		${TEST} ! -f "$file" ||
			FMTUTIL_CNF="${FMTUTIL_CNF} $file"
	done
	
	# Remove fmt, mem, and log files generated by fmtutil-sys.
	${TEST} -z "${FMTUTIL_CNF}" ||
	${CAT} ${FMTUTIL_CNF} | ${GREP} "^[^#]" |
	while read format junk; do
		${RM} -f ${TEXMF_VARDIR}/web2c/$format.base
		${RM} -f ${TEXMF_VARDIR}/web2c/$format.fmt
		${RM} -f ${TEXMF_VARDIR}/web2c/$format.log
		${RM} -f ${TEXMF_VARDIR}/web2c/$format.mem
	done

	# Remove engines generated by texlinks.  This is related to the
	# install_link() function in texlinks.
	#
	${TEST} -z "${FMTUTIL_CNF}" ||
	${CAT} ${FMTUTIL_CNF} | ${GREP} "^[^#]" |
	while read format engine junk; do
		case "$format" in
		cont-??|metafun|mptopdf)
			;;
		*)
			 ${TEST} "$format" = "$engine" ||
				${RM} -f ${PKG_PREFIX}/bin/$format
			;;
		esac
	done

	# Remove font map files generated by updmap-sys.  The list of
	# files is taken from the updmap man page and matches the
	# "output files" for updmap.
	#
	${RM} -f ${TEXMF_VARDIR}/fonts/map/dvipdfm/updmap/dvipdfm.map
	${RM} -f ${TEXMF_VARDIR}/fonts/map/dvipdfm/updmap/dvipdfm_dl14.map
	${RM} -f ${TEXMF_VARDIR}/fonts/map/dvipdfm/updmap/dvipdfm_ndl14.map
	${RM} -f ${TEXMF_VARDIR}/fonts/map/dvips/updmap/builtin35.map
	${RM} -f ${TEXMF_VARDIR}/fonts/map/dvips/updmap/download35.map
	${RM} -f ${TEXMF_VARDIR}/fonts/map/dvips/updmap/ps2pk.map
	${RM} -f ${TEXMF_VARDIR}/fonts/map/dvips/updmap/psfonts.map
	${RM} -f ${TEXMF_VARDIR}/fonts/map/dvips/updmap/psfonts_pk.map
	${RM} -f ${TEXMF_VARDIR}/fonts/map/dvips/updmap/psfonts_t1.map
	${RM} -f ${TEXMF_VARDIR}/fonts/map/pdftex/updmap/pdftex.map
	${RM} -f ${TEXMF_VARDIR}/fonts/map/pdftex/updmap/pdftex_dl14.map
	${RM} -f ${TEXMF_VARDIR}/fonts/map/pdftex/updmap/pdftex_ndl14.map
	${RM} -f ${TEXMF_VARDIR}/web2c/updmap.log

	# Remove stray directores.
	for dir in							\
		${TEXMF_VARDIR}/web2c					\
		${TEXMF_VARDIR}/fonts/map/dvipdfm/updmap		\
		${TEXMF_VARDIR}/fonts/map/dvips/updmap			\
		${TEXMF_VARDIR}/fonts/map/pdftex/updmap
	do
		${RMDIR} -p "$dir" 2>/dev/null || ${TRUE}
	done
	;;

*)
	${ECHO} "Usage: ./+TETEX ADD|REMOVE [metadatadir]"
	;;
esac
exit $exitcode
EOF
	${CHMOD} +x ./+TETEX
	;;
esac

#
# Generate a +TEXCONFIG script sets the initial font and papersize
# settings for the teTeX installation.
#
case "${STAGE},$1" in
UNPACK,|UNPACK,+TEXCONFIG)
	${CAT} > ./+TEXCONFIG << 'EOF'
#!@SH@
#
# +TEXCONFIG - default font and papersize settings for teTeX
#
# Usage: ./+TEXCONFIG FONTS [fontdir]
#        ./+TEXCONFIG PAPER [papersize]

ECHO="@ECHO@"
TEST="@TEST@"

SELF=$0
ACTION=$1
: ${PKG_PREFIX=@PREFIX@}

TEXCONFIG="${PKG_PREFIX}/bin/texconfig"

exitcode=0
case $ACTION in
FONTS)
	# Set the default texfonts directory to the specified directory.
	VARTEXFONTS="${2-@VARTEXFONTS@}"
	${TEST} ! -x ${TEXCONFIG} ||
		${TEXCONFIG} font vardir "$VARTEXFONTS"
	${TEST} ! -x ${TEXCONFIG} ||
		${TEXCONFIG} font rw
	;;

PAPER)
	# Set the paper size for dvips and xdvi to the specified papersize.
	PAPERSIZE="${2-@PAPERSIZE@}"
	PAPERSIZE_dvips="$PAPERSIZE"
	PAPERSIZE_xdvi="$PAPERSIZE"
	case "$PAPERSIZE" in
	letter)	PAPERSIZE_xdvi="us" ;;
	us)	PAPERSIZE_dvips="letter" ;;
	esac
	${TEST} ! -x ${TEXCONFIG} ||
		${TEXCONFIG} dvips paper $PAPERSIZE_dvips
	${TEST} ! -x ${TEXCONFIG} ||
		${TEXCONFIG} xdvi paper $PAPERSIZE_xdvi
	;;

*)
	${ECHO} "Usage: ./+TEXCONFIG FONTS [fontdir]"
	${ECHO} "       ./+TEXCONFIG PAPER [papersize]"
	;;
esac
exit $exitcode
EOF
	${CHMOD} +x ./+TEXCONFIG
	;;
esac