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