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