#!/bin/sh
#
# $NetBSD: header,v 1.3 2014/12/30 15:13:20 wiz Exp $

SELF="$0"
PKGNAME="$1"
STAGE="$2"
shift 2

AWK="/usr/bin/awk"
BASENAME="/usr/bin/basename"
CAT="/bin/cat"
CHGRP="/usr/bin/chgrp"
CHMOD="/bin/chmod"
CHOWN="/usr/sbin/chown"
CMP="/usr/bin/cmp"
CP="/bin/cp"
DIRNAME="/usr/bin/dirname"
ECHO="echo"
ECHO_N="echo -n"
EGREP="/usr/bin/egrep"
EXPR="/bin/expr"
FALSE="false"
FIND="/usr/bin/find"
GREP="/usr/bin/grep"
GTAR=""
HEAD="/usr/bin/head"
ID="/usr/bin/id"
LINKFARM="/usr/sbin/linkfarm"
LN="/bin/ln"
LS="/bin/ls"
MKDIR="/bin/mkdir -p"
MV="mv -f"
PERL5="/usr/pkg/bin/perl"
PKG_ADMIN="/usr/sbin/pkg_admin"
PKG_INFO="/usr/sbin/pkg_info"
PWD_CMD="/bin/pwd"
RM="/bin/rm"
RMDIR="/bin/rmdir"
SED="/usr/bin/sed"
SETENV="/usr/bin/env"
SH="/bin/sh"
SORT="/usr/bin/sort"
SU="/usr/bin/su"
TEST="test"
TOUCH="/usr/bin/touch"
TR="/usr/bin/tr"
TRUE="true"
XARGS="/usr/bin/xargs"

CURDIR=`${PWD_CMD}`
: ${PKG_METADATA_DIR=${CURDIR}}
PKGBASE="standalone-tcsh"

LOCALBASE="/usr/pkg"
X11BASE="/usr/X11R7"
PREFIX="/"

PKG_SYSCONFBASE="//etc"
PKG_SYSCONFBASEDIR="//etc"
PKG_SYSCONFDIR="/usr/pkg/etc"
CONF_DEPENDS=""

CONF_IGNORE_FILES="*[~#] *.OLD *.orig *,v .pkgsrc */.pkgsrc"

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

# $NetBSD: shell,v 1.6 2010/05/07 14:29:36 jmmv Exp $
#
# Generate a +SHELL script that handles shell registration for the package.
#
case "${STAGE},$1" in
UNPACK,|UNPACK,+SHELL)
	${CAT} > ./+SHELL << 'EOF'
#!/bin/sh
#
# +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="/bin/cat"
CP="/bin/cp"
ECHO="echo"
GREP="/usr/bin/grep"
PWD_CMD="/bin/pwd"
RM="/bin/rm"
SED="/usr/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=/}
: ${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

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

# $NetBSD: install,v 1.5 2017/06/14 16:23:09 prlw1 Exp $

case ${STAGE} in
PRE-INSTALL)
	#
	# Unpack the helper scriptlets.
	#
	${SH} ${SELF} ${PKGNAME} UNPACK
	#
	# Require that necessary users and groups exist or else fail the
	# installation of the package.
	#
	${TEST} ! -x ./+USERGROUP ||
		{ ./+USERGROUP ADD ${PKG_METADATA_DIR}
		  if ./+USERGROUP CHECK-ADD ${PKG_METADATA_DIR}; then
			:
		  else
			exit 1
		  fi; }
	${TEST} ! -x ./+DIRS ||
		./+DIRS ADD ${PKG_METADATA_DIR}
	${TEST} ! -x ./+DIRS ||
		./+DIRS PERMS ${PKG_METADATA_DIR}
        ;;

POST-INSTALL)
	#
	# Rebuild the system run-time library search path database.
	#
	${TEST} ! -x ./+SHLIBS ||
		./+SHLIBS ADD ${PKG_METADATA_DIR}
	#
	# Copy configuration/support files into place.
	#
        ${TEST} ! -x ./+FILES ||
		./+FILES ADD ${PKG_METADATA_DIR}
        ${TEST} ! -x ./+FILES ||
		./+FILES PERMS ${PKG_METADATA_DIR}
	#
	# Set special permissions on any files/directories that need them.
	#
	${TEST} ! -x ./+PERMS ||
		./+PERMS ${PKG_METADATA_DIR}
	#
	# Update any fonts databases.
	#
	${TEST} ! -x ./+FONTS ||
		./+FONTS ${PKG_METADATA_DIR}

	#
	# Update any icon theme caches.
	#
	${TEST} ! -x ./+ICON_THEMES ||
		./+ICON_THEMES UPDATE ${PKG_METADATA_DIR}

	# Check for any missing bits after we're finished installing.
	#
	${TEST} ! -x ./+DIRS ||
		./+DIRS CHECK-ADD ${PKG_METADATA_DIR}
	${TEST} ! -x ./+DIRS ||
		./+DIRS CHECK-PERMS ${PKG_METADATA_DIR}
	${TEST} ! -x ./+FILES ||
		./+FILES CHECK-ADD ${PKG_METADATA_DIR}
	${TEST} ! -x ./+FILES ||
		./+FILES CHECK-PERMS ${PKG_METADATA_DIR}

	#
	# Register shells in /etc/shells.
	#
	${TEST} ! -x ./+SHELL ||
		./+SHELL ADD ${PKG_METADATA_DIR}
	${TEST} ! -x ./+SHELL ||
		./+SHELL CHECK-ADD ${PKG_METADATA_DIR}
	#
	# Register info files.
	#
	${TEST} ! -x ./+INFO_FILES ||
		./+INFO_FILES ADD ${PKG_METADATA_DIR}

  #
  # Register package into ocaml-findlib.
  #
  ${TEST} ! -x ./+OCAML_FINDLIB_REGISTER ||
    ./+OCAML_FINDLIB_REGISTER ADD ${PKG_METADATA_DIR}
	;;
esac

# $NetBSD: footer,v 1.1 2006/05/21 23:50:15 jlam Exp $

exit 0

# SHELL: bin/tcsh
