regpkg revision 1.8
11.1Sagc#! /bin/sh
21.1Sagc#
31.8Sapb# $NetBSD: regpkg,v 1.8 2006/01/03 18:31:09 apb Exp $
41.1Sagc#
51.1Sagc# Copyright (c) 2003 Alistair G. Crooks.  All rights reserved.
61.1Sagc#
71.1Sagc# Redistribution and use in source and binary forms, with or without
81.1Sagc# modification, are permitted provided that the following conditions
91.1Sagc# are met:
101.1Sagc# 1. Redistributions of source code must retain the above copyright
111.1Sagc#    notice, this list of conditions and the following disclaimer.
121.1Sagc# 2. Redistributions in binary form must reproduce the above copyright
131.1Sagc#    notice, this list of conditions and the following disclaimer in the
141.1Sagc#    documentation and/or other materials provided with the distribution.
151.1Sagc# 3. All advertising materials mentioning features or use of this software
161.1Sagc#    must display the following acknowledgement:
171.1Sagc#	This product includes software developed by Alistair G. Crooks.
181.1Sagc#	for the NetBSD project.
191.1Sagc# 4. The name of the author may not be used to endorse or promote
201.1Sagc#    products derived from this software without specific prior written
211.1Sagc#    permission.
221.1Sagc#
231.1Sagc# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
241.1Sagc# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
251.1Sagc# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
261.1Sagc# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
271.1Sagc# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
281.1Sagc# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
291.1Sagc# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
301.1Sagc# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
311.1Sagc# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
321.1Sagc# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
331.1Sagc# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
341.1Sagc#
351.1Sagc
361.1Sagc# Usage: regpkg set pkgname
371.1Sagc
381.8Sapbrundir="$(dirname "$0")" # ${0%/*} isn't good enough when there's no "/"
391.6Serh
401.8SapbSYSPKGROOT="${PKG_DBDIR:-/var/db/pkg}"
411.1Sagccase "${SYSPKG_DBDIR}" in
421.1Sagc"")	;;
431.8Sapb*)	SYSPKGROOT="${SYSPKG_DBDIR}" ;;
441.1Sagcesac
451.1Sagc
461.8SapbPLIST="/tmp/.PLIST.$$"
471.1Sagc
481.1Sagcverbose=""
491.1Sagcwhile [ $# -gt 2 ]; do
501.8Sapb	case "$1" in
511.8Sapb	-v)	verbose="$1" ;;
521.1Sagc	*)	break ;;
531.1Sagc	esac
541.1Sagc	shift
551.1Sagcdone
561.1Sagc
571.1Sagcif [ $# -ne 2 ]; then
581.1Sagc	echo "Usage: regpkg set pkgname"
591.1Sagc	exit 1
601.1Sagcfi
611.1Sagc
621.8Sapbpkgset="$1"
631.1Sagc
641.8Sapbpkg="$2"
651.1Sagc
661.8Sapbcase "${verbose}" in
671.8Sapb-v)	echo "Making PLIST for \"${pkgset}\" set and \"${pkg}\" package" ;;
681.1Sagcesac
691.1Sagc
701.1Sagc# create the skeleton PLIST from the pkgset description
711.8Sapb"${rundir}/makeplist" "${pkgset}" "${pkg}" > "${PLIST}"
721.1Sagc
731.2Sjwise# create the pkg tiny version
741.3Sagccase "${SYSPKG_DATES}" in
751.8Sapb"")	tinyvers="$(${AWK} '$1 ~ '/"${pkg}"/' { print $2 }' "${rundir}/versions")"
761.8Sapb	case "${tinyvers}" in
771.3Sagc	"")	tinyvers=0
781.3Sagc		;;
791.3Sagc	esac
801.3Sagc	if [ -f ../../sys/conf/osrelease.sh ]; then
811.8Sapb		osvers="$(${HOST_SH} ../../sys/conf/osrelease.sh)"
821.4Sdyoung		method=osreleases
831.3Sagc	else
841.8Sapb		osvers="$(${UNAME} -r)"
851.3Sagc		method=uname
861.3Sagc	fi
871.8Sapb	t="${osvers}.${tinyvers}"
881.3Sagc	;;
891.8Sapb*)	args="$(${AWK} '
901.3Sagc		/^@cwd/ { prefix = $2; next }
911.3Sagc		/^@dirrm/ { next }
921.8Sapb		{ printf("%s%s\n", prefix, $0) }' "${PLIST}")"
931.3Sagc	# first try for any RCS identifiers in the files
941.3Sagc	t=0
951.8Sapb	case "${args}" in
961.3Sagc	"")	;;
971.8Sapb	*)	t="$(${IDENT} ${args} 2>/dev/null | ${AWK} '
981.3Sagc			BEGIN { last = 0 }
991.3Sagc			$2 == "crt0.c,v" { next }
1001.3Sagc			NF == 8 { t = $4; gsub("/", "", t); if (t > last) last = t; }
1011.8Sapb			END { print last }')"
1021.3Sagc		method=ident
1031.3Sagc		;;
1041.3Sagc	esac
1051.8Sapb	case "${t}" in
1061.3Sagc	0)	# we need the last mtime of the files which make up the package
1071.8Sapb		t="$(${ENV_CMD} TZ=UTC LOCALE=C ${LS} -lT ${args} | ${AWK} '
1081.3Sagc			BEGIN { newest = 0 }
1091.3Sagc			{
1101.3Sagc				t = $9 "";
1111.3Sagc				if ($6 == "Jan") t = t "01";
1121.3Sagc				if ($6 == "Feb") t = t "02";
1131.3Sagc				if ($6 == "Mar") t = t "03";
1141.3Sagc				if ($6 == "Apr") t = t "04";
1151.3Sagc				if ($6 == "May") t = t "05";
1161.3Sagc				if ($6 == "Jun") t = t "06";
1171.3Sagc				if ($6 == "Jul") t = t "07";
1181.3Sagc				if ($6 == "Aug") t = t "08";
1191.3Sagc				if ($6 == "Sep") t = t "09";
1201.3Sagc				if ($6 == "Oct") t = t "10";
1211.3Sagc				if ($6 == "Nov") t = t "11";
1221.3Sagc				if ($6 == "Dec") t = t "12";
1231.3Sagc				if ($7 < 10) t = t "0";
1241.3Sagc				t = t $7;
1251.3Sagc				#these next two lines add the 24h clock onto the date
1261.3Sagc				#gsub(":", "", $8);
1271.3Sagc				#t = sprintf("%s.%4.4s", t, $8);
1281.3Sagc				if (t > newest) newest = t;
1291.3Sagc			}
1301.8Sapb			END { print newest }')"
1311.3Sagc		method=ls
1321.3Sagc		;;
1331.3Sagc	esac
1341.2Sjwise	;;
1351.2Sjwiseesac
1361.2Sjwise
1371.3Sagc# print version number that we're using
1381.8Sapbcase "${verbose}" in
1391.8Sapb-v)	echo "${pkg} - ${t} version using ${method} method" ;;
1401.3Sagcesac
1411.1Sagc
1421.1Sagc# create the directory and minimal contents
1431.8SapbSYSPKGDIR="${SYSPKGROOT}/${pkg}-${t}"
1441.8Sapbif [ -d "${SYSPKGDIR}" ]; then
1451.8Sapb	echo "There is already a ${pkg}-${t} package installed (${SYSPKGDIR})"
1461.1Sagc	exit 1
1471.1Sagcfi
1481.1Sagc
1491.8Sapbmkdir -p "${SYSPKGDIR}"
1501.1Sagc
1511.4Sdyoung# add the dependencies
1521.8Sapb${AWK} '$1 ~ '/"${pkg}"/' { print $2 }' "${rundir}/deps" | ${SORT} | \
1531.8Sapb    ${AWK} '{ print "@pkgdep " $1 "-[0-9]*" }' >> "${PLIST}"
1541.4Sdyoung
1551.1Sagc# create the comment
1561.8Sapbcomment="$(${AWK} '$1 ~ '/"${pkg}"/' { print substr($0, length($1) + 2) }' \
1571.8Sapb	"${rundir}/comments")"
1581.8Sapbcase "${comment}" in
1591.8Sapb"")	echo "***WARNING ***: no comment for \"${pkg}\"" 2>&1
1601.8Sapb	comment="System package for ${pkg}"
1611.1Sagc	;;
1621.1Sagcesac
1631.8Sapbecho "${comment}" > "${SYSPKGDIR}/+COMMENT"
1641.1Sagc
1651.1Sagc# create the description
1661.8Sapbdescr="$(${AWK} '$1 ~ '/"${pkg}"/' { print substr($0, length($1) + 2) }' \
1671.8Sapb	"${rundir}/descrs")"
1681.8Sapbcase "${descr}" in
1691.8Sapb"")	echo "***WARNING ***: no description for \"${pkg}\"" 2>&1
1701.8Sapb	descr="System package for ${pkg}"
1711.1Sagc	;;
1721.1Sagcesac
1731.8Sapbecho "${descr}" > "${SYSPKGDIR}/+DESC"
1741.8Sapb${PRINTF} "\nHomepage:\nhttp://www.NetBSD.org/\n" >> "${SYSPKGDIR}/+DESC"
1751.1Sagc
1761.1Sagc# create the build information
1771.8Sapbif [ x"${BUILD_INFO_CACHE}" = x ]; then
1781.5Sdyoung	{
1791.8Sapb	echo "OPSYS=$(${UNAME} -s)"
1801.8Sapb	echo "OS_VERSION=$(${UNAME} -r)"
1811.7Sapb	${MAKE} -f- all <<EOF
1821.4Sdyoung.include <bsd.own.mk>
1831.4Sdyoungall:
1841.4Sdyoung	@echo OBJECT_FMT=${OBJECT_FMT}
1851.4Sdyoung	@echo MACHINE_ARCH=${MACHINE_ARCH}
1861.4Sdyoung	@echo MACHINE_GNU_ARCH=${MACHINE_GNU_ARCH}
1871.4SdyoungEOF
1881.8Sapb	echo "_PKGTOOLS_VER=$(${PKG_CREATE} -V)"
1891.8Sapb	} > "${SYSPKGDIR}/+BUILD_INFO"
1901.5Sdyoungelse
1911.8Sapb	cp "${BUILD_INFO_CACHE}" "${SYSPKGDIR}/+BUILD_INFO"
1921.5Sdyoungfi
1931.1Sagc
1941.1Sagc# test for attributes
1951.1Sagcargs=""
1961.8Sapbattrs="$(${AWK} '$1 ~ '/"${pkg}"/' { print substr($0, length($1) + 2) }' \
1971.8Sapb	"${rundir}/attrs")"
1981.8Sapbfor a in "${attrs}"; do
1991.8Sapb	case "${attrs}" in
2001.1Sagc	"")		;;
2011.8Sapb	preserve)	echo "${pkg}-${t}" > "${SYSPKGDIR}/+PRESERVE"
2021.8Sapb			args="${args} -n ${SYSPKGDIR}/+PRESERVE"
2031.1Sagc			;;
2041.1Sagc	esac
2051.1Sagcdone
2061.1Sagc
2071.8Sapb${PKG_CREATE} -v -c "${SYSPKGDIR}/+COMMENT" \
2081.8Sapb	-d "${SYSPKGDIR}/+DESC" \
2091.8Sapb	${args} \
2101.8Sapb	-f "${PLIST}" -l -b /dev/null -B "${SYSPKGDIR}/+BUILD_INFO" \
2111.8Sapb	-s /dev/null -S /dev/null -O "${pkg}-${t}.tgz" \
2121.8Sapb		> "${SYSPKGDIR}/+CONTENTS"
2131.1Sagc
2141.8Sapbrm "${PLIST}"
215