# $NetBSD: dirs,v 1.3 2006/12/15 12:46:24 martti Exp $
#
# Generate a +DIRS script that reference counts directories that are
# required for the proper functioning of the package.
#
case "${STAGE},$1" in
UNPACK,|UNPACK,+DIRS)
	${CAT} > ./+DIRS << 'EOF'
#!@SH@
#
# +DIRS - reference-counted directory management script
#
# Usage: ./+DIRS ADD|REMOVE|PERMS [metadatadir]
#        ./+DIRS CHECK-ADD|CHECK-REMOVE|CHECK-PERMS [metadatadir]
#
# This script supports two actions, ADD and REMOVE, that will add or
# remove the directories needed by the package associated with
# <metadatadir>.  The CHECK-ADD action will check whether any directories
# needed by the package are missing, and print an informative message
# noting those directories.  The CHECK-REMOVE action will check whether
# any directories needed by the package still exist, and print an
# informative message noting those directories.  The CHECK-ADD and
# CHECK-REMOVE actions return non-zero if they detect either missing
# or existing directories, respectively.  The PERMS action will correct
# any ownership or permission discrepancies between the existing
# directories and the data in this script, and the CHECK-PERMS action
# will check whether any directories have the wrong ownership or
# permission and print an informative message noting those directories.
# The CHECK-PERMS action will return non-zero if it detects directories
# with wrong ownership or permissions.
#
# Lines starting with "# DIR: " are data read by this script that
# name the directories that this package requires to exist to function
# correctly, e.g.
#
#	# DIR: /etc/foo m
#	# DIR: /var/log/foo/tmp mo 0700 foo-user foo-group
#	# DIR: share/foo-plugins fm
#
# For each DIR entry, if the directory path is relative, then it is taken
# to be relative to ${PKG_PREFIX}.
#
# The second field in each DIR entry is a set of flags with the following
# meanings:
#
#	f	ignore ${PKG_CONFIG}
#	m	create (make) the directory when ADDing
#	o	directory is owned by the package
#
AWK="@AWK@"
CAT="@CAT@"
CHGRP="@CHGRP@"
CHMOD="@CHMOD@"
CHOWN="@CHOWN@"
ECHO="@ECHO@"
GREP="@GREP@"
LS="@LS@"
MKDIR="@MKDIR@"
MV="@MV@"
PWD_CMD="@PWD_CMD@"
RM="@RM@"
RMDIR="@RMDIR@"
SED="@SED@"
SORT="@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_PREFIX=@PREFIX@}

PKG_REFCOUNT_DIRS_DBDIR="${PKG_REFCOUNT_DBDIR}/dirs"

case "${PKG_CONFIG:-@PKG_CONFIG@}" 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=no
	;;
esac
case "${PKG_CONFIG_PERMS:-@PKG_CONFIG_PERMS@}" in
[Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
	_PKG_CONFIG_PERMS=yes
	;;
[Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
	_PKG_CONFIG_PERMS=no
	;;
esac

exitcode=0
case $ACTION in
ADD)
	${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -u |
	while read dir d_flags d_mode d_user d_group; do
		case $dir in
		"")	continue ;;
		[!/]*)	dir="${PKG_PREFIX}/$dir" ;;
		esac
		case $d_flags in
		*m*)	;;
		*)	continue ;;
		esac
		shadow_dir="${PKG_REFCOUNT_DIRS_DBDIR}$dir"
		perms="$shadow_dir/+PERMISSIONS"
		preexist="$shadow_dir/+PREEXISTING"
		token="$shadow_dir/${PKGNAME}"
		if ${TEST} ! -d "$shadow_dir"; then
			${MKDIR} $shadow_dir
			${TEST} ! -d "$dir" ||
				${ECHO} "${PKGNAME}" > $preexist
		fi
		if ${TEST} -f "$token" && \
		   ${GREP} "^${PKG_METADATA_DIR}$" $token >/dev/null; then
			:
		else
			${ECHO} "${PKG_METADATA_DIR}" >> $token
		fi
		case $d_mode$d_user$d_group in
		"")	;;
		*)	${ECHO} "$d_user $d_group $d_mode" > $perms ;;
		esac
		case $d_flags:$_PKG_CONFIG in
		*f*:*|*:yes)
			${MKDIR} $dir
			case $d_user in
			"")	;;
			*)	${CHOWN} $d_user $dir ;;
			esac
			case $d_group in
			"")	;;
			*)	${CHGRP} $d_group $dir ;;
			esac
			case $d_mode in
			"")	;;
			*)	${CHMOD} $d_mode $dir ;;
			esac
			;;
		esac
	done
	;;

REMOVE)
	${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -ru |
	while read dir d_flags d_mode d_user d_group; do
		case $dir in
		"")	continue ;;
		[!/]*)	dir="${PKG_PREFIX}/$dir" ;;
		esac
		case $d_flags in
		*m*)	;;
		*)	continue ;;
		esac
		shadow_dir="${PKG_REFCOUNT_DIRS_DBDIR}$dir"
		perms="$shadow_dir/+PERMISSIONS"
		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
			"")
				${TEST} -f "$preexist" ||
					{ case $d_flags:$_PKG_CONFIG in
					  *f*:*|*:yes)
						${RMDIR} -p $dir 2>/dev/null || ${TRUE};
						;;
					  esac; }
				${RM} -f $perms $preexist $token $token.tmp.*
				${RMDIR} -p $shadow_dir 2>/dev/null || ${TRUE}
				;;
			*)
				${MV} -f $tokentmp $token
				;;
			esac
		fi
	done
	;;

PERMS)
	${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -u |
	while read dir d_flags d_mode d_user d_group; do
		case $_PKG_CONFIG:$_PKG_CONFIG_PERMS in
		yes:yes)	;;
		*)		continue ;;
		esac
		case $dir in
		"")	continue ;;
		[!/]*)	dir="${PKG_PREFIX}/$dir" ;;
		esac
		case $d_user in
		"")	;;
		*)	${CHOWN} $d_user $dir ;;
		esac
		case $d_group in
		"")	;;
		*)	${CHGRP} $d_group $dir ;;
		esac
		case $d_mode in
		"")	;;
		*)	${CHMOD} $d_mode $dir ;;
		esac
	done
	;;

CHECK-ADD)
	${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -u |
	{ while read dir d_flags d_mode d_user d_group; do
		case $dir in
		"")	continue ;;
		[!/]*)	dir="${PKG_PREFIX}/$dir" ;;
		esac
		${TEST} ! -d "$dir" || continue
		case $d_flags in
		*m*)	;;
		*)	continue ;;
		esac
		case "$printed_header" in
		yes)	;;
		*)	printed_header=yes
			${ECHO} "==========================================================================="
			${ECHO} "The following directories should be created for ${PKGNAME}:"
			${ECHO} ""
			;;
		esac
		case $d_user:$d_group:$d_mode in
		[!:]*:[!:]*:[!:]*)
			${ECHO} "	$dir (m=$d_mode, o=$d_user, g=$d_group)"
			;;
		*)
			${ECHO} "	$dir"
			;;
		esac
	done
	case "$printed_header" in
	yes)	${ECHO} ""
		${ECHO} "==========================================================================="
		exit 1
		;;
	esac; }
	${TEST} $? -eq 0 || exitcode=1
	;;

CHECK-REMOVE)
	${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -ru |
	{ while read dir d_flags d_mode d_user d_group; do
		case $dir in
		"")	continue ;;
		[!/]*)	dir="${PKG_PREFIX}/$dir" ;;
		esac
		${TEST} -d "$dir" || continue
		case $d_flags in
		*o*)	;;
		*)	continue ;;
		esac
		shadow_dir="${PKG_REFCOUNT_DIRS_DBDIR}$dir"
		${TEST} ! -d "$shadow_dir" || continue	# refcount isn't zero
		case "$printed_header" in
		yes)	;;
		*)	printed_header=yes
			${ECHO} "==========================================================================="
			${ECHO} "The following directories are no longer being used by ${PKGNAME},"
			${ECHO} "and they can be removed if no other packages are using them:"
			${ECHO} ""
			;;
		esac
		${ECHO} "	$dir"
	done
	case "$printed_header" in
	yes)	${ECHO} ""
		${ECHO} "==========================================================================="
		exit 1
		;;
	esac; }
	${TEST} $? -eq 0 || exitcode=1
	;;

CHECK-PERMS)
	tmpdir="./.pkginstall.$$"
	${MKDIR} -p $tmpdir 2>/dev/null || exit 1
	${CHMOD} 0700 $tmpdir
	${SED} -n "/^\# DIR: /{s/^\# DIR: //;p;}" ${SELF} | ${SORT} -ru |
	{ while read dir d_flags d_mode d_user d_group; do
		case $dir in
		"")	continue ;;
		[!/]*)	dir="${PKG_PREFIX}/$dir" ;;
		esac
		${TEST} -d "$dir" || continue
		case $d_user:$d_group:$d_mode in
		::)	continue ;;
		esac

		perms=`${LS} -ld $dir | ${AWK} '{ print $1":"$3":"$4 }'`
		testpath="$tmpdir/dir_perms"
		${MKDIR} -p $testpath
		${CHMOD} $d_mode $testpath 2>/dev/null
		longmode=`${LS} -ld $testpath | ${AWK} '{ print $1 }'`
		case $d_mode:$d_user:$d_group in
		:[!:]*:)
			case "$perms" in
			*:$d_user:*)	continue ;;
			esac
			;;
		:[!:]*:[!:]*)
			case "$perms" in
			*:$d_user:$d_group)	continue ;;
			esac
			;;
		[!:]*::)
			case "$perms" in
			$longmode:*:*)	continue ;;
			esac
			;;
		[!:]*:[!:]*:)
			case "$perms" in
			$longmode:$d_user:*)	continue ;;
			esac
			;;
		[!:]*:[!:]*:[!:]*)
			case "$perms" in
			$longmode:$d_user:$d_group)	continue ;;
			esac
			;;
		esac

		case "$printed_header" in
		yes)	;;
		*)	printed_header=yes
			${ECHO} "==========================================================================="
			${ECHO} "The following directories are used by ${PKGNAME} and"
			${ECHO} "have the wrong ownership and/or permissions:"
			${ECHO} ""
			;;
		esac
		case $d_mode:$d_user:$d_group in
		[!:]*::)
			${ECHO} "	$dir (m=$d_mode)"
			;;
		[!:]*:[!:]*:)
			${ECHO} "	$dir (m=$d_mode, o=$d_user)"
			;;
		[!:]*:[!:]*:[!:]*)
			${ECHO} "	$dir (m=$d_mode, o=$d_user, g=$d_group)"
			;;
		esac
	done
	case "$printed_header" in
	yes)	${ECHO} ""
		${ECHO} "==========================================================================="
		exit 1
		;;
	esac; }
	${RM} -fr $tmpdir
	${TEST} $? -eq 0 || exitcode=1
	;;

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

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