#!/tmp/pkgsrc/bootstrap/work/bin/pdksh
#
# +SHELL - shell registration script
#
# Usage: ./+SHELL ADD|REMOVE [metadatadir]
#        ./+SHELL CHECK-ADD|CHECK-REMOVE [metadatadir]
#
# This script supports two actions, ADD and REMOVE, that will add or
# remove shell paths from the shell database in /etc/shells.  The
# CHECK-ADD action will check whether shell paths provided by the
# package are missing from the shell database, and print an informative
# message noting those shell paths.  The CHECK-REMOVE action will check
# whether shell paths provided by the package are still present in the
# shell database, and print an informative message noting those shell
# paths.  The CHECK-ADD and CHECK-REMOVE actions return non-zero if
# they detect either missing or existing paths, respectively.
#
# Lines starting with "# SHELL: " are data read by this script that
# name the shell paths that should be added or removed from the shell
# database.  If the path is relative, then it is taken to be relative
# to ${PKG_PREFIX}.
#
#	# SHELL: bin/pdksh
#
CAT="/usr/bin/cat"
CP="/bin/cp"
ECHO="echo"
GREP="/usr/xpg4/bin/grep"
PWD_CMD="/bin/pwd"
RM="/usr/bin/rm"
SED="/tmp/pkgsrc/bootstrap/work/bin/sed"
SORT="/usr/bin/sort"
TEST="test"
TRUE="true"
TOUCH="/usr/bin/touch"

SELF=$0
ACTION=$1

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

case "${PKG_REGISTER_SHELLS:-YES}" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
	_PKG_REGISTER_SHELLS=yes
	;;
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
	_PKG_REGISTER_SHELLS=no
	;;
esac

exitcode=0
case $ACTION in
ADD)
	${SED} -n "/^\# SHELL: /{s/^\# SHELL: //;p;}" ${SELF} | ${SORT} -u |
	while read shell; do
		case ${_PKG_REGISTER_SHELLS} in
		no)	continue ;;
		esac
		case $shell in
		/*)	continue ;;
		*)	if [ ${PKG_PREFIX} = / ] ; then
				shell="/$shell"
			else
				shell="${PKG_PREFIX}/$shell"
			fi ;;
		esac
		${TEST} -f "$shell" || continue

		shelldb="/etc/shells"
		${TEST} -f "$shelldb" || continue
		if ${TEST} -f "$shelldb" && \
		   ${GREP} "^$shell" $shelldb >/dev/null; then
			:
		else
			${ECHO} "${PKGNAME}: adding $shell to $shelldb"
                	${TOUCH} $shelldb
                	${CP} $shelldb $shelldb.pkgsrc."$$"
			{ ${CAT} $shelldb.pkgsrc."$$"; ${ECHO} "$shell"; } > $shelldb
                	${RM} $shelldb.pkgsrc."$$"
		fi
	done
	;;

REMOVE)
	${SED} -n "/^\# SHELL: /{s/^\# SHELL: //;p;}" ${SELF} | ${SORT} -u |
	while read shell; do
		case ${_PKG_REGISTER_SHELLS} in
		no)	continue ;;
		esac
		case $shell in
		/*)	continue ;;
		*)	if [ ${PKG_PREFIX} = / ] ; then
				shell="/$shell"
			else
				shell="${PKG_PREFIX}/$shell"
			fi ;;
		esac
		${TEST} -f "$shell" || continue

		shelldb="/etc/shells"
		if ${TEST} -f "$shelldb" && \
		   ${GREP} "^$shell" $shelldb >/dev/null; then
			${ECHO} "${PKGNAME}: removing $shell from $shelldb"
                	${TOUCH} $shelldb
                	${CP} $shelldb $shelldb.pkgsrc."$$"
			{ ${GREP} -v "^$shell" $shelldb.pkgsrc."$$" || ${TRUE}; } > $shelldb
                	${RM} $shelldb.pkgsrc."$$"
		fi
	done
	;;

CHECK-ADD)
	${SED} -n "/^\# SHELL: /{s/^\# SHELL: //;p;}" ${SELF} | ${SORT} -u |
	{ while read shell; do
		case $shell in
		/*)	continue ;;
		*)	if [ ${PKG_PREFIX} = / ] ; then
				shell="/$shell"
			else
				shell="${PKG_PREFIX}/$shell"
			fi ;;
		esac
		${TEST} -f "$shell" || continue

		shelldb="/etc/shells"
		if ${TEST} -f "$shelldb" && \
		   ${GREP} "^$shell" $shelldb >/dev/null; then
			:
		else
			case "$printed_header" in
			yes)	;;
			*)	printed_header=yes
				${ECHO} "==========================================================================="
				${ECHO} "The following lines can be added to $shelldb:"
				${ECHO} ""
				;;
			esac
			${ECHO} "	$shell"
		fi
	done
	case "$printed_header" in
	yes)	${ECHO} ""
		${ECHO} "==========================================================================="
		exit 1
		;;
	esac; }
	${TEST} $? -eq 0 || exitcode=1
	;;

CHECK-REMOVE)
	${SED} -n "/^\# SHELL: /{s/^\# SHELL: //;p;}" ${SELF} | ${SORT} -u |
	{ while read shell; do
		case $shell in
		/*)	continue ;;
		*)	if [ ${PKG_PREFIX} = / ] ; then
				shell="/$shell"
			else
				shell="${PKG_PREFIX}/$shell"
			fi ;;
		esac
		${TEST} -f "$shell" || continue

		shelldb="/etc/shells"
		if ${TEST} -f "$shelldb" && \
		   ${GREP} "^$shell" $shelldb >/dev/null; then
			case "$printed_header" in
			yes)	;;
			*)	printed_header=yes
				${ECHO} "==========================================================================="
				${ECHO} "The following lines can be removed from $shelldb:"
				${ECHO} ""
				;;
			esac
			${ECHO} "	$shell"
		fi
	done
	case "$printed_header" in
	yes)	${ECHO} ""
		${ECHO} "==========================================================================="
		exit 1
		;;
	esac; }
	${TEST} $? -eq 0 || exitcode=1
	;;

*)
	${ECHO} "Usage: ./+SHELL ADD|REMOVE [metadatadir]"
	${ECHO} "       ./+SHELL CHECK-ADD|CHECK-REMOVE [metadatadir]"
	;;
esac
exit $exitcode

# SHELL: bin/pdksh
