maketars revision 1.76
1#!/bin/sh
2#
3# $NetBSD: maketars,v 1.76 2013/01/16 21:11:05 christos Exp $
4#
5# Make release tar files for some or all lists.  Usage:
6# maketars [-b] [-x] [-i installdir] [-a arch] [-m machine] [-s setsdir]
7#	[-M metalog] [-N etcdir] [-d destdir] [-t tardir] [setname ...]
8#
9# The default sets are "base comp debug etc games man misc tests text"
10# The X sets are "xbase xcomp xdebug xetc xfont xserver"
11# The extsrc sets are "extbase extcomp extetc"
12#
13# If '-i installdir' is given, copy the given sets to installdir
14# (using pax -rw ...) instead of creating tar files.
15# In this case, remove "etc", "xetc", and "extetc" from the list of default sets.
16#
17
18prog="${0##*/}"
19rundir="$(dirname "$0")" # ${0%/*} isn't good enough when there's no "/"
20. "${rundir}/sets.subr"
21
22# set defaults
23lists=
24tars="${RELEASEDIR}"
25dest="${DESTDIR}"
26metalog=
27installdir=
28etcdir=
29setfilesonly=false
30quiet=false
31
32usage()
33{
34	cat 1>&2 <<USAGE
35Usage: ${prog} [-L base,x,ext] [-b] [-x] [-y] [-i idir] [-a arch] [-m machine] [-s setsdir] [-S]
36	    [-M metalog] [-N etcdir] [-d dest] [-t targetdir] [setname ...]
37	-L base,x,ext	Make specified lists
38	-b		Make both netbsd and x11 lists
39	-x		Only make x11 lists
40		[Default: make netbsd lists]
41	-y		Only make extsrc lists
42		[Default: make netbsd lists]
43	-i idir		Install sets to idir instead of creating tar files
44	-a arch		Set arch (e.g, m68k, mipseb, mipsel, powerpc) [${MACHINE_ARCH}]
45	-m machine	Set machine (e.g, amiga, i386, macppc) [${MACHINE}]
46	-q		Quiet operation
47	-s setsdir	Directory to find sets [${setsdir}]
48	-S		Exit after creating set files ${dest}/etc/mtree/set.*
49	-M metalog	metalog file
50	-N etcdir	etc dir for metalog use [${dest}/etc]
51	-d dest		\${DESTDIR}	[${dest}]
52	-t targetdir	\${RELEASEDIR}	[${tars}]
53	[setname ...]	Sets to build 	[${lists}]
54USAGE
55	exit 1
56}
57
58msg()
59{
60	$quiet || echo $*
61}
62
63# handle args
64while getopts L:bxyi:a:m:qs:SM:N:d:t: ch; do
65	case ${ch} in
66	L)
67		save_IFS="${IFS}"
68		IFS=,
69		for _list in ${OPTARG}; do
70			case $_list in
71			base)	lists="${lists} ${nlists}" ;;
72			x)	lists="${lists} ${xlists}" ;;
73			ext)	lists="${lists} ${extlists}" ;;
74			esac
75		done
76		IFS="${save_IFS}"
77		;;
78	# backward compat
79	b)
80		lists="${nlists} ${xlists}"
81		;;
82	x)
83		lists="${xlists}"
84		;;
85	y)
86		lists="${extlists}"
87		;;
88	i)
89		installdir="${OPTARG}"
90		;;
91	a)
92		MACHINE_ARCH="${OPTARG}"
93		MACHINE_CPU="$(arch_to_cpu "${OPTARG}")"
94		;;
95	m)
96		MACHINE="${OPTARG}"
97		;;
98	q)
99		quiet=true
100		;;
101	s)
102		setsdir="${OPTARG}"
103		;;
104	S)
105		setfilesonly=true
106		;;
107	M)
108		metalog="${OPTARG}"
109		;;
110	N)
111		etcdir="${OPTARG}"
112		;;
113	d)
114		dest="${OPTARG}"
115		;;
116	t)
117		tars="${OPTARG}"
118		;;
119	*)
120		usage
121		;;
122	esac
123done
124shift $((${OPTIND} - 1))
125if [ -n "${installdir}" ]; then	# if -i, remove etc + xetc + extetc from the default list
126	lists="$(echo ${lists} | ${SED} -e 's/ etc / /;s/ xetc / /;s/ extetc / /')"
127fi
128if [ -n "$*" ]; then
129	lists="$*"
130fi
131
132if [ -z "${tars}" -a -z "${installdir}" ]; then
133	echo >&2 "${prog}: \${RELEASEDIR} must be set, or -i must be used"
134	exit 1
135fi
136
137if [ -z "${dest}" ]; then
138	echo >&2 "${prog}: \${DESTDIR} must be set"
139	exit 1
140fi
141: ${etcdir:="${dest}/etc"}
142
143SDIR="$(${MKTEMP} -d "/tmp/${prog}.XXXXXX")"
144TMPFILES=
145
146setlistdir="${dest}/etc/mtree"
147
148cleanup()
149{
150	es=$?
151	/bin/rm -rf "${SDIR}" ${TMPFILES}
152	trap - 0
153	exit ${es}
154}
155trap cleanup 0 2 3 13		# EXIT INT QUIT PIPE
156
157#
158# build the setfiles
159#
160
161for setname in ${lists}; do
162	${HOST_SH} "${setsdir}/makeflist" -a "${MACHINE_ARCH}" -m "${MACHINE}" \
163	    -s "${setsdir}" "${setname}" > "${SDIR}/flist.${setname}" \
164	    || exit 1
165	if [ ! -s "${SDIR}/flist.${setname}" ]; then
166		echo >&2 "makeflist output is empty for ${setname}"
167		exit 1
168	fi
169	if [ -n "${metalog}" ]; then
170		${setfilesonly} && msg "Creating ${setlistdir}/set.${setname}"
171		${AWK} -f "${rundir}/getdirs.awk" "${SDIR}/flist.${setname}" \
172		    > "${SDIR}/flist.${setname}.full" \
173		    || exit 1
174		(
175			echo "/set uname=root gname=wheel"
176			${AWK} -f "${rundir}/join.awk" \
177				"${SDIR}/flist.${setname}.full" "${metalog}"
178			echo "./etc/mtree/set.${setname} type=file mode=0444"
179		) | ${MTREE} -CS -k all -R time -N "${etcdir}" \
180		    > "${setlistdir}/set.${setname}" \
181		    || exit 1
182		# We deliberately do not add set.${setname} to ${metalog},
183		# because we depend on it as an input.
184	elif ! cmp -s "${SDIR}/flist.${setname}" \
185			"${setlistdir}/set.${setname}" >/dev/null 2>&1; then
186		rm -f "${setlistdir}/set.${setname}"
187		cp "${SDIR}/flist.${setname}" "${setlistdir}/set.${setname}" \
188		    || exit 1
189	fi
190done
191if ${setfilesonly}; then		# exit after creating the set lists
192	exit 0
193fi
194
195#
196# now build the tarfiles
197#
198
199GZIP=-9n		# for pax -z
200export GZIP
201es=0
202for setname in ${lists:-${nlists}}; do
203	out="${setname}.tgz"
204	if [ -n "${installdir}" ]; then
205		msg "Copying set ${setname}"
206		( cd "${dest}"; \
207		    ${PAX} -O -rwpe -d -N"${etcdir}" ${metalog:+-M} \
208			"${installdir}" < "${setlistdir}/set.${setname}" )
209	else
210		if [ -n "${metalog}" -a "${tars}/${out}" -nt "${metalog}" ]
211		then
212			msg "${out} is up to date"
213			continue
214		fi
215		msg "Creating ${out}"
216		rm -f "${tars}/${out}"
217		TMPFILES="${TMPFILES} ${tars}/${out}.tmp"
218		( cd "${dest}"; \
219		    ${PAX} -O -w -d -z -N"${etcdir}" ${metalog:+-M} \
220			--use-compress-program ${COMPRESS_PROGRAM} \
221			< "${setlistdir}/set.${setname}" ) \
222			> "${tars}/${out}.tmp" &&
223		mv "${tars}/${out}.tmp" "${tars}/${out}"
224	fi
225	es=$((${es} + $?))
226done
227if [ ${es} -gt 255 ]; then
228	es=255
229fi
230exit ${es}
231