#!/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/pkg/sbin/linkfarm"
LN="/bin/ln"
LS="/bin/ls"
MKDIR="/bin/mkdir -p"
MV="/bin/mv"
PERL5="/usr/pkg/bin/perl"
PKG_ADMIN="/usr/pkg/sbin/pkg_admin"
PKG_INFO="/usr/pkg/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="xfce4-orage"

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

PKG_SYSCONFBASE="/usr/pkg/etc"
PKG_SYSCONFBASEDIR="/usr/pkg/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: dirs,v 1.4 2008/01/04 21:50:27 heinz 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'
#!/bin/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="/usr/bin/awk"
CAT="/bin/cat"
CHGRP="/usr/bin/chgrp"
CHMOD="/bin/chmod"
CHOWN="/usr/sbin/chown"
ECHO="echo"
GREP="/usr/bin/grep"
LS="/bin/ls"
MKDIR="/bin/mkdir -p"
MV="/bin/mv"
PWD_CMD="/bin/pwd"
RM="/bin/rm"
RMDIR="/bin/rmdir"
SED="/usr/bin/sed"
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_PREFIX=/usr/pkg}

PKG_REFCOUNT_DIRS_DBDIR="${PKG_REFCOUNT_DBDIR}/dirs"

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=no
	;;
esac
case "${PKG_CONFIG_PERMS:-NO}" 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; }
	${TEST} $? -eq 0 || exitcode=1
	${RM} -fr $tmpdir
	;;

*)
	${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

# $NetBSD: icon-themes,v 1.1 2017/06/14 16:23:09 prlw1 Exp $
#
# Generate an +ICON_THEMES script that handles the icon theme cache for
# the package.
#
case "${STAGE},$1" in
UNPACK,|UNPACK,+ICON_THEMES)
	${CAT} > ./+ICON_THEMES << 'EOF'
#!/bin/sh
#
# +ICON_THEMES - icon theme cache management script
#
# Usage: ./+ICON_THEMES ADD|REMOVE [metadatadir]
#
# This script supports two actions, UPDATE and REMOVE, that will update or
# remove cache files for icon themes from the package associated with
# <metadatadir>.
#
# Lines starting with "# ICON_THEME: " are data read by this script that
# name the icon theme and directory containing the "index.theme".
#
#	# ICON_THEME: hicolor
#	# ICON_THEME: gnome
#
# For each ICON_THEME entry, if the path is relative, that it is taken to be
# relative to ${PKG_PREFIX}/share/icons.
#

ECHO="echo"
GREP="/usr/bin/grep"
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"
GTK2_UPDATE_ICON_CACHE="/usr/pkg/bin/gtk2-update-icon-cache"
GTK3_UPDATE_ICON_CACHE="/usr/pkg/bin/gtk-update-icon-cache"
: ${GTK_UPDATE_ICON_CACHE=true}

SELF=$0
ACTION=$1

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

for _t in ${GTK3_UPDATE_ICON_CACHE} ${GTK2_UPDATE_ICON_CACHE}; do
	if ${TEST} -x $_t; then
		GTK_UPDATE_ICON_CACHE=$_t;
		break;
	fi
done

update_icon_cache()
{
	_dir="$1"
	if ${TEST} ! -f "$_dir/index.theme"; then
		:
	else
		${GTK_UPDATE_ICON_CACHE}  -f -q "$_dir"
	fi
}

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

		update_icon_cache $theme
	done
	;;

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

		${RM} -f "$theme/icon-theme.cache"
	done
	;;
esac
exit $exitcode

EOF
	${SED} -n "/^\# ICON_THEME: /p" ${SELF} >> ./+ICON_THEMES
	${CHMOD} +x ./+ICON_THEMES
	;;
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: install.tmpl,v 1.1.1.1 2004/09/21 17:03:54 jmmv Exp $

APPLICATIONS_PATH="/usr/pkg/share/applications"
UPDATE_DESKTOPDB="/usr/pkg/bin/update-desktop-database"

case ${STAGE} in
POST-INSTALL|DEINSTALL)
	${UPDATE_DESKTOPDB} ${APPLICATIONS_PATH}
	;;
esac
# $NetBSD: footer,v 1.1 2006/05/21 23:50:15 jlam Exp $

exit 0

# DIR: libexec/xfce4 m
# DIR: libexec/xfce4/panel-plugins m
# DIR: share/xfce4 m
# DIR: share/xfce4/panel-plugins m
# ICON_THEME: hicolor
