# $NetBSD: info-files,v 1.4 2007/07/12 19:41:46 jlam Exp $
#
# Generate an +INFO_FILES script that handles info file registration for
# the package.
#
case "${STAGE},$1" in
UNPACK,|UNPACK,+INFO_FILES)
	${CAT} > ./+INFO_FILES << 'EOF'
#!/bin/sh
#
# +INFO_FILES - info file registration management script
#
# Usage: ./+INFO_FILES ADD|REMOVE [metadatadir]
#
# This script supports two actions, ADD and REMOVE, that will add or
# remove entries for info files from the package associated with
# <metadatadir> from the info index files (the "dir" file in the
# same directory as the info files).
#
# Lines starting with "# INFO: " are data read by this script that
# name the info files and directory containing the "dir" index that will
# that will be updated.  If the directory is not specified, then the
# "dir" index is assumed to be in the same directory as the info file.
#
#	# INFO: /usr/pkg/info/bar.info /usr/pkg/info
#	# INFO: /usr/pkg/info/baz.info /usr/pkg/info
#
# For each INFO entry, if the path is relative, that it is taken to be
# relative to ${PKG_PREFIX}.
#

ECHO="echo"
GREP="/usr/bin/grep"
INSTALL_INFO="/usr/bin/install-info"
MKDIR="/bin/mkdir -p"
PWD_CMD="/bin/pwd"
RM="/bin/rm"
RMDIR="/bin/rmdir"
SED="/usr/bin/sed"
SORT="/usr/bin/sort"
TEST="test"

SELF=$0
ACTION=$1

CURDIR=`${PWD_CMD}`
PKG_METADATA_DIR="${2-${CURDIR}}"
: ${PKGNAME=${PKG_METADATA_DIR##*/}}
: ${PKG_PREFIX=/usr/pkg}

case "${INFO_FILES_VERBOSE:-NO}" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
	echo="${ECHO}"
	;;
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
	echo=":"
	;;
esac

exitcode=0
case $ACTION in
ADD)
	${SED} -n "/^\# INFO: /{s/^\# INFO: //;p;}" ${SELF} | ${SORT} -u |
	while read file infodir; do
		case $file in
		"")	continue ;;
		[!/]*)	file="${PKG_PREFIX}/$file" ;;
		esac

		if ${TEST} ! -f "$file"; then
			:
		else
			case $infodir in
			"")	infodir="${file%/*}" ;;
			[!/]*)	infodir="${PKG_PREFIX}/$infodir" ;;
			esac
			infoindex="$infodir/dir"
			nentries="`${GREP} -c '^\*' $infoindex 2>/dev/null`"
			case "$nentries" in
			[0-9]*)	${TEST} $nentries -gt 0 || ${RM} $infoindex ;;
			esac
			$echo "${PKGNAME}: registering info file $file"
			${MKDIR} -p "$infodir"
			${INSTALL_INFO} --info-dir="$infodir" --delete $file >/dev/null 2>&1
			${INSTALL_INFO} --info-dir="$infodir" $file >/dev/null 2>&1
		fi
	done
	;;

REMOVE)
	${SED} -n "/^\# INFO: /{s/^\# INFO: //;p;}" ${SELF} | ${SORT} -u |
	while read file infodir; do
		case $file in
		"")	continue ;;
		[!/]*)	file="${PKG_PREFIX}/$file" ;;
		esac

		if ${TEST} ! -f "$file"; then
			:
		else
			case $infodir in
			"")	infodir="${file%/*}" ;;
			[!/]*)	infodir="${PKG_PREFIX}/$infodir" ;;
			esac
			infoindex="$infodir/dir"
			$echo "${PKGNAME}: unregistering info file $file"
			${INSTALL_INFO} --info-dir="$infodir" --delete $file >/dev/null 2>&1
			nentries="`${GREP} -c '^\*' $infoindex 2>/dev/null`"
			case "$nentries" in
			[0-9]*)	${TEST} $nentries -gt 1 || ${RM} $infoindex ;;
			esac
			${RMDIR} -p "$infodir" 2>/dev/null || ${TRUE}
		fi
	done
	;;
esac
exit $exitcode

EOF
	${SED} -n "/^\# INFO: /p" ${SELF} >> ./+INFO_FILES
	${CHMOD} +x ./+INFO_FILES
	;;
esac

