#!/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="sourcehansans-fonts"

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: fonts,v 1.7 2014/06/01 16:57:27 wiz Exp $
#
# Generate a +FONTS script that updates font databases for the package.
#
case "${STAGE},$1" in
UNPACK,|UNPACK,+FONTS)
	${CAT} > ./+FONTS << 'EOF'
#!/usr/bin/bash
#
# +FONTS - font database management script
#
# Usage: ./+FONTS [metadatadir]
#
# This scripts rebuilds font databases needed by the package associated
# with <metadatadir>.
#
# Lines starting with "# FONTS: " are data read by this script that name
# the directories in which the font database will be rebuilt.
#
#	# FONTS: /usr/pkg/share/fonts/X11/TTF ttf
#	# FONTS: /usr/pkg/share/fonts/X11/Type1 type1
#	# FONTS: /usr/pkg/share/fonts/X11/misc x11
#
# For each FONTS entry, if the path is relative, that it is taken to be
# relative to ${PKG_PREFIX}.
#

ECHO="echo"
FIND="/usr/bin/find"
GREP="/usr/xpg4/bin/grep"
MKFONTDIR="/opt/local/bin/mkfontdir"
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"
TTF_INDEX_CMD="/opt/local/bin/mkfontscale"
TYPE1_INDEX_CMD="@TYPE1_INDEX_CMD@"
TYPE1_POSTINDEX_CMD="@TYPE1_POSTINDEX_CMD@"
X11_ENCODINGSDIR="/opt/local/share/fonts/X11/encodings"

SELF=$0

CURDIR=`${PWD_CMD}`
PKG_METADATA_DIR="${1-${CURDIR}}"
: ${PKGNAME=${PKG_METADATA_DIR##*/}}
: ${PKG_PREFIX=/opt/local}

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

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

${SED} -n "/^\# FONTS: /{s/^\# FONTS: //;p;}" ${SELF} | ${SORT} -u |
while read dir font_type; do
	case ${_PKG_UPDATE_FONTS_DB} in
	no)	continue ;;
	esac
	case $dir in
	"")	continue ;;
	[!/]*)	dir="${PKG_PREFIX}/$dir" ;;
	esac
	${TEST} -d "$dir" || continue
	( $echo "${PKGNAME}: updating font database in $dir ($font_type)"
	  cd $dir
	  update_args=
	  post_update_cmd=
	  case $font_type in
	  [tT][tT][fF])		update_cmd="${TTF_INDEX_CMD}" ;;
	  [tT][yY][pP][eE]1)	update_cmd="${TYPE1_INDEX_CMD}"
				post_update_cmd="${TYPE1_POSTINDEX_CMD}" ;;
	  [xX]11)		update_cmd="${MKFONTDIR}"
				[ ! -z "${X11_ENCODINGSDIR}" ] && update_args="-e ${X11_ENCODINGSDIR}" ;;
	  esac
	  ${TEST} -f "$update_cmd" || update_cmd="${TRUE}"
	  $update_cmd $update_args >/dev/null
	  $post_update_cmd
	  #
	  # Remove fonts databases if there are no fonts in the directory.
	  # We filter out the encodings.dir, fonts.{dirs,scale}, and
	  # Fontmap database files from the directory listing.  Also remove
	  # the directory if it turns out to be empty.
	  #
	  if ${FIND} . -type f | ${GREP} -v "/encodings.dir" | ${GREP} -v "/fonts\.scale" | \
	     ${GREP} -v "/fonts\.dir" | ${GREP} -v "/Fontmap" >/dev/null
	  then
	      : # Directory not empty; do nothing.
	  else
	      ${RM} -f fonts.dir fonts.scale Fontmap* encodings.dir
	      ${RMDIR} -p $dir 2>/dev/null
	  fi
	)
done

EOF
	${SED} -n "/^\# FONTS: /p" ${SELF} >> ./+FONTS
	${CHMOD} +x ./+FONTS
	;;
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: 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

# FONTS: share/fonts/X11/TTF ttf
# FONTS: share/fonts/X11/TTF x11
