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