#!/usr/bin/bash
#
# $NetBSD: header,v 1.2 2006/07/19 22:26:26 jlam Exp $

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

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

CURDIR=`${PWD_CMD}`
: ${PKG_METADATA_DIR=${CURDIR}}
PKGBASE="mysql-server"

LOCALBASE="/opt/local"
X11BASE="/opt/local"
DEPOTBASE="/opt/local/packages"
PREFIX="/opt/local"
case ${PKG_PREFIX} in
${LOCALBASE}/*)	VIEW="${PKG_PREFIX#${LOCALBASE}/}" ;;
*)		VIEW="" ;;
esac

PKG_SYSCONFBASE="/opt/local/etc"
PKG_SYSCONFDEPOTBASE=""
PKG_SYSCONFBASEDIR="/opt/local/etc"
PKG_SYSCONFDIR="/opt/local/etc"
CONF_DEPENDS=""

case ${VIEW} in
"")	PKG_SYSCONFVIEWBASE="${PKG_SYSCONFBASE}" ;;
*)	PKG_SYSCONFVIEWBASE="${PKG_SYSCONFBASE}/${VIEW}" ;;
esac

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

PKG_INSTALLATION_TYPE="overwrite"

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: usergroup,v 1.3 2009/10/29 20:19:27 joerg Exp $
#
# Generate a +USERGROUP script that reference-counts users and groups
# that are required for the proper functioning of the package.
#
case "${STAGE},$1" in
UNPACK,|UNPACK,+USERGROUP)
	${CAT} > ./+USERGROUP << 'EOF'
#!/usr/bin/bash
#
# +USERGROUP - users and groups management script
#
# Usage: ./+USERGROUP ADD|REMOVE [metadatadir]
#        ./+USERGROUP CHECK-ADD|CHECK-REMOVE [metadatadir]
#
# This script supports two actions, ADD and REMOVE, that will add or
# remove the users and groups needed by the package associated with
# <metadatadir>.  The CHECK-ADD action will check whether any users or
# groups needed by the package are missing, and print an informative
# message noting those users and groups.  The CHECK-REMOVE action will
# check whether any users and groups needed by the package still exist,
# and print an informative message noting those users and groups.  The
# CHECK-ADD and CHECK-REMOVE actions return non-zero if they detect
# either missing or existing users/groups, respectively.
#
# Lines starting with "# USER: " or "# GROUP: " are data read by this
# script that name the users and groups that this package requires to
# exist to function correctly, e.g.
#
#	# USER: foo:foogrp::The Foomister
#	# GROUP: foogrp
#
# The USER lines are of the form:
#
#	user:group[:[userid][:[descr][:[home][:shell]]]]
#
# Only the user and group are required; everything else is optional,
# but the colons must be in the right places when specifying optional
# bits.
#
# The GROUP lines are of the form:
#
#	group[:groupid]
#
# Only the group is required; the groupid is optional.
#
AWK="/opt/local/bin/nawk"
CAT="/usr/bin/cat"
CHGRP="/usr/bin/chgrp"
CHMOD="/usr/bin/chmod"
CHOWN="/usr/bin/chown"
ECHO="echo"
GREP="/usr/xpg4/bin/grep"
LS="/usr/bin/ls"
MKDIR="/usr/bin/mkdir -p"
MV="/usr/bin/mv"
PWD_CMD="/bin/pwd"
RM="/usr/bin/rm"
RMDIR="/usr/bin/rmdir"
SED="/opt/local/bin/nbsed"
SORT="/usr/bin/sort"
TEST="test"
TRUE="true"

SELF=$0
ACTION=$1

CURDIR=`${PWD_CMD}`
PKG_METADATA_DIR="${2-${CURDIR}}"
: ${PKGNAME=${PKG_METADATA_DIR##*/}}
: ${PKG_DBDIR=${PKG_METADATA_DIR%/*}}
: ${PKG_REFCOUNT_DBDIR=${PKG_DBDIR}.refcount}

PKG_REFCOUNT_USERS_DBDIR="${PKG_REFCOUNT_DBDIR}/users"
PKG_REFCOUNT_GROUPS_DBDIR="${PKG_REFCOUNT_DBDIR}/groups"

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

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

listwrap()
{
	_length=$1
	_buffer=
	while read _line; do
		set -- $_line
		for _word; do
			case $_buffer in
			"")	_buffer="$_word" ;;
			*)	_buffer="$_buffer  $_word" ;;
			esac
			if ${TEST} ${#_buffer} -gt $_length; then
				${ECHO} "	$_buffer"
				_buffer=
			fi
		done
	done
	case $_buffer in
	"")	;;
	*)	${ECHO} "	$_buffer" ;;
	esac
}

# DO NOT CHANGE THE FOLLOWING LINE!
# platform-specific adduser/addgroup functions
# $NetBSD: usergroupfuncs,v 1.4 2013/04/03 09:35:19 jperkin Exp $
#
# Default implementations of user_exists() and group_exists() shell
# functions for checking the existence of users and groups, and of
# adduser() and addgroup() shell functions for adding users and groups.
# Assume there are NetBSD/Solaris-compatible versions of useradd(8) and
# groupadd(8) available through ${USERADD} and ${GROUPADD}, respectively.
#
# Platform-specific replacements for this file should be located at:
#
#	pkgsrc/mk/pkginstall/usergroupfuncs.${OPSYS}
#

# group_exists group [groupid]
#	Returns 0 if $group exists and has gid $groupid
#	Returns 1 if neither $group nor $groupid exist
#	Returns 2 if $group exists but $groupid does not match
#	Returns 3 if $group does not exist but $groupid does
#	Returns 4 for all errors
#
group_exists()
{
	_group="$1"; _groupid="$2"
	${TEST} -n "$_group" || return 4

	# Check using chgrp to work properly in an NSS/NIS environment.
	_tmpdir="./.pkginstall.$$"
	${MKDIR} -p $_tmpdir 2>/dev/null || return 4
	${CHMOD} 0700 $_tmpdir
	_testpath="$_tmpdir/group_exists"
	${ECHO} > $_testpath
	if ${CHGRP} $_group $_testpath >/dev/null 2>&1; then
		# $_group exists
		_id=`${LS} -ln $_testpath 2>/dev/null | ${AWK} '{ print $4 }'`
		${RM} -fr $_tmpdir
		${TEST} -n "$_groupid" || _groupid=$_id
		if ${TEST} "$_groupid" = "$_id"; then
			return 0
		fi
		return 2
	elif ${TEST} -z "$_groupid"; then
		${RM} -fr $_tmpdir
		# $_group doesn't exist and $_groupid is not set
		return 1
	elif ${CHGRP} $_groupid $_testpath >/dev/null 2>&1; then
		_name=`${LS} -l $_testpath 2>/dev/null | ${AWK} '{ print $4 }'`
		${RM} -fr $_tmpdir
		if ${TEST} "$_name" != "$_groupid"; then
			# $_group doesn't exist, but $_groupid exists
			return 3
		fi
		# neither $_group nor $_groupid exist
		return 1
	fi
	${RM} -fr $_tmpdir; return 4
}

# user_exists user [userid]
#	Returns 0 if $user exists and has uid $userid
#	Returns 1 if neither $user nor $userid exist
#	Returns 2 if $user exists but $userid does not match
#	Returns 3 if $user does not exist but $userid does
#	Returns 4 for all errors
#
user_exists()
{
	_user="$1"; _userid="$2"
	${TEST} -n "$_user" || return 4

	# Check using chown to work properly in an NSS/NIS environment.
	_tmpdir="./.pkginstall.$$"
	${MKDIR} -p $_tmpdir 2>/dev/null || return 4
	${CHMOD} 0700 $_tmpdir
	_testpath="$_tmpdir/user_exists"
	${ECHO} > $_testpath
	if ${CHOWN} $_user $_testpath >/dev/null 2>&1; then
		# $_user exists
		_id=`${LS} -ln $_testpath 2>/dev/null | ${AWK} '{ print $3 }'`
		${RM} -fr $_tmpdir
		${TEST} -n "$_userid" || _userid=$_id
		if ${TEST} "$_userid" = "$_id"; then
			return 0
		fi
		return 2
	elif ${TEST} -z "$_userid"; then
		${RM} -fr $_tmpdir
		# $_user doesn't exist and $_userid is not set
		return 1
	elif ${CHOWN} $_userid $_testpath >/dev/null 2>&1; then
		_name=`${LS} -l $_testpath 2>/dev/null | ${AWK} '{ print $3 }'`
		${RM} -fr $_tmpdir
		if ${TEST} "$_name" != "$_userid"; then
			# $_user doesn't exist, but $_userid exists
			return 3
		fi
		# neither $_user nor $_userid exist
		return 1
	fi
	${RM} -fr $_tmpdir; return 4
}

# adduser user group [userid] [descr] [home] [shell]
adduser()
{
	user="$1"; group="$2"; userid="$3"
	descr="$4"; home="$5" shell="$6"
	${TEST} $# -eq 6 || return 1
	${TEST} -n "$user" || return 2
	${TEST} -n "$group" || return 2

	USERADD="/usr/sbin/useradd"

	case $user in
	${PKGNAME%-[0-9]*})	descr_dflt="$user user" ;;
	*)			descr_dflt="${PKGNAME%-[0-9]*} $user user" ;;
	esac
	: ${descr:="$descr_dflt"}
	: ${home:="/nonexistent"}
	: ${shell:="/usr/bin/false"}

	if ${TEST} -n "${USERADD}" -a -x "${USERADD}"; then
		${ECHO} "${PKGNAME}: Creating user \`\`$user''"
		case $userid in
		"")
			${USERADD}					\
				-c "$descr" -d "$home" -s "$shell"	\
				-g $group $user
			;;
		*)
			${USERADD}					\
				-c "$descr" -d "$home" -s "$shell"	\
				-g $group -u $userid $user
			;;
		esac
		passwd -N $user
	fi
	return 0
}

# addgroup group [groupid]
addgroup()
{
	group="$1"; groupid="$2"
	${TEST} $# -eq 2 || return 1
	${TEST} -n "$group" || return 2

	GROUPADD="/usr/sbin/groupadd"

	if ${TEST} -n "${GROUPADD}" -a -x "${GROUPADD}"; then
		${ECHO} "${PKGNAME}: Creating group \`\`$group''"
		case $groupid in
		"")	${GROUPADD} $group ;;
		*)	${GROUPADD} -g $groupid $group ;;
		esac
	fi
	return 0
}

exitcode=0
case $ACTION in
ADD)
	${SED} -n "/^\# GROUP: /{s/^\# GROUP: //;p;}" ${SELF} | ${SORT} -u |
	{ while read line; do
		SAVEIFS="$IFS"; IFS=":"
		set -- $line
		group="$1"; groupid="$2"
		IFS="$SAVEIFS"
		case $group in
		"")	continue ;;
		esac
		shadow_dir="${PKG_REFCOUNT_GROUPS_DBDIR}/$group"
		preexist="$shadow_dir/+PREEXISTING"
		token="$shadow_dir/${PKGNAME}"
		if ${TEST} ! -d "$shadow_dir"; then
			${MKDIR} $shadow_dir
			group_exists $group $groupid
			case $? in
			0|2)	${ECHO} "${PKGNAME}" > $preexist ;;
			esac
		fi
		if ${TEST} -f "$token" && \
		   ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
			:
		else
			${ECHO} "${PKG_METADATA_DIR}" >> $token
		fi
		case ${_PKG_CREATE_USERGROUP} in
		yes)
			group_exists $group $groupid
			case $? in
			1)	addgroup "$group" "$groupid" ;;
			3)	case ${_PKGINSTALL_IGNORE_UIDGID} in
				yes)	addgroup "$group" "" ;;
				esac
				;;
			esac
			;;
		esac
	done; }
	${SED} -n "/^\# USER: /{s/^\# USER: //;p;}" ${SELF} | ${SORT} -u |
	{ while read line; do
		SAVEIFS="$IFS"; IFS=":"
		set -- $line
		user="$1"; group="$2"; userid="$3"
		descr="$4"; home="$5" shell="$6"
		IFS="$SAVEIFS"
		case $user in
		"")	continue ;;
		esac
		case $group in
		"")	continue ;;
		esac
		shadow_dir="${PKG_REFCOUNT_USERS_DBDIR}/$user"
		preexist="$shadow_dir/+PREEXISTING"
		token="$shadow_dir/${PKGNAME}"
		if ${TEST} ! -d "$shadow_dir"; then
			${MKDIR} $shadow_dir
			user_exists $user $userid
			case $? in
			0|2)	${ECHO} "${PKGNAME}" > $preexist ;;
			esac
		fi
		if ${TEST} -f "$token" && \
		   ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
			:
		else
			${ECHO} "${PKG_METADATA_DIR}" >> $token
		fi
		case ${_PKG_CREATE_USERGROUP} in
		yes)
			group_exists $group || continue
			user_exists $user $userid
			case $? in
			1)	adduser "$user" "$group" "$userid"	\
					"$descr" "$home" "$shell"
				;;
			3)	case ${_PKGINSTALL_IGNORE_UIDGID} in
				yes)
					adduser "$user" "$group" ""	\
						"$descr" "$home" "$shell"
					;;
				esac
				;;
			esac
			;;
		esac
	done; }
	;;

REMOVE)
	${SED} -n "/^\# USER: /{s/^\# USER: //;p;}" ${SELF} | ${SORT} -u |
	{ while read line; do
		SAVEIFS="$IFS"; IFS=":"
		set -- $line
		user="$1"; group="$2"; userid="$3"
		descr="$4"; home="$5" shell="$6"
		IFS="$SAVEIFS"
		case $user in
		"")	continue ;;
		esac
		shadow_dir="${PKG_REFCOUNT_USERS_DBDIR}/$user"
		preexist="$shadow_dir/+PREEXISTING"
		token="$shadow_dir/${PKGNAME}"
		tokentmp="$token.tmp.$$"
		if ${TEST} -f "$token" && \
		   ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
			${CAT} "$token" | ${GREP} -v "^${PKG_METADATA_DIR}$" > $tokentmp
			case `${CAT} $tokentmp | ${SED} -n "$="` in
			"")
				${RM} -f $preexist $token $token.tmp.*
				${RMDIR} -p $shadow_dir 2>/dev/null || ${TRUE}
				;;
			*)
				${MV} -f $tokentmp $token
				;;
			esac
		fi
	done; }
	${SED} -n "/^\# GROUP: /{s/^\# GROUP: //;p;}" ${SELF} | ${SORT} -u |
	{ while read line; do
		SAVEIFS="$IFS"; IFS=":"
		set -- $line
		group="$1"; groupid="$2"
		IFS="$SAVEIFS"
		case $group in
		"")	continue ;;
		esac
		shadow_dir="${PKG_REFCOUNT_GROUPS_DBDIR}/$group"
		preexist="$shadow_dir/+PREEXISTING"
		token="$shadow_dir/${PKGNAME}"
		tokentmp="$token.tmp.$$"
		if ${TEST} -f "$token" && \
		   ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
			${CAT} "$token" | ${GREP} -v "^${PKG_METADATA_DIR}$" > $tokentmp
			case `${CAT} $tokentmp | ${SED} -n "$="` in
			"")
				${RM} -f $preexist $token $token.tmp.*
				${RMDIR} -p $shadow_dir 2>/dev/null || ${TRUE}
				;;
			*)
				${MV} -f $tokentmp $token
				;;
			esac
		fi
	done; }
	;;

CHECK-ADD)
	${SED} -n "/^\# GROUP: /{s/^\# GROUP: //;p;}" ${SELF} | ${SORT} -u |
	{ while read line; do
		SAVEIFS="$IFS"; IFS=":"
		set -- $line
		group="$1"; groupid="$2"
		IFS="$SAVEIFS"
		case $group in
		"")	continue ;;
		*)	group_exists $group $groupid
			case $? in
			0)	continue ;;
			2)	case ${_PKGINSTALL_IGNORE_UIDGID} in
				yes)	continue ;;
				esac
				;;
			esac
			;;
		esac
		case "$printed_header" in
		yes)	;;
		*)	printed_header=yes
			${ECHO} "==========================================================================="
			${ECHO} "The following groups need to be created for ${PKGNAME}:"
			${ECHO} ""
			;;
		esac
		case $groupid in
		"")	${ECHO} "	$group" ;;
		*)	${ECHO} "	$group (gid = $groupid)"
			${ECHO} ""
			${ECHO} "If you wish to ignore this check and either re-use an existing '$group' group"
			${ECHO} "or create a new one with a system-assigned gid then set"
			${ECHO} ""
			${ECHO} "	PKGINSTALL_IGNORE_UIDGID=yes"
			${ECHO} ""
			${ECHO} "in the environment."
			;;
		esac
	done
	case "$printed_header" in
	yes)	${ECHO} ""
		${ECHO} "==========================================================================="
		exit 1
		;;
	esac; }
	${TEST} $? -eq 0 || exitcode=1
	${SED} -n "/^\# USER: /{s/^\# USER: //;p;}" ${SELF} | ${SORT} -u |
	{ while read line; do
		SAVEIFS="$IFS"; IFS=":"
		set -- $line
		user="$1"; group="$2"; userid="$3"
		descr="$4"; home="$5" shell="$6"
		IFS="$SAVEIFS"
		case $user in
		"")	continue ;;
		*)	user_exists $user $userid
			case $? in
			0)	continue ;;
			2)	case ${_PKGINSTALL_IGNORE_UIDGID} in
				yes)	continue ;;
				esac
				;;
			esac
			;;
		esac
		case "$printed_header" in
		yes)	;;
		*)	printed_header=yes
			${ECHO} "==========================================================================="
			${ECHO} "The following users need to be created for ${PKGNAME}:"
			${ECHO} ""
			;;
		esac
		: ${home:="/nonexistent"}
		: ${shell:="/usr/bin/false"}
		case $userid in
		"")	${ECHO} "	$user: $group, $home, $shell" ;;
		*)	${ECHO} "	$user (uid = $userid): $group, $home, $shell"
			${ECHO} ""
			${ECHO} "If you wish to ignore this check and either re-use an existing '$user' user"
			${ECHO} "or create a new one with a system-assigned uid then set"
			${ECHO} ""
			${ECHO} "	PKGINSTALL_IGNORE_UIDGID=yes"
			${ECHO} ""
			${ECHO} "in the environment."
			;;
		esac
	done
	case "$printed_header" in
	yes)	${ECHO} ""
		${ECHO} "==========================================================================="
		exit 1
		;;
	esac; }
	${TEST} $? -eq 0 || exitcode=1
	;;

CHECK-REMOVE)
	${SED} -n "/^\# USER: /{s/^\# USER: //;p;}" ${SELF} | ${SORT} -u |
	{ while read line; do
		SAVEIFS="$IFS"; IFS=":"
		set -- $line
		user="$1"; group="$2"; userid="$3"
		descr="$4"; home="$5" shell="$6"
		IFS="$SAVEIFS"
		case $user in
		"")	continue ;;
		*)	user_exists $user $userid
			case $? in
			0|2)	continue ;;
			esac
			;;
		esac
		shadow_dir="${PKG_REFCOUNT_USERS_DBDIR}/$user"
		${TEST} ! -d "$shadow_dir" || continue	# refcount isn't zero
		existing_users="$existing_users $user"
	done
	case $existing_users in
	"")	;;
	*)	${ECHO} "==========================================================================="
		${ECHO} "The following users are no longer being used by ${PKGNAME},"
		${ECHO} "and they can be removed if no other software is using them:"
		${ECHO} ""
		${ECHO} "$existing_users" | listwrap 40
		${ECHO} ""
		${ECHO} "==========================================================================="
		exit 1
		;;
	esac; }
	${TEST} $? -eq 0 || exitcode=1
	${SED} -n "/^\# GROUP: /{s/^\# GROUP: //;p;}" ${SELF} | ${SORT} -u |
	{ while read line; do
		SAVEIFS="$IFS"; IFS=":"
		set -- $line
		group="$1"; groupid="$2"
		IFS="$SAVEIFS"
		case $group in
		"")	continue ;;
		*)	group_exists $group $groupid
			case $? in
			0|2)	continue ;;
			esac
			;;
		esac
		shadow_dir="${PKG_REFCOUNT_GROUPS_DBDIR}/$group"
		${TEST} ! -d "$shadow_dir" || continue	# refcount isn't zero
		existing_groups="$existing_groups $group"
	done
	case $existing_groups in
	"")	;;
	*)	${ECHO} "==========================================================================="
		${ECHO} "The following groups are no longer being used by ${PKGNAME},"
		${ECHO} "and they can be removed if no other software is using them:"
		${ECHO} ""
		${ECHO} "$existing_groups" | listwrap 40
		${ECHO} ""
		${ECHO} "==========================================================================="
		exit 1
		;;
	esac; }
	${TEST} $? -eq 0 || exitcode=1
	;;

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

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

# $NetBSD: install,v 1.2 2007/07/18 18:01:03 jlam 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; }
	#
	# Create package directories at pre-install time.
	#
	if [ "${PKG_INSTALLATION_TYPE}" = "pkgviews" -a			\
	     "${_PKG_CONFIG}" = "yes" -a -n "${CONF_DEPENDS}" ]; then
		pkg=`${PKG_ADMIN} -b -d ${DEPOTBASE} -s "" lsbest "${CONF_DEPENDS}"`
		sysconfdir=`${PKG_INFO} -B -K ${DEPOTBASE} $pkg |	\
			${AWK} '/^PKG_SYSCONFDIR=/ {			\
				gsub("^PKG_SYSCONFDIR=[ 	]*", ""); \
				print;					\
			}'						\
		`
		if [ -d $sysconfdir -a ! -d ${PKG_SYSCONFDIR} ]; then
			${MKDIR} -p `${DIRNAME} ${PKG_SYSCONFDIR}`
			${LN} -sf $sysconfdir ${PKG_SYSCONFDIR}
		fi
	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}

	# 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}
	;;

VIEW-INSTALL)
	#
	# 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}

	# If ${PKG_SYSCONFBASE} points outside of ${PREFIX}, then add the
	# package config files to the proper view.
	#
	if [ "${_PKG_CONFIG}" = "yes" -a -n "${PKG_SYSCONFDEPOTBASE}" ]; then
		${SETENV} PLIST_IGNORE_FILES="${CONF_IGNORE_FILES}" \
			${LINKFARM} -t ${PKG_SYSCONFVIEWBASE} -d ${PKG_SYSCONFDEPOTBASE} ${PKGNAME}
	fi
	;;
esac

# $NetBSD$

case ${STAGE} in
POST-INSTALL)
	if [ ! -f /var/log/mysql/error.log ]; then
		mkdir -p /var/log/mysql
		touch /var/log/mysql/error.log
		chown -R mysql:mysql /var/log/mysql
	fi
	if [ -d /var/mysql/mysql ]; then
		echo "Skipping MySQL database initialization, as databases already exist."
	else
		# Initialize MySQL
		${PKG_PREFIX}/bin/mysql_install_db --datadir=/var/mysql --user=mysql
	fi
	;;
esac
# $NetBSD$

case ${STAGE} in
POST-INSTALL)
	logadm -C 5 -c -s 100m -z 1 -w '/var/log/mysql/*.log'
	;;
DEINSTALL)
	logadm -r '/var/log/mysql/*.log'
	;;
esac
# $NetBSD$

case ${STAGE} in
POST-INSTALL)
	grep '^mysql' /etc/project >/dev/null 2>&1
	if [ $? != 0 ]; then
		projid=`id -u mysql`
		LANG=C projadd -p ${projid} -U mysql -G mysql -K "process.max-file-descriptor=(basic,15000,deny)" mysql
	fi
	;;
esac
case ${STAGE} in
POST-INSTALL)
	cat <<EOF
============================================================================
This package has SMF support.  You may use svcadm(1M) to 'enable', 'disable'
or 'restart' services.  To enable the instance(s) for this package, run:

EOF
	for svc in default; do
		cat <<EOF
	/usr/sbin/svcadm enable svc:/pkgsrc/mysql:${svc}
EOF
	done
	cat <<EOF

Use svcs(1) to check on service status.  See smf(5) for more information.
EOF
	if [ -z "${PKG_SKIP_SMF}" ]; then
		/usr/sbin/svccfg import ${PKG_PREFIX}/lib/svc/manifest/mysql.xml
	else
		cat <<EOF

The PKG_SKIP_SMF variable was set, automatic import of SMF manifests was
skipped.  You must import the SMF manifest first with:

	/usr/sbin/svccfg import ${PKG_PREFIX}/lib/svc/manifest/mysql.xml

EOF
	fi
	cat <<EOF
============================================================================
EOF
	;;
esac
# $NetBSD: install-post,v 1.1 2006/05/21 23:50:15 jlam Exp $

# Ensure that the VIEW-INSTALL action is called for overwrite packages.
# This is here to ensure that it's the final POST-INSTALL action (after
# any INSTALL_TEMPLATE POST-INSTALL actions).
#
case ${STAGE} in
POST-INSTALL)
	if [ "${PKG_INSTALLATION_TYPE}" = "overwrite" ]; then
		${SETENV} PKG_PREFIX="${PKG_PREFIX}" \
			$0 ${PKGNAME} VIEW-INSTALL
        fi
	;;
esac

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

exit 0

# GROUP: mysql:924
# USER: mysql:mysql:924:MySQL database administrator:/var/mysql:/usr/bin/bash
