xref: /minix/distrib/sets/regpkg (revision 77e79d33)
19152e1c5SLionel Sambuc#! /bin/sh
29152e1c5SLionel Sambuc#
30a6a1f1dSLionel Sambuc# $NetBSD: regpkg,v 1.22 2014/05/30 08:37:35 uebayasi Exp $
49152e1c5SLionel Sambuc#
59152e1c5SLionel Sambuc# Copyright (c) 2003,2009 The NetBSD Foundation, Inc.
69152e1c5SLionel Sambuc# All rights reserved.
79152e1c5SLionel Sambuc#
89152e1c5SLionel Sambuc# This code is derived from software contributed to The NetBSD Foundation
99152e1c5SLionel Sambuc# by Alistair Crooks (agc@NetBSD.org)
109152e1c5SLionel Sambuc#
119152e1c5SLionel Sambuc# Redistribution and use in source and binary forms, with or without
129152e1c5SLionel Sambuc# modification, are permitted provided that the following conditions
139152e1c5SLionel Sambuc# are met:
149152e1c5SLionel Sambuc# 1. Redistributions of source code must retain the above copyright
159152e1c5SLionel Sambuc#    notice, this list of conditions and the following disclaimer.
169152e1c5SLionel Sambuc# 2. Redistributions in binary form must reproduce the above copyright
179152e1c5SLionel Sambuc#    notice, this list of conditions and the following disclaimer in the
189152e1c5SLionel Sambuc#    documentation and/or other materials provided with the distribution.
199152e1c5SLionel Sambuc#
209152e1c5SLionel Sambuc# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
219152e1c5SLionel Sambuc# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
229152e1c5SLionel Sambuc# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
239152e1c5SLionel Sambuc# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
249152e1c5SLionel Sambuc# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
259152e1c5SLionel Sambuc# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
269152e1c5SLionel Sambuc# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
279152e1c5SLionel Sambuc# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
289152e1c5SLionel Sambuc# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
299152e1c5SLionel Sambuc# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
309152e1c5SLionel Sambuc# POSSIBILITY OF SUCH DAMAGE.
319152e1c5SLionel Sambuc#
329152e1c5SLionel Sambuc
339152e1c5SLionel Sambuc# Usage: regpkg [options] set pkgname
349152e1c5SLionel Sambuc#
359152e1c5SLionel Sambuc# Registers a syspkg in the database directory,
369152e1c5SLionel Sambuc# and optionally creates a binary package.
379152e1c5SLionel Sambuc#
389152e1c5SLionel Sambuc# Options:
399152e1c5SLionel Sambuc#   -q		Quiet.
409152e1c5SLionel Sambuc#   -v		Verbose.
419152e1c5SLionel Sambuc#   -f		Force.
429152e1c5SLionel Sambuc#   -m		Ignore errors from missing files.
439152e1c5SLionel Sambuc#   -u		Update.
449152e1c5SLionel Sambuc#   -c		Use cached information from ${BUILD_INFO_CACHE}.
459152e1c5SLionel Sambuc#   -d destdir	Sets DESTDIR.
469152e1c5SLionel Sambuc#   -t binpkgdir Create a binary package (in *.tgz format) in the
479152e1c5SLionel Sambuc#		specified directory.  Without this option, a binary
489152e1c5SLionel Sambuc#		package is not created.
499152e1c5SLionel Sambuc#   -M metalog	Use the specified metalog file to override file
509152e1c5SLionel Sambuc#		or directory attributes when creating a binary package.
519152e1c5SLionel Sambuc#   -N etcdir	Use the specified directory for passwd and group files.
529152e1c5SLionel Sambuc#
539152e1c5SLionel Sambuc# When -f is set:  If the desired syspkg already exists, it is overwritten.
549152e1c5SLionel Sambuc# When -u is set:  If the desired syspkg already exists, it might be
559152e1c5SLionel Sambuc#		overwritten or left alone, depending on whether it's older
569152e1c5SLionel Sambuc#		or newer than the files that belong to the syspkg.
579152e1c5SLionel Sambuc# When neither -u nor -f are set:  It's an error for the desired syspkg
589152e1c5SLionel Sambuc#		to already exist.
599152e1c5SLionel Sambuc
609152e1c5SLionel Sambucprog="${0##*/}"
619152e1c5SLionel Sambuctoppid=$$
629152e1c5SLionel Sambucrundir="$(dirname "$0")" # ${0%/*} isn't good enough when there's no "/"
639152e1c5SLionel Sambuc. "${rundir}/sets.subr"
649152e1c5SLionel Sambuc
659152e1c5SLionel Sambucbomb()
669152e1c5SLionel Sambuc{
679152e1c5SLionel Sambuc	#echo "${prog}: bomb: start, toppid=${toppid} \$\$=$$"
689152e1c5SLionel Sambuc	kill ${toppid}		# in case we were invoked from a subshell
699152e1c5SLionel Sambuc	#echo "${prog}: bomb: killed ${toppid}"
709152e1c5SLionel Sambuc	exit 1
719152e1c5SLionel Sambuc}
729152e1c5SLionel Sambuc
739152e1c5SLionel Sambuc# A literal newline
749152e1c5SLionel Sambucnl='
759152e1c5SLionel Sambuc'
769152e1c5SLionel Sambuc# A literal tab
779152e1c5SLionel Sambuctab='	'
789152e1c5SLionel Sambuc
799152e1c5SLionel Sambuc# Prefixes for error messages, warnings, and important informational
809152e1c5SLionel Sambuc# messages.
819152e1c5SLionel SambucERROR="${prog}: ERROR: "
829152e1c5SLionel SambucWARNING="${prog}: WARNING: "
839152e1c5SLionel SambucNOTE="${prog}: NOTE: "
849152e1c5SLionel SambucERRWARN="${ERROR}"	# may be changed by "-f" (force) command line flag
859152e1c5SLionel SambucERRWARNNOTE="${ERROR}"	# may be changed by "-u" (update) command line flag
869152e1c5SLionel Sambuc
879152e1c5SLionel Sambuc#
889152e1c5SLionel Sambuc# All temporary files will go in ${SCRATCH}, which will be deleted on
899152e1c5SLionel Sambuc# exit.
909152e1c5SLionel Sambuc#
919152e1c5SLionel SambucSCRATCH="$(${MKTEMP} -d "/var/tmp/${0##*/}.XXXXXX")"
929152e1c5SLionel Sambucif [ $? -ne 0 -o \! -d "${SCRATCH}" ]; then
939152e1c5SLionel Sambuc	echo >&2 "${prog}: Could not create scratch directory."
949152e1c5SLionel Sambuc	bomb
959152e1c5SLionel Sambucfi
969152e1c5SLionel Sambuc
979152e1c5SLionel Sambuc#
989152e1c5SLionel Sambuc# cleanup() always deletes the SCRATCH directory, and might also
999152e1c5SLionel Sambuc# delete other files or directories.
1009152e1c5SLionel Sambuc#
1019152e1c5SLionel Sambuces=0
1029152e1c5SLionel Sambuccleanup_must_delete_binpkgfile=false
1039152e1c5SLionel Sambuccleanup_must_delete_dbsubdir=false
1049152e1c5SLionel Sambuccleanup()
1059152e1c5SLionel Sambuc{
1069152e1c5SLionel Sambuc	trap - 0
1079152e1c5SLionel Sambuc	#echo "${prog}: cleanup start"
1089152e1c5SLionel Sambuc	if ${cleanup_must_delete_binpkgfile:-false} && [ -e "${binpkgfile}" ]
1099152e1c5SLionel Sambuc	then
1109152e1c5SLionel Sambuc		echo >&2 "${prog}: deleting partially-created ${binpkgfile}"
1119152e1c5SLionel Sambuc		rm -f "${binpkgfile}"
1129152e1c5SLionel Sambuc	fi
1139152e1c5SLionel Sambuc	if ${cleanup_must_delete_dbsubdir:-false} \
1149152e1c5SLionel Sambuc	   && [ -e "${SYSPKG_DB_SUBDIR}" ]
1159152e1c5SLionel Sambuc	then
1169152e1c5SLionel Sambuc		echo >&2 "${prog}: deleting partially-created ${SYSPKG_DB_SUBDIR}"
1179152e1c5SLionel Sambuc		rm -rf "${SYSPKG_DB_SUBDIR}"
1189152e1c5SLionel Sambuc	fi
1199152e1c5SLionel Sambuc	rm -rf "${SCRATCH}"
1209152e1c5SLionel Sambuc	#echo "${prog}: cleanup done, exit ${es}"
1219152e1c5SLionel Sambuc	exit ${es}
1229152e1c5SLionel Sambuc}
1239152e1c5SLionel Sambuctrap 'es=128; cleanup' 1 2 3 13 15	# HUP INT QUIT PIPE TERM
1249152e1c5SLionel Sambuctrap 'es=$?; cleanup' 0 		# EXIT
1259152e1c5SLionel Sambuc
1269152e1c5SLionel Sambuc#
1279152e1c5SLionel Sambuc# Parse command line args.
1289152e1c5SLionel Sambuc#
1299152e1c5SLionel Sambucverbose=false
1309152e1c5SLionel Sambucverbosity=0
1319152e1c5SLionel Sambucquiet=false
1329152e1c5SLionel Sambucforce=false
1339152e1c5SLionel Sambucupdate=false
1349152e1c5SLionel Sambucallowmissing=false
1359152e1c5SLionel SambucDESTDIR="${DESTDIR}"
1369152e1c5SLionel Sambucbinpkgdir=""
1379152e1c5SLionel Sambucmetalog=""
1389152e1c5SLionel Sambucetcdir=""
1399152e1c5SLionel SambucSYSPKG_DB_TOPDIR=""
1409152e1c5SLionel Sambucpkgset=""
1419152e1c5SLionel Sambucpkg=""
1429152e1c5SLionel Sambucparse_args()
1439152e1c5SLionel Sambuc{
1449152e1c5SLionel Sambuc	while [ $# -gt 2 ]; do
1459152e1c5SLionel Sambuc		case "$1" in
1469152e1c5SLionel Sambuc		-q)	quiet=true; verbose=false ;;
1479152e1c5SLionel Sambuc		-v)	verbose=true; quiet=false
1489152e1c5SLionel Sambuc			verbosity=$(( ${verbosity} + 1 ))
1499152e1c5SLionel Sambuc			;;
1509152e1c5SLionel Sambuc		-f)	force=true ;;
1519152e1c5SLionel Sambuc		-u)	update=true ;;
1529152e1c5SLionel Sambuc		-m)	allowmissing=true ;;
1539152e1c5SLionel Sambuc		-c)	# The -c option is ignored.  The BUILD_INFO_CACHE
1549152e1c5SLionel Sambuc			# environment variable is used instead.
1559152e1c5SLionel Sambuc			;;
1569152e1c5SLionel Sambuc		-d)	DESTDIR="$2"; shift ;;
1579152e1c5SLionel Sambuc		-d*)	DESTDIR="${1#-?}" ;;
1589152e1c5SLionel Sambuc		-t)	binpkgdir="$2"; shift ;;
1599152e1c5SLionel Sambuc		-t*)	binpkgdir="${1#-?}" ;;
1609152e1c5SLionel Sambuc		-M)	metalog="$2"; shift ;;
1619152e1c5SLionel Sambuc		-M*)	metalog="${1#-?}" ;;
1629152e1c5SLionel Sambuc		-N)	etcdir="$2"; shift ;;
1639152e1c5SLionel Sambuc		-N*)	etcdir="${1#-?}" ;;
1649152e1c5SLionel Sambuc		*)	break ;;
1659152e1c5SLionel Sambuc		esac
1669152e1c5SLionel Sambuc		shift
1679152e1c5SLionel Sambuc	done
1689152e1c5SLionel Sambuc	if ${force}; then
1699152e1c5SLionel Sambuc		ERRWARN="${WARNING}"
1709152e1c5SLionel Sambuc	else
1719152e1c5SLionel Sambuc		ERRWARN="${ERROR}"
1729152e1c5SLionel Sambuc	fi
1739152e1c5SLionel Sambuc	if ${update}; then
1749152e1c5SLionel Sambuc		ERRWARNNOTE="${NOTE}"
1759152e1c5SLionel Sambuc	else
1769152e1c5SLionel Sambuc		ERRWARNNOTE="${ERRWARN}"
1779152e1c5SLionel Sambuc	fi
1789152e1c5SLionel Sambuc	DESTDIR="${DESTDIR%/}" # delete trailing "/" if any
1799152e1c5SLionel Sambuc	if [ \! -n "${etcdir}" ]; then
1809152e1c5SLionel Sambuc		etcdir="${DESTDIR}/etc"
1819152e1c5SLionel Sambuc	fi
1829152e1c5SLionel Sambuc	if [ -n "${binpkgdir}" -a \! -d "${binpkgdir}" ]; then
1839152e1c5SLionel Sambuc		echo >&2 "${ERROR}binary pkg directory ${binpkgdir} does not exist"
1849152e1c5SLionel Sambuc		bomb
1859152e1c5SLionel Sambuc	fi
1869152e1c5SLionel Sambuc	#
1879152e1c5SLionel Sambuc	# SYSPKG_DB_TOPDIR is the top level directory for registering
1889152e1c5SLionel Sambuc	# syspkgs.  It defaults to ${DESTDIR}/var/db/syspkg, but can be
1899152e1c5SLionel Sambuc	# overridden by environment variables SYSPKG_DBDIR or PKG_DBDIR.
1909152e1c5SLionel Sambuc	#
1919152e1c5SLionel Sambuc	# Note that this corresponds to the default value of PKG_DBDIR
1929152e1c5SLionel Sambuc	# set in .../distrib/syspkg/mk/bsd.syspkg.mk.
1939152e1c5SLionel Sambuc	#
1949152e1c5SLionel Sambuc	SYSPKG_DB_TOPDIR="${SYSPKG_DBDIR:-${PKG_DBDIR:-${DESTDIR}/var/db/syspkg}}"
1959152e1c5SLionel Sambuc
1969152e1c5SLionel Sambuc	if [ $# -ne 2 ]; then
1979152e1c5SLionel Sambuc		echo "Usage: regpkg [options] set pkgname"
1989152e1c5SLionel Sambuc		bomb
1999152e1c5SLionel Sambuc	fi
2009152e1c5SLionel Sambuc
2019152e1c5SLionel Sambuc	pkgset="$1"
2029152e1c5SLionel Sambuc	pkg="$2"
2039152e1c5SLionel Sambuc}
2049152e1c5SLionel Sambuc
2059152e1c5SLionel Sambuc#
2069152e1c5SLionel Sambuc# make_PLIST() creates a skeleton PLIST from the pkgset description.
2079152e1c5SLionel Sambuc#
2089152e1c5SLionel Sambuc# The result is stored in the file ${PLIST}.
2099152e1c5SLionel Sambuc#
2109152e1c5SLionel SambucPLIST="${SCRATCH}/PLIST"
2119152e1c5SLionel Sambucmake_PLIST()
2129152e1c5SLionel Sambuc{
2139152e1c5SLionel Sambuc	if ${verbose}; then
2149152e1c5SLionel Sambuc		echo "Making PLIST for \"${pkg}\" package (part of ${pkgset} set)"
2159152e1c5SLionel Sambuc	fi
2169152e1c5SLionel Sambuc	prefix="${DESTDIR:-/}"
2179152e1c5SLionel Sambuc	realprefix=/
2189152e1c5SLionel Sambuc	${HOST_SH} "${rundir}/makeplist" -p "${prefix}" -I "${realprefix}" \
2199152e1c5SLionel Sambuc		"${pkgset}" "${pkg}" \
2209152e1c5SLionel Sambuc		>"${PLIST}" 2>"${SCRATCH}/makeplist-errors"
2219152e1c5SLionel Sambuc	if ${EGREP} -v '^DEBUG:' "${SCRATCH}/makeplist-errors"; then
2229152e1c5SLionel Sambuc		# "find" invoked from makeplist sometimes reports
2239152e1c5SLionel Sambuc		# errors about missing files or directories, and
2249152e1c5SLionel Sambuc		# makeplist ignores the errors.  Catch them here.
2259152e1c5SLionel Sambuc		echo >&2 "${ERROR}makeplist reported errors for ${pkg}:"
2269152e1c5SLionel Sambuc		cat >&2 "${SCRATCH}/makeplist-errors"
2279152e1c5SLionel Sambuc		echo >&2 "${ERROR}see above for errors from makeplist"
2289152e1c5SLionel Sambuc		if ${allowmissing}; then
2299152e1c5SLionel Sambuc			echo >&2 "${prog}: ${NOTE}: ignoring above errors, due to '-m' option."
2309152e1c5SLionel Sambuc		else
2319152e1c5SLionel Sambuc			${force} || bomb
2329152e1c5SLionel Sambuc		fi
2339152e1c5SLionel Sambuc	fi
2349152e1c5SLionel Sambuc}
2359152e1c5SLionel Sambuc
2369152e1c5SLionel Sambuc#
2379152e1c5SLionel Sambuc# init_allfiles() converts the PLIST (which contains relative filenames)
2389152e1c5SLionel Sambuc# into a list of absolute filenames.  Directories are excluded from the
2399152e1c5SLionel Sambuc# result.
2409152e1c5SLionel Sambuc#
2419152e1c5SLionel Sambuc# The result is stored in the variable ${allfiles}.
2429152e1c5SLionel Sambuc#
2439152e1c5SLionel Sambucallfiles=''
2449152e1c5SLionel Sambucinit_allfiles()
2459152e1c5SLionel Sambuc{
2469152e1c5SLionel Sambuc	[ -f "${PLIST}" ] || make_PLIST
2479152e1c5SLionel Sambuc	allfiles="$(${AWK} '
2489152e1c5SLionel Sambuc		BEGIN { destdir = "'"${DESTDIR%/}"'" }
2499152e1c5SLionel Sambuc		/^@cwd/ { prefix = $2; next }
2509152e1c5SLionel Sambuc		/^@dirrm/ { next }
2519152e1c5SLionel Sambuc		{ printf("%s%s%s\n", destdir, prefix, $0) }' "${PLIST}")"
2529152e1c5SLionel Sambuc}
2539152e1c5SLionel Sambuc
2549152e1c5SLionel Sambuc#
2559152e1c5SLionel Sambuc# init_newestfile() finds the newest file (most recent mtime).
2569152e1c5SLionel Sambuc#
2579152e1c5SLionel Sambuc# The result is stored in the variable ${newestfile}.
2589152e1c5SLionel Sambuc#
2599152e1c5SLionel Sambucnewestfile=''
2609152e1c5SLionel Sambucinit_newestfile()
2619152e1c5SLionel Sambuc{
2629152e1c5SLionel Sambuc	[ -s "${allfiles}" ] || init_allfiles
2639152e1c5SLionel Sambuc	# We assume no shell special characters in ${allfiles},
2649152e1c5SLionel Sambuc	# and spaces only between file names, not inside file names.
2659152e1c5SLionel Sambuc	# This should be safe, because it has no no user-specified parts.
2669152e1c5SLionel Sambuc	newestfile="$(${LS} -1dt ${allfiles} | ${SED} '1q')"
2679152e1c5SLionel Sambuc}
2689152e1c5SLionel Sambuc
2699152e1c5SLionel Sambuc#
2709152e1c5SLionel Sambuc# Various ways of getting parts of the syspkg version number:
2719152e1c5SLionel Sambuc#
2729152e1c5SLionel Sambuc# get_osvers() - get the OS version number from osrelease.sh or $(uname -r),
2739152e1c5SLionel Sambuc#		return it in ${osvers}, and set ${method}.
2749152e1c5SLionel Sambuc# get_tinyvers() - get the tiny version number from the "versions" file,
2759152e1c5SLionel Sambuc#		and return it in ${tinyvers}.  Does not set ${method}.
2769152e1c5SLionel Sambuc# get_newest_rcsid_date() - get the newest RCS date,
2779152e1c5SLionel Sambuc#		and return it in ${newest}.  Does not set ${method}.
2789152e1c5SLionel Sambuc# get_newest_mtime_date() - get the newest file modification date,
2799152e1c5SLionel Sambuc#		and return it in ${newest}.  Does not set ${method}.
2809152e1c5SLionel Sambuc# get_newest_date() - get date from rcsid or mtime, return it in ${newest},
2819152e1c5SLionel Sambuc#		and set ${method}.
2829152e1c5SLionel Sambuc#
2839152e1c5SLionel Sambucget_osvers()
2849152e1c5SLionel Sambuc{
2859152e1c5SLionel Sambuc	if [ -f ../../sys/conf/osrelease.sh ]; then
2869152e1c5SLionel Sambuc		osvers="$(${HOST_SH} ../../sys/conf/osrelease.sh)"
2879152e1c5SLionel Sambuc		method=osreleases
2889152e1c5SLionel Sambuc	else
2899152e1c5SLionel Sambuc		osvers="$(${UNAME} -r)"
2909152e1c5SLionel Sambuc		method=uname
2919152e1c5SLionel Sambuc	fi
2929152e1c5SLionel Sambuc	#echo "${osvers}"
2939152e1c5SLionel Sambuc}
2949152e1c5SLionel Sambucget_tinyvers()
2959152e1c5SLionel Sambuc{
2969152e1c5SLionel Sambuc	tinyvers="$(${AWK} '$1 ~ '/"${pkg}"/' { print $2 }' \
2979152e1c5SLionel Sambuc			"${rundir}/versions")"
2989152e1c5SLionel Sambuc	case "${tinyvers}" in
2999152e1c5SLionel Sambuc	"")	tinyvers=0
3009152e1c5SLionel Sambuc		;;
3019152e1c5SLionel Sambuc	esac
3029152e1c5SLionel Sambuc	#echo "${tinyvers}"
3039152e1c5SLionel Sambuc}
3049152e1c5SLionel Sambucget_newest_rcsid_date()
3059152e1c5SLionel Sambuc{
3069152e1c5SLionel Sambuc	[ -s "${allfiles}" ] || init_allfiles
3079152e1c5SLionel Sambuc
3089152e1c5SLionel Sambuc	# Old RCS identifiers might have 2-digit years, so we match both
3099152e1c5SLionel Sambuc	# YY/MM/DD and YYYY/MM/DD.  We also try to deal with the Y10K
3109152e1c5SLionel Sambuc	# problem by allowing >4 digit years.
3119152e1c5SLionel Sambuc	newest=0
3129152e1c5SLionel Sambuc	case "${allfiles}" in
3139152e1c5SLionel Sambuc	"")	;;
3149152e1c5SLionel Sambuc	*)	newest="$(${IDENT} ${allfiles} 2>/dev/null | ${AWK} '
3159152e1c5SLionel Sambuc			BEGIN { last = 0 }
3169152e1c5SLionel Sambuc			$2 == "crt0.c,v" { next }
3179152e1c5SLionel Sambuc			NF == 8 && \
3189152e1c5SLionel Sambuc			$4 ~ /^[0-9][0-9]\/[0-9][0-9]\/[0-9][0-9]$/ \
3199152e1c5SLionel Sambuc				{ t = "19" $4; gsub("/", "", t);
3209152e1c5SLionel Sambuc				  if (t > last) last = t; }
3219152e1c5SLionel Sambuc			NF == 8 && \
3229152e1c5SLionel Sambuc			$4 ~ /^[0-9][0-9][0-9][0-9][0-9]*\/[0-9][0-9]\/[0-9][0-9]$/ \
3239152e1c5SLionel Sambuc				{ t = $4; gsub("/", "", t);
3249152e1c5SLionel Sambuc				  if (t > last) last = t; }
3259152e1c5SLionel Sambuc			END { print last }')"
3269152e1c5SLionel Sambuc		method=ident
3279152e1c5SLionel Sambuc		;;
3289152e1c5SLionel Sambuc	esac
3299152e1c5SLionel Sambuc	#echo "${newest}"
3309152e1c5SLionel Sambuc}
3319152e1c5SLionel Sambucget_newest_mtime_date()
3329152e1c5SLionel Sambuc{
3339152e1c5SLionel Sambuc	[ -s "${newestfile}" ] || init_newestfile
3349152e1c5SLionel Sambuc
3359152e1c5SLionel Sambuc	# We could simplify the awk program to take advantage of the
3369152e1c5SLionel Sambuc	# fact thet it should have exactly one line of input.
3379152e1c5SLionel Sambuc	newest="$(${ENV_CMD} TZ=UTC LOCALE=C ${LS} -lT "${newestfile}" \
3389152e1c5SLionel Sambuc		| ${AWK} '
3399152e1c5SLionel Sambuc		BEGIN { newest = 0 }
3409152e1c5SLionel Sambuc		{
3419152e1c5SLionel Sambuc			t = $9 "";
3429152e1c5SLionel Sambuc			if ($6 == "Jan") t = t "01";
3439152e1c5SLionel Sambuc			if ($6 == "Feb") t = t "02";
3449152e1c5SLionel Sambuc			if ($6 == "Mar") t = t "03";
3459152e1c5SLionel Sambuc			if ($6 == "Apr") t = t "04";
3469152e1c5SLionel Sambuc			if ($6 == "May") t = t "05";
3479152e1c5SLionel Sambuc			if ($6 == "Jun") t = t "06";
3489152e1c5SLionel Sambuc			if ($6 == "Jul") t = t "07";
3499152e1c5SLionel Sambuc			if ($6 == "Aug") t = t "08";
3509152e1c5SLionel Sambuc			if ($6 == "Sep") t = t "09";
3519152e1c5SLionel Sambuc			if ($6 == "Oct") t = t "10";
3529152e1c5SLionel Sambuc			if ($6 == "Nov") t = t "11";
3539152e1c5SLionel Sambuc			if ($6 == "Dec") t = t "12";
3549152e1c5SLionel Sambuc			if ($7 < 10) t = t "0";
3559152e1c5SLionel Sambuc			t = t $7;
3569152e1c5SLionel Sambuc			#these next two lines add the 24h clock onto the date
3579152e1c5SLionel Sambuc			#gsub(":", "", $8);
3589152e1c5SLionel Sambuc			#t = sprintf("%s.%4.4s", t, $8);
3599152e1c5SLionel Sambuc			if (t > newest) newest = t;
3609152e1c5SLionel Sambuc		}
3619152e1c5SLionel Sambuc		END { print newest }')"
3629152e1c5SLionel Sambuc	#echo "${newest}"
3639152e1c5SLionel Sambuc}
3649152e1c5SLionel Sambucget_newest_date()
3659152e1c5SLionel Sambuc{
3669152e1c5SLionel Sambuc	get_newest_rcsid_date
3679152e1c5SLionel Sambuc	case "${newest}" in
3689152e1c5SLionel Sambuc	""|0)	get_newest_mtime_date
3699152e1c5SLionel Sambuc		method=ls
3709152e1c5SLionel Sambuc		;;
3719152e1c5SLionel Sambuc	*)	method=rcsid
3729152e1c5SLionel Sambuc		;;
3739152e1c5SLionel Sambuc	esac
3749152e1c5SLionel Sambuc	#echo "${newest}"
3759152e1c5SLionel Sambuc}
3769152e1c5SLionel Sambuc
3779152e1c5SLionel Sambuc#
3789152e1c5SLionel Sambuc# choose_version_number() chooses the syspkg version number,
3799152e1c5SLionel Sambuc# by concatenating several components (OS version, syspkg "tiny"
3809152e1c5SLionel Sambuc# version and date).  We end up with something like
3819152e1c5SLionel Sambuc# osvers="3.99.15", tinyvers="0", newest="20060104",
3829152e1c5SLionel Sambuc# and t="3.99.15.0.20060104".
3839152e1c5SLionel Sambuc#
3849152e1c5SLionel Sambuc# The result is stored in the variables ${t} and ${method}.
3859152e1c5SLionel Sambuc#
3869152e1c5SLionel Sambucmethod=''
3879152e1c5SLionel Sambuct=''
3889152e1c5SLionel Sambucchoose_version_number()
3899152e1c5SLionel Sambuc{
3909152e1c5SLionel Sambuc	get_osvers; m1="${method}"
3919152e1c5SLionel Sambuc	get_tinyvers # does not set ${method}
3929152e1c5SLionel Sambuc	get_newest_date; m2="${method}"
3939152e1c5SLionel Sambuc	t="${osvers}.${tinyvers}.${newest}"
3949152e1c5SLionel Sambuc	method="${m1}.${m2}"
3959152e1c5SLionel Sambuc
3969152e1c5SLionel Sambuc	# print version number that we're using
3979152e1c5SLionel Sambuc	if ${verbose}; then
3989152e1c5SLionel Sambuc		echo "${pkg} - ${t} version using ${method} method"
3999152e1c5SLionel Sambuc	fi
4009152e1c5SLionel Sambuc}
4019152e1c5SLionel Sambuc
4029152e1c5SLionel Sambuc#
4039152e1c5SLionel Sambuc# init_db_opts() sets the dbfile, dbtype and db_opts variables,
4049152e1c5SLionel Sambuc# used for accessing the pkgdb.byfile.db database.
4059152e1c5SLionel Sambuc#
4069152e1c5SLionel Sambucinit_db_opts()
4079152e1c5SLionel Sambuc{
4089152e1c5SLionel Sambuc	dbfile="${SYSPKG_DB_TOPDIR}/pkgdb.byfile.db"
4099152e1c5SLionel Sambuc	dbtype="btree"
4109152e1c5SLionel Sambuc	db_opts=''
4119152e1c5SLionel Sambuc	: ${TARGET_ENDIANNESS:="$(arch_to_endian "${MACHINE_ARCH}")"}
4129152e1c5SLionel Sambuc	case "${TARGET_ENDIANNESS}" in
4139152e1c5SLionel Sambuc	4321)	db_opts="${db_opts} -E B" # big-endian
4149152e1c5SLionel Sambuc		;;
4159152e1c5SLionel Sambuc	1234)	db_opts="${db_opts} -E L" # little-endian
4169152e1c5SLionel Sambuc		;;
4179152e1c5SLionel Sambuc	*)
4189152e1c5SLionel Sambuc		echo >&2 "${WARNING}Unknown or unsupported target endianness"
4199152e1c5SLionel Sambuc		echo >&2 "${NOTE}Using host endianness"
4209152e1c5SLionel Sambuc		;;
4219152e1c5SLionel Sambuc	esac
4229152e1c5SLionel Sambuc	if ${update} || ${force}; then
4239152e1c5SLionel Sambuc		# overwriting an existing entry is not an error
4249152e1c5SLionel Sambuc		db_opts="${db_opts} -R"
4259152e1c5SLionel Sambuc	fi
4269152e1c5SLionel Sambuc	if [ ${verbosity} -lt 2 ]; then
4279152e1c5SLionel Sambuc		# don't print all the keys added to the database
4289152e1c5SLionel Sambuc		db_opts="${db_opts} -q"
4299152e1c5SLionel Sambuc	fi
4309152e1c5SLionel Sambuc}
4319152e1c5SLionel Sambuc
4329152e1c5SLionel Sambuc#
4339152e1c5SLionel Sambuc# print_dir_exec_lines outputs an "@exec install" line for each
4349152e1c5SLionel Sambuc# directory in ${PLIST}
4359152e1c5SLionel Sambuc#
4369152e1c5SLionel Sambucprint_dir_exec_lines()
4379152e1c5SLionel Sambuc{
4389152e1c5SLionel Sambuc	local dir uname gname mode
4399152e1c5SLionel Sambuc	local dot_slash_dir
4409152e1c5SLionel Sambuc	local no_dot_dir
4419152e1c5SLionel Sambuc	local word line
4429152e1c5SLionel Sambuc	${AWK} '/^@dirrm/ { print $2 }' <"${PLIST}" | \
4439152e1c5SLionel Sambuc	${SORT} | \
4449152e1c5SLionel Sambuc	while read dir; do
4459152e1c5SLionel Sambuc		# Sanitise the name. ${dir} could be an absolute or
4469152e1c5SLionel Sambuc		# relative name, with or without a leading "./".
4479152e1c5SLionel Sambuc		# ${dot_slash_dir} always has a leading "./" (except when
4489152e1c5SLionel Sambuc		# it's exactly equal to "."). ${no_dot_dir} never has a
4499152e1c5SLionel Sambuc		# leading "." or "/" (except when it's exactly equal to
4509152e1c5SLionel Sambuc		# ".").
4519152e1c5SLionel Sambuc		case "${dir}" in
4529152e1c5SLionel Sambuc		.|./|/)	dot_slash_dir=.  ;;
4539152e1c5SLionel Sambuc		./*)	dot_slash_dir="${dir}" ;;
4549152e1c5SLionel Sambuc		/*)	dot_slash_dir=".${dir}" ;;
4559152e1c5SLionel Sambuc		*)	dot_slash_dir="./${dir}" ;;
4569152e1c5SLionel Sambuc		esac
4579152e1c5SLionel Sambuc		no_dot_dir="${dot_slash_dir#./}"
4589152e1c5SLionel Sambuc		# Get the directory's owner, group, and mode
4599152e1c5SLionel Sambuc		# from the live file system, or let it be overridden
4609152e1c5SLionel Sambuc		# by the metalog.
4619152e1c5SLionel Sambuc		eval "$(${STAT} -f 'uname=%Su gname=%Sg mode=%#OLp' \
4629152e1c5SLionel Sambuc				"${DESTDIR}/${dot_slash_dir}")"
4639152e1c5SLionel Sambuc		if [ -n "${metalog}" ]; then
4649152e1c5SLionel Sambuc			line="$(echo "${dot_slash_dir}" | \
4659152e1c5SLionel Sambuc				${AWK} -f "${rundir}/join.awk" \
4669152e1c5SLionel Sambuc					/dev/stdin "${metalog}")"
4679152e1c5SLionel Sambuc			for word in ${line}; do
4689152e1c5SLionel Sambuc				case "${word}" in
4699152e1c5SLionel Sambuc				uname=*|gname=*|mode=*)	eval "${word}" ;;
4709152e1c5SLionel Sambuc				esac
4719152e1c5SLionel Sambuc			done
4729152e1c5SLionel Sambuc		fi
4739152e1c5SLionel Sambuc		# XXX: Work around yet another pkg_add bug: @cwd lines
4749152e1c5SLionel Sambuc		# do not actually cause the working directory to change,
4759152e1c5SLionel Sambuc		# so file names in @exec lines need to be qualified by
4769152e1c5SLionel Sambuc		# %D, which (in our case, since we know there's an
4779152e1c5SLionel Sambuc		# "@cwd /" line) will be the dir name passed to
4789152e1c5SLionel Sambuc		# "pkg_add -p PREFIX".
4799152e1c5SLionel Sambuc		case "${no_dot_dir}" in
4809152e1c5SLionel Sambuc		.) d="%D" ;;
4819152e1c5SLionel Sambuc		*) d="%D/${no_dot_dir}" ;;
4829152e1c5SLionel Sambuc		esac
4839152e1c5SLionel Sambuc		cat <<EOF
4849152e1c5SLionel Sambuc@exec install -d -o ${uname} -g ${gname} -m ${mode} ${d}
4859152e1c5SLionel SambucEOF
4869152e1c5SLionel Sambuc	done
4879152e1c5SLionel Sambuc}
4889152e1c5SLionel Sambuc
4899152e1c5SLionel Sambuc#
4909152e1c5SLionel Sambuc# register_syspkg() registers the syspkg in ${SYSPKG_DB_TOPDIR}.
4919152e1c5SLionel Sambuc# This involves creating the subdirectory ${SYSPKG_DB_SUBDIR}
4929152e1c5SLionel Sambuc# and populating it with several files.
4939152e1c5SLionel Sambuc#
4949152e1c5SLionel Sambucregister_syspkg()
4959152e1c5SLionel Sambuc{
4969152e1c5SLionel Sambuc	cleanup_must_delete_dbsubdir=true
4979152e1c5SLionel Sambuc	[ -n "${SYSPKG_DB_SUBDIR}" ] || bomb
4989152e1c5SLionel Sambuc	mkdir -p "${SYSPKG_DB_SUBDIR}"
4999152e1c5SLionel Sambuc
5009152e1c5SLionel Sambuc	#
5019152e1c5SLionel Sambuc	# Guess what versions of other packages to depend on.
5029152e1c5SLionel Sambuc	#
5039152e1c5SLionel Sambuc	# If we are using the OS version as part of the pkg
5049152e1c5SLionel Sambuc	# version, then depend on any version ">=${osvers}".  For
5059152e1c5SLionel Sambuc	# example, etc-sys-etc-1.6ZI.0.20040206 might depend on
5069152e1c5SLionel Sambuc	# base-sys-root>=1.6ZI.
5079152e1c5SLionel Sambuc	#
5089152e1c5SLionel Sambuc	# Failing that, depend on any version "-[0-9]*".
5099152e1c5SLionel Sambuc	#
5109152e1c5SLionel Sambuc	# XXX: We could extend the format of the "deps" file to carry
5119152e1c5SLionel Sambuc	# this sort of information, so we wouldn't have to guess.
5129152e1c5SLionel Sambuc	#
5139152e1c5SLionel Sambuc	case "${t}" in
5149152e1c5SLionel Sambuc	${osvers}.*)	depversion=">=${osvers}" ;;
5159152e1c5SLionel Sambuc	*)		depversion="-[0-9]*" ;;
5169152e1c5SLionel Sambuc	esac
5179152e1c5SLionel Sambuc
5189152e1c5SLionel Sambuc	#
5199152e1c5SLionel Sambuc	# Add the dependencies.
5209152e1c5SLionel Sambuc	#
5219152e1c5SLionel Sambuc	# We always add a "@pkgdep" line for each prerequisite package.
5229152e1c5SLionel Sambuc	#
5239152e1c5SLionel Sambuc	# If the prerequisite pkg is already registered (as it should be
5249152e1c5SLionel Sambuc	# if our caller is doing things in the right order), then we put
5259152e1c5SLionel Sambuc	# its exact version number in a "@blddep" line.
5269152e1c5SLionel Sambuc	#
5279152e1c5SLionel Sambuc	${AWK} '$1 ~ '/"${pkg}"/' { print $2 }' "${rundir}/deps" | ${SORT} | \
5289152e1c5SLionel Sambuc	while read depname; do
5299152e1c5SLionel Sambuc		# ${pkgdepglob} is a shell glob pattern that should match
5309152e1c5SLionel Sambuc		# any version of a pkg.  ${pkgdep} uses the special syntax
5319152e1c5SLionel Sambuc		# for pkg dependencies, and is not usable as a shell
5329152e1c5SLionel Sambuc		# glob pattern.
5339152e1c5SLionel Sambuc		pkgdepglob="${depname}-[0-9]*"
5349152e1c5SLionel Sambuc		pkgdep="${depname}${depversion}"
5359152e1c5SLionel Sambuc		echo "@pkgdep ${pkgdep}"
5369152e1c5SLionel Sambuc		blddep="$(cd "${SYSPKG_DB_TOPDIR}" && echo ${pkgdepglob} \
5379152e1c5SLionel Sambuc			|| bomb)"
5389152e1c5SLionel Sambuc		case "${blddep}" in
5399152e1c5SLionel Sambuc		*\*)	# pkgdepglob did not match anything
5409152e1c5SLionel Sambuc			echo >&2 "${WARNING}${pkg} depends on '${pkgdep}' but there is no matching syspkg in ${SYSPKG_DB_TOPDIR}"
5419152e1c5SLionel Sambuc			;;
5429152e1c5SLionel Sambuc		*\ *)	# pkgdepglob matched more than once.
5439152e1c5SLionel Sambuc			echo >&2 "${ERRWARN}${pkg} depends on '${pkgdep}' but there are multiple matching syspkgs in ${SYSPKG_DB_TOPDIR}"
5449152e1c5SLionel Sambuc			${force} || bomb
5459152e1c5SLionel Sambuc			# If ${force} is set, then assume that the last
5469152e1c5SLionel Sambuc			# match is the most recent.
5479152e1c5SLionel Sambuc			# XXX: This might be wrong, because of
5489152e1c5SLionel Sambuc			# differences between lexical sorting and
5499152e1c5SLionel Sambuc			# numeric sorting.
5509152e1c5SLionel Sambuc			lastmatch="${blddep##* }"
5519152e1c5SLionel Sambuc			echo "@blddep ${lastmatch}"
5529152e1c5SLionel Sambuc			;;
5539152e1c5SLionel Sambuc		*)	# exactly one match.
5549152e1c5SLionel Sambuc			# XXX: We ignore the possibility that the
5559152e1c5SLionel Sambuc			# version we found via ${pkgdepglob} might not
5569152e1c5SLionel Sambuc			# satisfy ${pkgdep}.  We could conceivably use
5579152e1c5SLionel Sambuc			# "pkg_admin pmatch" to check, but that's not a
5589152e1c5SLionel Sambuc			# host tool so we can't assume that it will be
5599152e1c5SLionel Sambuc			# available.
5609152e1c5SLionel Sambuc			echo "@blddep ${blddep}"
5619152e1c5SLionel Sambuc			;;
5629152e1c5SLionel Sambuc		esac
5639152e1c5SLionel Sambuc	done >>"${PLIST}"
5649152e1c5SLionel Sambuc
5659152e1c5SLionel Sambuc	# create the comment (should be one line)
5669152e1c5SLionel Sambuc	comment="$(${AWK} '$1 ~ '/"${pkg}"/' \
5679152e1c5SLionel Sambuc			{ print substr($0, length($1) + 2) }' \
5689152e1c5SLionel Sambuc			"${rundir}/comments")"
5699152e1c5SLionel Sambuc	case "${comment}" in
5709152e1c5SLionel Sambuc	"")	echo >&2 "${WARNING}no comment for \"${pkg}\" (using placeholder)"
5719152e1c5SLionel Sambuc		comment="System package for ${pkg}"
5729152e1c5SLionel Sambuc		;;
5739152e1c5SLionel Sambuc	*"${nl}"*)
5749152e1c5SLionel Sambuc		echo >&2 "${ERRWARN}multi-line comment for \"${pkg}\""
5759152e1c5SLionel Sambuc		${force} || bomb
5769152e1c5SLionel Sambuc		;;
5779152e1c5SLionel Sambuc	esac
5789152e1c5SLionel Sambuc	echo "${comment}" > "${SYSPKG_DB_SUBDIR}/+COMMENT"
5799152e1c5SLionel Sambuc
5809152e1c5SLionel Sambuc	# create the description (could be multiple lines)
5819152e1c5SLionel Sambuc	descr="$(${AWK} '$1 ~ '/"${pkg}"/' {
5829152e1c5SLionel Sambuc			print substr($0, length($1) + 2) }' \
5839152e1c5SLionel Sambuc			"${rundir}/descrs")"
5849152e1c5SLionel Sambuc	case "${descr}" in
5859152e1c5SLionel Sambuc	"")	echo >&2 "${WARNING}no description for \"${pkg}\" (re-using comment)" 2>&1
5869152e1c5SLionel Sambuc		descr="${comment}"
5879152e1c5SLionel Sambuc		;;
5889152e1c5SLionel Sambuc	esac
5899152e1c5SLionel Sambuc	echo "${descr}" > "${SYSPKG_DB_SUBDIR}/+DESC"
5909152e1c5SLionel Sambuc	${PRINTF} "\nHomepage:\nhttp://www.NetBSD.org/\n" >> "${SYSPKG_DB_SUBDIR}/+DESC"
5919152e1c5SLionel Sambuc
5929152e1c5SLionel Sambuc	# create the build information
5939152e1c5SLionel Sambuc	if [ x"${BUILD_INFO_CACHE}" = x ]; then
5949152e1c5SLionel Sambuc		{
5959152e1c5SLionel Sambuc		# These variables describe the build
5969152e1c5SLionel Sambuc		# environment, not the target.
5979152e1c5SLionel Sambuc		echo "OPSYS=$(${UNAME} -s)"
5989152e1c5SLionel Sambuc		echo "OS_VERSION=$(${UNAME} -r)"
5999152e1c5SLionel Sambuc		${MAKE} -B -f- all <<EOF
6009152e1c5SLionel Sambuc.include <bsd.own.mk>
6019152e1c5SLionel Sambucall:
6029152e1c5SLionel Sambuc	@echo OBJECT_FMT=${OBJECT_FMT}
6039152e1c5SLionel Sambuc	@echo MACHINE_ARCH=${MACHINE_ARCH}
6049152e1c5SLionel Sambuc	@echo MACHINE_GNU_ARCH=${MACHINE_GNU_ARCH}
6059152e1c5SLionel SambucEOF
6069152e1c5SLionel Sambuc		} > "${SYSPKG_DB_SUBDIR}/+BUILD_INFO"
6079152e1c5SLionel Sambuc	else
6089152e1c5SLionel Sambuc		cp "${BUILD_INFO_CACHE}" "${SYSPKG_DB_SUBDIR}/+BUILD_INFO"
6099152e1c5SLionel Sambuc	fi
6109152e1c5SLionel Sambuc
6119152e1c5SLionel Sambuc	# test for attributes
6129152e1c5SLionel Sambuc	args=""
6139152e1c5SLionel Sambuc	attrs="$(${AWK} '$1 ~ '/"${pkg}"/' { \
6149152e1c5SLionel Sambuc			print substr($0, length($1) + 2) }' \
6159152e1c5SLionel Sambuc		"${rundir}/attrs")"
6169152e1c5SLionel Sambuc	for a in "${attrs}"; do
6179152e1c5SLionel Sambuc		case "${attrs}" in
6189152e1c5SLionel Sambuc		"")	;;
6199152e1c5SLionel Sambuc		preserve)
6209152e1c5SLionel Sambuc			echo "${pkg}-${t}" >"${SYSPKG_DB_SUBDIR}/+PRESERVE"
6219152e1c5SLionel Sambuc			args="${args} -n ${SYSPKG_DB_SUBDIR}/+PRESERVE"
6229152e1c5SLionel Sambuc			;;
6239152e1c5SLionel Sambuc		esac
6249152e1c5SLionel Sambuc	done
6259152e1c5SLionel Sambuc
6269152e1c5SLionel Sambuc	#
6279152e1c5SLionel Sambuc	# Create ${SYSPKGSIR}/+CONTENTS from ${PLIST}, by adding an
6289152e1c5SLionel Sambuc	# "@name" line and a lot of "@comment MD5:" lines.
6299152e1c5SLionel Sambuc	#
6309152e1c5SLionel Sambuc	{
6310a6a1f1dSLionel Sambuc		rcsid='$NetBSD: regpkg,v 1.22 2014/05/30 08:37:35 uebayasi Exp $'
6329152e1c5SLionel Sambuc		utcdate="$(${ENV_CMD} TZ=UTC LOCALE=C \
6339152e1c5SLionel Sambuc			${DATE} '+%Y-%m-%d %H:%M')"
6349152e1c5SLionel Sambuc		user="${USER:-root}"
6359152e1c5SLionel Sambuc		host="$(${HOSTNAME_CMD})"
6369152e1c5SLionel Sambuc		echo "@name ${pkg}-${t}"
6379152e1c5SLionel Sambuc		echo "@comment Packaged at ${utcdate} UTC by ${user}@${host}"
6389152e1c5SLionel Sambuc		echo "@comment Packaged using ${prog} ${rcsid}"
6399152e1c5SLionel Sambuc		# XXX: "option extract-in-place" might help to get
6409152e1c5SLionel Sambuc		#	pkg_add to create directories.
6419152e1c5SLionel Sambuc		# XXX: no, it doesn't work.  Yet another pkg_add bug.
6429152e1c5SLionel Sambuc		## echo "@option extract-in-place"
6439152e1c5SLionel Sambuc		# Move the @pkgdep and @blddep lines up, so that
6449152e1c5SLionel Sambuc		# they are easy to see when people do "less
6459152e1c5SLionel Sambuc		# ${DESTDIR}/var/db/syspkg/*/+CONTENTS".
6469152e1c5SLionel Sambuc		${EGREP} '^(@pkgdep|@blddep)' "${PLIST}" || true
6479152e1c5SLionel Sambuc		# Now do the remainder of the file.
6489152e1c5SLionel Sambuc		while read line; do
6499152e1c5SLionel Sambuc			case "${line}" in
6509152e1c5SLionel Sambuc			@pkgdep*|@blddep*)
6519152e1c5SLionel Sambuc				# already handled by grep above
6529152e1c5SLionel Sambuc				;;
6539152e1c5SLionel Sambuc			@cwd*)
6549152e1c5SLionel Sambuc				# There should be exactly one @cwd line.
6559152e1c5SLionel Sambuc				# Just after it, add an "@exec mkdir"
6569152e1c5SLionel Sambuc				# line for every directory.  This is to
6579152e1c5SLionel Sambuc				# work around a pkg-add bug (see
6589152e1c5SLionel Sambuc				# <http://mail-index.NetBSD.org/tech-pkg/2003/12/11/0018.html>)
6599152e1c5SLionel Sambuc				echo "${line}"
6609152e1c5SLionel Sambuc				print_dir_exec_lines
6619152e1c5SLionel Sambuc				;;
6629152e1c5SLionel Sambuc			@*)
6639152e1c5SLionel Sambuc				# just pass through all other @foo lines
6649152e1c5SLionel Sambuc				echo "${line}"
6659152e1c5SLionel Sambuc				;;
6669152e1c5SLionel Sambuc			*)
6679152e1c5SLionel Sambuc				# This should be a file name.  Pass it
6689152e1c5SLionel Sambuc				# through, and append "@comment MD5:".
6699152e1c5SLionel Sambuc				# XXX why not SHA256 ?
6709152e1c5SLionel Sambuc				echo "${line}"
6719152e1c5SLionel Sambuc				file="${DESTDIR}${line}"
6729152e1c5SLionel Sambuc				if [ -f "${file}" -a -r "${file}" ];
6739152e1c5SLionel Sambuc				then
6749152e1c5SLionel Sambuc					md5sum="$(${CKSUM} -n -m "${file}" \
6759152e1c5SLionel Sambuc						   | ${AWK} '{print $1}'
6769152e1c5SLionel Sambuc						)"
6779152e1c5SLionel Sambuc					echo "@comment MD5:${md5sum}"
6789152e1c5SLionel Sambuc				fi
6799152e1c5SLionel Sambuc				;;
6809152e1c5SLionel Sambuc			esac
6819152e1c5SLionel Sambuc		done <"${PLIST}"
6829152e1c5SLionel Sambuc	} >"${SYSPKG_DB_SUBDIR}/+CONTENTS"
6839152e1c5SLionel Sambuc
6849152e1c5SLionel Sambuc	#
6859152e1c5SLionel Sambuc	#  Update ${SYSPKG_DB_TOPDIR}/pkgdb.byfile.db.
6869152e1c5SLionel Sambuc	#
6879152e1c5SLionel Sambuc	{
6889152e1c5SLionel Sambuc		init_db_opts # sets dbfile, dbtype, and db_opts
6899152e1c5SLionel Sambuc
6909152e1c5SLionel Sambuc		# Transform ${PLIST} into a form to be used as keys in
6919152e1c5SLionel Sambuc		# ${dbfile}.  The results look like absolute paths,
6929152e1c5SLionel Sambuc		# but they are really relative to ${DESTDIR}.
6939152e1c5SLionel Sambuc		#
6949152e1c5SLionel Sambuc		# "@dirrm ."		-> "/"
6959152e1c5SLionel Sambuc		# "@dirrm foo/bar"	-> "/foo/bar"
6969152e1c5SLionel Sambuc		# "@dirrm ./foo/bar"	-> "/foo/bar"
6979152e1c5SLionel Sambuc		# "foo/bar/baz"		-> "/foo/bar/baz"
6989152e1c5SLionel Sambuc		# "./foo/bar/baz"	-> "/foo/bar/baz"
6999152e1c5SLionel Sambuc		#
7009152e1c5SLionel Sambuc		dblist="${SCRATCH}/dblist"
7019152e1c5SLionel Sambuc		${AWK} '/^@dirrm \.\//	{gsub("^.", "", $2); print $2; next}
7029152e1c5SLionel Sambuc			/^@dirrm \.$/	{print "/"; next}
7039152e1c5SLionel Sambuc			/^@dirrm/	{print "/" $2; next}
7049152e1c5SLionel Sambuc			/^@/		{next}
7059152e1c5SLionel Sambuc			/^\.\//		{gsub("^.", "", $0); print $0; next}
7069152e1c5SLionel Sambuc			/./		{print "/" $0; next}' \
7079152e1c5SLionel Sambuc			<"${PLIST}" >"${dblist}"
7089152e1c5SLionel Sambuc		# Add all the path names to the database.
7099152e1c5SLionel Sambuc		${AWK} '{print $1 "\t" "'"${pkg}-${t}"'"}' <"${dblist}" \
7109152e1c5SLionel Sambuc		| ${DB} -w ${db_opts} -F "${tab}" -f - "${dbtype}" "${dbfile}"
7119152e1c5SLionel Sambuc	}
7129152e1c5SLionel Sambuc
7139152e1c5SLionel Sambuc	if ${verbose}; then
7149152e1c5SLionel Sambuc		echo "Registered ${pkg}-${t} in ${SYSPKG_DB_TOPDIR}"
7159152e1c5SLionel Sambuc	elif ! ${quiet}; then
7169152e1c5SLionel Sambuc		echo "Registered ${pkg}-${t}"
7179152e1c5SLionel Sambuc	fi
7189152e1c5SLionel Sambuc
7199152e1c5SLionel Sambuc	cleanup_must_delete_dbsubdir=false
7209152e1c5SLionel Sambuc}
7219152e1c5SLionel Sambuc
7229152e1c5SLionel Sambuc#
7239152e1c5SLionel Sambuc# create_syspkg_tgz() creates the *.tgz file for the package.
7249152e1c5SLionel Sambuc#
7259152e1c5SLionel Sambuc# The output file is ${binpkgdir}/${pkg}-${t}.tgz.
7269152e1c5SLionel Sambuc#
7279152e1c5SLionel Sambuccreate_syspkg_tgz()
7289152e1c5SLionel Sambuc{
7299152e1c5SLionel Sambuc	#
7309152e1c5SLionel Sambuc	# pkg_create does not understand metalog files, so we have to
7319152e1c5SLionel Sambuc	# use pax directly.
7329152e1c5SLionel Sambuc	#
7339152e1c5SLionel Sambuc	# We create two specfiles: specfile_overhead describes the
7349152e1c5SLionel Sambuc	# special files that are part of the package system's metadata
7359152e1c5SLionel Sambuc	# (+CONTENTS, +COMMENT, +DESCR, and more); and specfile_payload
7369152e1c5SLionel Sambuc	# describes the files and directories that we actually want as
7379152e1c5SLionel Sambuc	# part of the package's payload.
7389152e1c5SLionel Sambuc	#
7399152e1c5SLionel Sambuc	# We then use the specfiles to create a compressed tarball that
7409152e1c5SLionel Sambuc	# contains both the overhead files and the payload files.
7419152e1c5SLionel Sambuc	#
7429152e1c5SLionel Sambuc	# There's no trivial way to get a single pax run to do
7439152e1c5SLionel Sambuc	# everything we want, so we run pax twice, with a different
7449152e1c5SLionel Sambuc	# working directory and a different specfile each time.
7459152e1c5SLionel Sambuc	#
7469152e1c5SLionel Sambuc	# We could conceivably make clever use of pax's "-s" option to
7479152e1c5SLionel Sambuc	# get what we want from a single pax run with a single (more
7489152e1c5SLionel Sambuc	# complicated) specfile, but the extra trouble doesn't seem
7499152e1c5SLionel Sambuc	# warranted.
7509152e1c5SLionel Sambuc	#
7519152e1c5SLionel Sambuc	cleanup_must_delete_binpkgfile=true
7529152e1c5SLionel Sambuc	specfile_overhead="${SCRATCH}/spec_overhead"
7539152e1c5SLionel Sambuc	specfile_payload="${SCRATCH}/spec_payload"
7549152e1c5SLionel Sambuc	tarball_uncompressed="${SCRATCH}/tarball_uncompressed"
7559152e1c5SLionel Sambuc
7569152e1c5SLionel Sambuc	# Create a specfile for all the overhead files (+CONTENTS and
7579152e1c5SLionel Sambuc	# friends).
7589152e1c5SLionel Sambuc	{
7599152e1c5SLionel Sambuc		plusnames_first="${SCRATCH}/plusnames_first"
7609152e1c5SLionel Sambuc		plusnames_rest="${SCRATCH}/plusnames_rest"
7619152e1c5SLionel Sambuc
7629152e1c5SLionel Sambuc		# Ensure that the first few files are in the same order
7639152e1c5SLionel Sambuc		# that "pkg_create" would have used, just in case anything
7649152e1c5SLionel Sambuc		# depends on that.  Other files in alphabetical order.
7659152e1c5SLionel Sambuc		SHOULD_BE_FIRST="+CONTENTS +COMMENT +DESC"
7669152e1c5SLionel Sambuc		(
7679152e1c5SLionel Sambuc			cd "${SYSPKG_DB_SUBDIR}" || bomb
7689152e1c5SLionel Sambuc			for file in ${SHOULD_BE_FIRST}; do
7699152e1c5SLionel Sambuc				[ -e "./${file}" ] && echo "${file}"
7709152e1c5SLionel Sambuc			done >"${plusnames_first}"
7719152e1c5SLionel Sambuc			${LS} -1 | ${FGREP} -v -f "${plusnames_first}" \
7729152e1c5SLionel Sambuc				>"${plusnames_rest}" \
7739152e1c5SLionel Sambuc				|| true
7749152e1c5SLionel Sambuc		)
7759152e1c5SLionel Sambuc
7769152e1c5SLionel Sambuc		# Convert the file list to specfile format, and override the
7779152e1c5SLionel Sambuc		# uid/gid/mode.
7789152e1c5SLionel Sambuc		{
7799152e1c5SLionel Sambuc			echo ". optional type=dir"
7809152e1c5SLionel Sambuc			${AWK} '{print "./" $0 " type=file uid=0 gid=0 mode=0444"
7819152e1c5SLionel Sambuc				}' "${plusnames_first}" "${plusnames_rest}"
7829152e1c5SLionel Sambuc		} >"${specfile_overhead}"
7839152e1c5SLionel Sambuc	}
7849152e1c5SLionel Sambuc
7859152e1c5SLionel Sambuc	# Create a specfile for the payload of the package.
7869152e1c5SLionel Sambuc	{
7879152e1c5SLionel Sambuc		spec1="${SCRATCH}/spec1"
7889152e1c5SLionel Sambuc		spec2="${SCRATCH}/spec2"
7899152e1c5SLionel Sambuc
7909152e1c5SLionel Sambuc		# Transform ${PLIST} into simple specfile format:
7919152e1c5SLionel Sambuc		#
7929152e1c5SLionel Sambuc		# "@dirrm ."		-> ". type=dir"
7939152e1c5SLionel Sambuc		# "@dirrm foo/bar"	-> "./foo/bar type=dir"
7949152e1c5SLionel Sambuc		# "@dirrm ./foo/bar"	-> "./foo/bar type=dir"
7959152e1c5SLionel Sambuc		# "foo/bar/baz"		-> "./foo/bar/baz"
7969152e1c5SLionel Sambuc		# "./foo/bar/baz"	-> "./foo/bar/baz"
7979152e1c5SLionel Sambuc		#
7989152e1c5SLionel Sambuc		# Ignores @cwd lines.  This should be safe, given how
7999152e1c5SLionel Sambuc		# makeplist works.
8009152e1c5SLionel Sambuc		${AWK} '/^@dirrm \.\//	{print $2 " type=dir"; next}
8019152e1c5SLionel Sambuc			/^@dirrm \.$/	{print ". type=dir"; next}
8029152e1c5SLionel Sambuc			/^@dirrm/	{print "./" $2 " type=dir"; next}
8039152e1c5SLionel Sambuc			/^@/		{next}
8049152e1c5SLionel Sambuc			/^\.\//		{print $0; next}
8059152e1c5SLionel Sambuc			/./		{print "./" $0; next}' \
8060a6a1f1dSLionel Sambuc			<"${PLIST}" |
8070a6a1f1dSLionel Sambuc		# Escape some characters to match the new mtree(8) format.
8080a6a1f1dSLionel Sambuc		# C.f. usr.sbin/mtree/spec.c:vispath()
8090a6a1f1dSLionel Sambuc		# XXX escape only '[' for now
8100a6a1f1dSLionel Sambuc		${SED} -e 's,\[,\\133,g' \
8110a6a1f1dSLionel Sambuc		>"${spec1}"
8129152e1c5SLionel Sambuc
8139152e1c5SLionel Sambuc		# If metalog was specified, attributes from metalog override
8149152e1c5SLionel Sambuc		# attributes in the file system.  We also fake up an
8159152e1c5SLionel Sambuc		# entry for the ./etc/mtree/set.${pkgset} file.
8169152e1c5SLionel Sambuc		{
8179152e1c5SLionel Sambuc			if [ -n "${metalog}" ]; then
8189152e1c5SLionel Sambuc				${AWK} -f "${rundir}/join.awk" \
8199152e1c5SLionel Sambuc					"${spec1}" "${metalog}"
8209152e1c5SLionel Sambuc				${AWK} -f "${rundir}/join.awk" \
8219152e1c5SLionel Sambuc					"${spec1}" /dev/stdin <<EOF
822*77e79d33SDavid van Moolenbroek./etc/mtree/set.${pkgset} type=file mode=0444 uname=root gname=wheel
8239152e1c5SLionel SambucEOF
8249152e1c5SLionel Sambuc			else
8259152e1c5SLionel Sambuc				cat "${spec1}"
8269152e1c5SLionel Sambuc			fi
8279152e1c5SLionel Sambuc		} >"${spec2}"
8289152e1c5SLionel Sambuc
8299152e1c5SLionel Sambuc		#
8309152e1c5SLionel Sambuc		# If a file or directory to was mentioned explicitly
8319152e1c5SLionel Sambuc		# in ${PLIST} but not mentioned in ${metalog}, then the
8329152e1c5SLionel Sambuc		# file or directory will not be mentioned in ${spec2}.
8339152e1c5SLionel Sambuc		# This is an error, and means that the metalog was
8349152e1c5SLionel Sambuc		# not built correctly.
8359152e1c5SLionel Sambuc		#
8369152e1c5SLionel Sambuc		if [ -n "${metalog}" ]; then
8379152e1c5SLionel Sambuc			names1="${SCRATCH}/names1"
8389152e1c5SLionel Sambuc			names2="${SCRATCH}/names2"
8399152e1c5SLionel Sambuc			${AWK} '{print $1}' <"${spec1}" | ${SORT} >"${names1}"
8409152e1c5SLionel Sambuc			${AWK} '{print $1}' <"${spec2}" | ${SORT} >"${names2}"
8419152e1c5SLionel Sambuc			if ${FGREP} -v -f "${names2}" "${spec1}" >/dev/null
8429152e1c5SLionel Sambuc			then
8439152e1c5SLionel Sambuc				cat >&2 <<EOM
8449152e1c5SLionel Sambuc${ERRWARN}The metalog file (${metalog}) does not
8459152e1c5SLionel Sambuc	contain entries for the following files or directories
8469152e1c5SLionel Sambuc	which should be part of the ${pkg} syspkg:
8479152e1c5SLionel SambucEOM
8489152e1c5SLionel Sambuc				${FGREP} -v -f "${names2}" "${spec1}" >&2
8499152e1c5SLionel Sambuc				${force} || bomb
8509152e1c5SLionel Sambuc			fi
8519152e1c5SLionel Sambuc			if ${FGREP} -v -f "${names1}" "${spec2}" >/dev/null
8529152e1c5SLionel Sambuc			then
8539152e1c5SLionel Sambuc				cat >&2 <<EOM
8549152e1c5SLionel Sambuc${ERRWARN}The following lines are in the metalog file
8559152e1c5SLionel Sambuc	(${metalog}), and the corresponding files or directories
8569152e1c5SLionel Sambuc	should be in the ${pkg} syspkg, but something is wrong:
8579152e1c5SLionel SambucEOM
8589152e1c5SLionel Sambuc				${FGREP} -v -f "${names1}" "${spec2}" >&2
8599152e1c5SLionel Sambuc				bomb
8609152e1c5SLionel Sambuc			fi
8619152e1c5SLionel Sambuc		fi
8629152e1c5SLionel Sambuc
8639152e1c5SLionel Sambuc		# Add lines (tagged "optional") for any implicit directories.
8649152e1c5SLionel Sambuc		#
8659152e1c5SLionel Sambuc		# For example, if we have a file ./foo/bar/baz, then we add
8669152e1c5SLionel Sambuc		# "./foo/bar optional type=dir", "./foo optional type=dir",
8679152e1c5SLionel Sambuc		# and ". optional type=dir", unless those directories were
8689152e1c5SLionel Sambuc		# already mentioned explicitly.
8699152e1c5SLionel Sambuc		#
8709152e1c5SLionel Sambuc		${AWK} -f "${rundir}/getdirs.awk" "${spec2}" \
8719152e1c5SLionel Sambuc		| ${SORT} -u >"${specfile_payload}"
8729152e1c5SLionel Sambuc	}
8739152e1c5SLionel Sambuc
8749152e1c5SLionel Sambuc	# Use two pax invocations followed by gzip to create
8759152e1c5SLionel Sambuc	# the tgz file.
8769152e1c5SLionel Sambuc	#
8779152e1c5SLionel Sambuc	# Remove any leading "./" from path names, because that
8789152e1c5SLionel Sambuc	# could confuse tools that work with binary packages.
8799152e1c5SLionel Sambuc	(
8809152e1c5SLionel Sambuc		cd "${SYSPKG_DB_SUBDIR}" && \
8819152e1c5SLionel Sambuc		${PAX} -O -w -d -N"${etcdir}" -M '-s,^\./,,' \
8829152e1c5SLionel Sambuc			-f "${tarball_uncompressed}" \
8839152e1c5SLionel Sambuc			<"${specfile_overhead}" \
8849152e1c5SLionel Sambuc		|| bomb
8859152e1c5SLionel Sambuc	)
8869152e1c5SLionel Sambuc	(
8879152e1c5SLionel Sambuc		cd "${DESTDIR:-/}" && \
8889152e1c5SLionel Sambuc		${PAX} -O -w -d -N"${etcdir}" -M '-s,^\./,,' \
8899152e1c5SLionel Sambuc			-a -f "${tarball_uncompressed}" \
8909152e1c5SLionel Sambuc			<"${specfile_payload}" \
8919152e1c5SLionel Sambuc		|| bomb
8929152e1c5SLionel Sambuc	)
8939152e1c5SLionel Sambuc	${GZIP_CMD} -9n <"${tarball_uncompressed}" >"${binpkgfile}" || bomb
8949152e1c5SLionel Sambuc
8959152e1c5SLionel Sambuc	# (Extra space is to make message line up with "Registered" message.)
8969152e1c5SLionel Sambuc	if ${verbose}; then
8979152e1c5SLionel Sambuc		echo "  Packaged ${binpkgfile}"
8989152e1c5SLionel Sambuc	elif ! ${quiet}; then
8999152e1c5SLionel Sambuc		echo "  Packaged ${binpkgfile##*/}"
9009152e1c5SLionel Sambuc	fi
9019152e1c5SLionel Sambuc
9029152e1c5SLionel Sambuc	cleanup_must_delete_binpkgfile=false
9039152e1c5SLionel Sambuc
9049152e1c5SLionel Sambuc}
9059152e1c5SLionel Sambuc
9069152e1c5SLionel Sambuc#
9079152e1c5SLionel Sambuc# do_register_syspkg() registers the syspkg if appropriate.
9089152e1c5SLionel Sambuc#
9099152e1c5SLionel Sambuc# If SYSPKG_DB_SUBDIR already exists, that might be an error, depending
9109152e1c5SLionel Sambuc# on ${force} and ${update} flags.
9119152e1c5SLionel Sambuc#
9129152e1c5SLionel Sambucdo_register_syspkg()
9139152e1c5SLionel Sambuc{
9149152e1c5SLionel Sambuc	# Check that necessary variables are defined
9159152e1c5SLionel Sambuc	[ -n "${SYSPKG_DB_TOPDIR}" ] || bomb
9169152e1c5SLionel Sambuc	[ -n "${SYSPKG_DB_SUBDIR}" ] || bomb
9179152e1c5SLionel Sambuc
9189152e1c5SLionel Sambuc	# Create SYSPKG_DB_TOPDIR if necessary
9199152e1c5SLionel Sambuc	[ -d "${SYSPKG_DB_TOPDIR}" ] || mkdir -p "${SYSPKG_DB_TOPDIR}" || bomb
9209152e1c5SLionel Sambuc
9219152e1c5SLionel Sambuc	# A function to delete db entries referring to any version of ${pkg}
9229152e1c5SLionel Sambuc	delete_old_db_entries()
9239152e1c5SLionel Sambuc	{
9249152e1c5SLionel Sambuc		init_db_opts # sets dbfile, dbtype, and db_opts
9259152e1c5SLionel Sambuc		dblist="${SCRATCH}/dblist"
9269152e1c5SLionel Sambuc		${DB} ${db_opts} -O "${tab}" "${dbtype}" "${dbfile}" \
9279152e1c5SLionel Sambuc		| ${AWK} -F "${tab}" '$2 ~ /^'"${pkg}"'-[0-9]/ { print $1 }' \
9289152e1c5SLionel Sambuc			>"${dblist}"
9299152e1c5SLionel Sambuc		${DB} -d ${db_opts} -f "${dblist}" "${dbtype}" "${dbfile}"
9309152e1c5SLionel Sambuc	}
9319152e1c5SLionel Sambuc
9329152e1c5SLionel Sambuc	# A function to delete any old version of ${pkg}
9339152e1c5SLionel Sambuc	delete_old_pkg()
9349152e1c5SLionel Sambuc	{
9359152e1c5SLionel Sambuc		pattern="${pkg}-[0-9]*"
9369152e1c5SLionel Sambuc		matches="$(cd "${SYSPKG_DB_TOPDIR}" && echo ${pattern} \
9379152e1c5SLionel Sambuc			|| bomb)"
9389152e1c5SLionel Sambuc		echo >&2 "${NOTE}deleting old pkg (${matches})"
9399152e1c5SLionel Sambuc		cleanup_must_delete_dbsubdir=true
9409152e1c5SLionel Sambuc		delete_old_db_entries
9419152e1c5SLionel Sambuc		( cd "${SYSPKG_DB_TOPDIR}" && rm -rf ${matches} )
9429152e1c5SLionel Sambuc	}
9439152e1c5SLionel Sambuc
9449152e1c5SLionel Sambuc	# Check whether another version of ${pkg} is already registered.
9459152e1c5SLionel Sambuc	pattern="${pkg}-[0-9]*"
9469152e1c5SLionel Sambuc	matches="$(cd "${SYSPKG_DB_TOPDIR}" && echo ${pattern} || bomb)"
9479152e1c5SLionel Sambuc	case "${matches}" in
9489152e1c5SLionel Sambuc	*\*)		;;	# wildcard did not match anything
9499152e1c5SLionel Sambuc	"${pkg}-${t}")	;;	# exact match
9509152e1c5SLionel Sambuc	*)	echo >&2 "${ERRWARNNOTE}another version of ${pkg} is already registered"
9519152e1c5SLionel Sambuc		${verbose} && echo >&2 "	in ${SYSPKG_DB_TOPDIR}"
9529152e1c5SLionel Sambuc		${verbose} && echo >&2 "	(while registering ${pkg}-${t})"
9539152e1c5SLionel Sambuc		${force} || ${update} || bomb
9549152e1c5SLionel Sambuc		delete_old_pkg
9559152e1c5SLionel Sambuc		;;
9569152e1c5SLionel Sambuc	esac
9579152e1c5SLionel Sambuc
9589152e1c5SLionel Sambuc	# Check whether the desired version of ${pkg} is already registered,
9599152e1c5SLionel Sambuc	# and create it if appropriate.
9609152e1c5SLionel Sambuc	if [ -d "${SYSPKG_DB_SUBDIR}" ]; then
9619152e1c5SLionel Sambuc		echo >&2 "${ERRWARNNOTE}${pkg}-${t} is already registered"
9629152e1c5SLionel Sambuc		${verbose} && echo >&2 "	in ${SYSPKG_DB_TOPDIR}"
9639152e1c5SLionel Sambuc		if ${force}; then
9649152e1c5SLionel Sambuc			delete_old_pkg
9659152e1c5SLionel Sambuc			register_syspkg
9669152e1c5SLionel Sambuc		elif ${update}; then
9679152e1c5SLionel Sambuc			#
9689152e1c5SLionel Sambuc			# If all files in SYSPKG_DB_SUBDIR are newer
9699152e1c5SLionel Sambuc			# than all files in the pkg, then do nothing.
9709152e1c5SLionel Sambuc			# Else delete and re-register the pkg.
9719152e1c5SLionel Sambuc			#
9729152e1c5SLionel Sambuc			[ -n "${newestfile}" ] || init_newestfile
9739152e1c5SLionel Sambuc			if [ -n "${newestfile}" ]; then
9749152e1c5SLionel Sambuc				case "$(${FIND} "${SYSPKG_DB_SUBDIR}" -type f \
9759152e1c5SLionel Sambuc					! -newer "${newestfile}" -print)" \
9769152e1c5SLionel Sambuc				in
9779152e1c5SLionel Sambuc				"")	;;
9789152e1c5SLionel Sambuc				*)
9799152e1c5SLionel Sambuc					echo >&2 "${NOTE}some files are newer but pkg version is unchanged"
9809152e1c5SLionel Sambuc					delete_old_pkg
9819152e1c5SLionel Sambuc					register_syspkg
9829152e1c5SLionel Sambuc					;;
9839152e1c5SLionel Sambuc				esac
9849152e1c5SLionel Sambuc
9859152e1c5SLionel Sambuc			else
9869152e1c5SLionel Sambuc				# No files in the pkg?  (This could happen
9879152e1c5SLionel Sambuc				# if a pkg contains only directories.)
9889152e1c5SLionel Sambuc				# Do nothing (keep the already-registered pkg).
9899152e1c5SLionel Sambuc				:
9909152e1c5SLionel Sambuc			fi
9919152e1c5SLionel Sambuc		else
9929152e1c5SLionel Sambuc			bomb
9939152e1c5SLionel Sambuc		fi
9949152e1c5SLionel Sambuc	else
9959152e1c5SLionel Sambuc		register_syspkg
9969152e1c5SLionel Sambuc	fi
9979152e1c5SLionel Sambuc}
9989152e1c5SLionel Sambuc
9999152e1c5SLionel Sambuc#
100084d9c625SLionel Sambuc# do_create_syspkg_tgz() creates the binary pkg (*.tgz) if
10019152e1c5SLionel Sambuc# appropriate.
10029152e1c5SLionel Sambuc#
10039152e1c5SLionel Sambuc# If binpkgfile already exists, that might be an error, depending on
10049152e1c5SLionel Sambuc# ${force} and ${update} flags.
10059152e1c5SLionel Sambuc#
10069152e1c5SLionel Sambucdo_create_syspkg_tgz()
10079152e1c5SLionel Sambuc{
10089152e1c5SLionel Sambuc	[ -n "${binpkgfile}" ] || bomb
10099152e1c5SLionel Sambuc
10109152e1c5SLionel Sambuc	delete_and_recreate()
10119152e1c5SLionel Sambuc	{
10129152e1c5SLionel Sambuc		echo >&2 "${ERRWARNNOTE}deleting and re-creating ${pkg}-${t}.tgz"
10139152e1c5SLionel Sambuc		rm -f "${binpkgfile}"
10149152e1c5SLionel Sambuc		create_syspkg_tgz
10159152e1c5SLionel Sambuc	}
10169152e1c5SLionel Sambuc
10179152e1c5SLionel Sambuc	# Check whether another version of ${pkg} already exists.
10189152e1c5SLionel Sambuc	pattern="${pkg}-[0-9]*"
10199152e1c5SLionel Sambuc	matches="$(cd "${binpkgdir}" && echo ${pattern} || bomb)"
10209152e1c5SLionel Sambuc	case "${matches}" in
10219152e1c5SLionel Sambuc	*\*)	;;	# wildcard did not match anything
10229152e1c5SLionel Sambuc	"${pkg}-${t}.tgz") ;;	# exact match
10239152e1c5SLionel Sambuc	*)	echo >&2 "${ERRWARNNOTE}another version of ${pkg} binary pkg already exists"
10249152e1c5SLionel Sambuc		${verbose} && echo >&2 "	in ${binpkgdir}"
10259152e1c5SLionel Sambuc		${verbose} && echo >&2 "	(while creating ${pkg}-${t}.tgz)"
10269152e1c5SLionel Sambuc		# If neither force nor update, this is a fatal error.
10279152e1c5SLionel Sambuc		# If force but not update, then leave old .tgz in place.
10289152e1c5SLionel Sambuc		# If update, then delete the old .tgz.
10299152e1c5SLionel Sambuc		${force} || ${update} || bomb
10309152e1c5SLionel Sambuc		if ${update}; then
10319152e1c5SLionel Sambuc			echo >&2 "${NOTE}deleting old binary pkg (${matches})"
10329152e1c5SLionel Sambuc			( cd "${binpkgdir}" && rm -f ${matches} || bomb )
10339152e1c5SLionel Sambuc		fi
10349152e1c5SLionel Sambuc		;;
10359152e1c5SLionel Sambuc	esac
10369152e1c5SLionel Sambuc
10379152e1c5SLionel Sambuc	# Check whether the desired version of ${pkg} already exists,
10389152e1c5SLionel Sambuc	# and create it if appropriate.
10399152e1c5SLionel Sambuc	if [ -e "${binpkgfile}" ]; then
10409152e1c5SLionel Sambuc		echo >&2 "${ERRWARNNOTE}${pkg}-${t}.tgz already exists"
10419152e1c5SLionel Sambuc		${verbose} && echo >&2 "	in ${binpkgdir}"
10429152e1c5SLionel Sambuc		if ${force}; then
10439152e1c5SLionel Sambuc			delete_and_recreate
10449152e1c5SLionel Sambuc		elif ${update}; then
10459152e1c5SLionel Sambuc			#
10469152e1c5SLionel Sambuc			# If all files in SYSPKG_DB_SUBDIR are older
10479152e1c5SLionel Sambuc			# than ${binpkgfile}, then do nothing.
10489152e1c5SLionel Sambuc			# Else delete and re-create the tgz.
10499152e1c5SLionel Sambuc			#
10509152e1c5SLionel Sambuc			case "$(${FIND} "${SYSPKG_DB_SUBDIR}" -type f \
10519152e1c5SLionel Sambuc				-newer "${binpkgfile}" -print)" \
10529152e1c5SLionel Sambuc			in
10539152e1c5SLionel Sambuc			"")	;;
10549152e1c5SLionel Sambuc			*)	delete_and_recreate ;;
10559152e1c5SLionel Sambuc			esac
10569152e1c5SLionel Sambuc		else
10579152e1c5SLionel Sambuc			bomb
10589152e1c5SLionel Sambuc		fi
10599152e1c5SLionel Sambuc	else
10609152e1c5SLionel Sambuc		create_syspkg_tgz
10619152e1c5SLionel Sambuc	fi
10629152e1c5SLionel Sambuc}
10639152e1c5SLionel Sambuc
10649152e1c5SLionel Sambuc####################
10659152e1c5SLionel Sambuc# begin main program
10669152e1c5SLionel Sambuc
10679152e1c5SLionel Sambucparse_args ${1+"$@"}
10689152e1c5SLionel Sambucmake_PLIST
10699152e1c5SLionel Sambucchoose_version_number
10709152e1c5SLionel SambucSYSPKG_DB_SUBDIR="${SYSPKG_DB_TOPDIR}/${pkg}-${t}"
10719152e1c5SLionel Sambucdo_register_syspkg
10729152e1c5SLionel Sambucif [ -n "${binpkgdir}" ]; then
10739152e1c5SLionel Sambuc	binpkgfile="${binpkgdir}/${pkg}-${t}.tgz"
10749152e1c5SLionel Sambuc	do_create_syspkg_tgz
10759152e1c5SLionel Sambucfi
10769152e1c5SLionel Sambuc
10779152e1c5SLionel Sambucexit 0
1078