xref: /netbsd-src/distrib/sets/regpkg (revision 69b6d498973bb4d7230c2d3c12bd9a032738ec8e)
1#! /bin/sh
2#
3# $NetBSD: regpkg,v 1.6 2004/07/23 15:11:26 erh Exp $
4#
5# Copyright (c) 2003 Alistair G. Crooks.  All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15# 3. All advertising materials mentioning features or use of this software
16#    must display the following acknowledgement:
17#	This product includes software developed by Alistair G. Crooks.
18#	for the NetBSD project.
19# 4. The name of the author may not be used to endorse or promote
20#    products derived from this software without specific prior written
21#    permission.
22#
23# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
24# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
27# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34#
35
36# Usage: regpkg set pkgname
37
38rundir=${0%/*}
39
40SYSPKGROOT=${PKG_DBDIR:-/var/db/pkg}
41case "${SYSPKG_DBDIR}" in
42"")	;;
43*)	SYSPKGROOT=${SYSPKG_DBDIR} ;;
44esac
45
46PLIST=/tmp/.PLIST.$$
47
48verbose=""
49while [ $# -gt 2 ]; do
50	case $1 in
51	-v)	verbose=$1 ;;
52	*)	break ;;
53	esac
54	shift
55done
56
57if [ $# -ne 2 ]; then
58	echo "Usage: regpkg set pkgname"
59	exit 1
60fi
61
62pkgset=$1
63
64pkg=$2
65
66case $verbose in
67-v)	echo "Making PLIST for \"$pkgset\" set and \"$pkg\" package" ;;
68esac
69
70# create the skeleton PLIST from the pkgset description
71$rundir/makeplist $pkgset $pkg > $PLIST
72
73# create the pkg tiny version
74case "${SYSPKG_DATES}" in
75"")	tinyvers=`awk '$1 ~ '/$pkg/' { print $2 }' $rundir/versions`
76	case "$tinyvers" in
77	"")	tinyvers=0
78		;;
79	esac
80	if [ -f ../../sys/conf/osrelease.sh ]; then
81		osvers=`sh ../../sys/conf/osrelease.sh`
82		method=osreleases
83	else
84		osvers=`uname -r`
85		method=uname
86	fi
87	t=$osvers.$tinyvers
88	;;
89*)	args=`awk '
90		/^@cwd/ { prefix = $2; next }
91		/^@dirrm/ { next }
92		{ printf("%s%s\n", prefix, $0) }' $PLIST`
93	# first try for any RCS identifiers in the files
94	t=0
95	case "$args" in
96	"")	;;
97	*)	t=`ident $args 2>/dev/null | awk '
98			BEGIN { last = 0 }
99			$2 == "crt0.c,v" { next }
100			NF == 8 { t = $4; gsub("/", "", t); if (t > last) last = t; }
101			END { print last }'`
102		method=ident
103		;;
104	esac
105	case "$t" in
106	0)	# we need the last mtime of the files which make up the package
107		t=`env TZ=UTC LOCALE=C ls -lT $args | awk '
108			BEGIN { newest = 0 }
109			{
110				t = $9 "";
111				if ($6 == "Jan") t = t "01";
112				if ($6 == "Feb") t = t "02";
113				if ($6 == "Mar") t = t "03";
114				if ($6 == "Apr") t = t "04";
115				if ($6 == "May") t = t "05";
116				if ($6 == "Jun") t = t "06";
117				if ($6 == "Jul") t = t "07";
118				if ($6 == "Aug") t = t "08";
119				if ($6 == "Sep") t = t "09";
120				if ($6 == "Oct") t = t "10";
121				if ($6 == "Nov") t = t "11";
122				if ($6 == "Dec") t = t "12";
123				if ($7 < 10) t = t "0";
124				t = t $7;
125				#these next two lines add the 24h clock onto the date
126				#gsub(":", "", $8);
127				#t = sprintf("%s.%4.4s", t, $8);
128				if (t > newest) newest = t;
129			}
130			END { print newest }'`
131		method=ls
132		;;
133	esac
134	;;
135esac
136
137# print version number that we're using
138case "$verbose" in
139-v)	echo "$pkg - $t version using $method method" ;;
140esac
141
142# create the directory and minimal contents
143SYSPKGDIR=${SYSPKGROOT}/$pkg-$t
144if [ -d ${SYSPKGDIR} ]; then
145	echo "There is already a $pkg-$t package installed (${SYSPKGDIR})"
146	exit 1
147fi
148
149mkdir -p ${SYSPKGDIR}
150
151# add the dependencies
152awk '$1 ~ '/$pkg/' { print $2 }' $rundir/deps | sort | \
153    awk '{ print "@pkgdep " $1 "-[0-9]*" }' >> $PLIST
154
155# create the comment
156comment=`awk '$1 ~ '/$pkg/' { print substr($0, length($1) + 2) }' $rundir/comments`
157case "$comment" in
158"")	echo "***WARNING ***: no comment for \"$pkg\"" 2>&1
159	comment="System package for $pkg"
160	;;
161esac
162echo "$comment" > ${SYSPKGDIR}/+COMMENT
163
164# create the description
165descr=`awk '$1 ~ '/$pkg/' { print substr($0, length($1) + 2) }' $rundir/descrs`
166case "$descr" in
167"")	echo "***WARNING ***: no description for \"$pkg\"" 2>&1
168	descr="System package for $pkg"
169	;;
170esac
171echo "$descr" > ${SYSPKGDIR}/+DESC
172printf "\nHomepage:\nhttp://www.NetBSD.org/\n" >> ${SYSPKGDIR}/+DESC
173
174# create the build information
175if [ x${BUILD_INFO_CACHE} = x ]; then
176	{
177	echo "OPSYS=`uname -s`"
178	echo "OS_VERSION=`uname -r`"
179	make -f- all <<EOF
180.include <bsd.own.mk>
181all:
182	@echo OBJECT_FMT=${OBJECT_FMT}
183	@echo MACHINE_ARCH=${MACHINE_ARCH}
184	@echo MACHINE_GNU_ARCH=${MACHINE_GNU_ARCH}
185EOF
186	echo "_PKGTOOLS_VER=`pkg_create -V`"
187	} > ${SYSPKGDIR}/+BUILD_INFO
188else
189	cp ${BUILD_INFO_CACHE} ${SYSPKGDIR}/+BUILD_INFO
190fi
191
192# test for attributes
193args=""
194attrs=`awk '$1 ~ '/$pkg/' { print substr($0, length($1) + 2) }' $rundir/attrs`
195for a in "$attrs"; do
196	case "$attrs" in
197	"")		;;
198	preserve)	echo "$pkg-$t" > ${SYSPKGDIR}/+PRESERVE
199			args="$args -n ${SYSPKGDIR}/+PRESERVE"
200			;;
201	esac
202done
203
204pkg_create -v -c ${SYSPKGDIR}/+COMMENT \
205	-d ${SYSPKGDIR}/+DESC \
206	$args \
207	-f $PLIST -l -b /dev/null -B ${SYSPKGDIR}/+BUILD_INFO \
208	-s /dev/null -S /dev/null -O $pkg-$t.tgz \
209		> ${SYSPKGDIR}/+CONTENTS
210
211rm $PLIST
212