summaryrefslogtreecommitdiff
path: root/src/librefetch/librefetch
blob: 45df21b7b97f8abbbea670946cec3f5b80c243eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env bash
# librefetch
#
# Copyright (C) 2013-2015 Luke Shumaker <lukeshu@sbcglobal.net>
#
# License: GNU GPLv3+
#
# This file is part of Parabola.
#
# Parabola is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Parabola is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Parabola. If not, see <http://www.gnu.org/licenses/>.

. "$(librelib conf)"
. "$(librelib messages)"
setup_traps

tmpfiles=()
trap 'rm -f -- "${tmpfiles[@]}"' EXIT

cmd=${0##*/}
usage() {
	print "Usage: %s [OPTIONS] SOURCE_URL [OUTPUT_FILE]" "$cmd"
	print "Usage: %s -[g|S|M|h]" "$cmd"
	print "Downloads or creates a liberated source tarball."
	echo
	prose "The default mode is to create OUTPUT_FILE, first by trying
	       download mode, then create mode."
	echo
	prose "If OUTPUT_FILE isn't specified, it defaults to the non-directory
	       part of SOURCE_URL, in the current directory."
	echo
	prose "Unless '-C' is specified, if SOURCE_URL does not begin with a
	       configured mirror, create mode is inhibited."
	echo
	prose "In download mode, it simply tries to download SOURCE_URL.  At the
	       beginning of a URL, 'libre://' expands to the first configured
	       mirror."
	echo
	prose "In create mode, it looks at a build script, and uses that to
	       create the source tarball.  SOURCE_URL is ignored, except that it
	       is used to set the default value of OUTPUT_FILE."
	echo
	prose "The default build script is 'PKGBUILD', or 'SRCBUILD' if it
	       exists."
	echo
	prose "Other options, if they are valid \`makepkg\` options, are passed
	       straight to makepkg."
	echo
	print "Example usage:"
	print '  $ %s https://repo.parabola.nu/other/mypackage/mypackage-1.0.tar.gz' "$cmd"
	echo
	print "Options:"
	print " Settings:"
	flag "-C"               "Force create mode (don't download)"
	flag "-D"               "Force download mode (don't create)"
	flag "-p <$(_ FILE)>"   "Use an alternate build script (instead of
	                         'PKGBUILD') If an SRCBUILD exists in the same
	                         directory, it is used instead"
	print " Alternate modes:"
	flag "-g, --geninteg"   "Generate integrity checks for source files"
	flag "-S, --srcbuild"   "Print the effective build script (SRCBUILD)"
	flag "-M, --makepkg"    "Print the effective makepkg script"
	flag "-h, --help"       "Show this message"
}

main() {
	BUILDFILE="$(realpath -Lm PKGBUILD)"
	makepkg_opts=()
	extra_opts=()
	mode=download-create
	if ! parse_options "$@"; then
		usage >&2
		exit 1
	fi

	# Mode: help ###########################################################

	if [[ $mode =~ help ]]; then
		usage
		exit 0
	fi

	########################################################################

	makepkg="$(modified_makepkg "$(which makepkg)")"
	tmpfiles+=("$makepkg")

	# Mode: makepkg ########################################################

	if [[ $mode =~ makepkg ]]; then
		cat "$makepkg"
		exit 0
	fi

	########################################################################

	local BUILDFILEDIR="${BUILDFILE%/*}"
	if [[ -f "${BUILDFILEDIR}/SRCBUILD" ]]; then
		BUILDFILE="${BUILDFILEDIR}/SRCBUILD"
	fi
	if [[ ! -f "$BUILDFILE" ]]; then
		error "%s does not exist." "$BUILDFILE"
		exit 1
	fi
	case "$BUILDFILE" in
		*/SRCBUILD) srcbuild="$(modified_srcbuild "$BUILDFILE")";;
		*)          srcbuild="$(modified_pkgbuild "$BUILDFILE")";;
	esac
	tmpfiles+=("$srcbuild")

	# Mode: checksums ######################################################

	if [[ $mode =~ checksums ]]; then
		"$makepkg" "${makepkg_opts[@]}" -g -p "$srcbuild" |
		case ${BUILDFILE##*/} in
			PKGBUILD) sed -e 's/^[a-z]/mk&/' -e 's/^\s/  &/';;
			SRCBUILD) cat;;
		esac
		exit 0
	fi

	# Mode: srcbuild #######################################################

	if [[ $mode =~ srcbuild ]]; then
		cat "$srcbuild"
		exit 0
	fi

	########################################################################

	local src="${extra_opts[0]}"
	local dst="${extra_opts[1]:-${src##*/}}"

	# Don't canonicalize $src unless mode =~ download, and we've validated
	# that $MIRRORS is configured.

	# Canonicalize $dst
	dst="$(realpath -Lm -- "$dst")"

	# Mode: download #######################################################

	if [[ $mode =~ download ]]; then
		load_files librefetch
		check_vars librefetch MIRRORS DOWNLOADER || exit 1

		# Canonicalize $src
		if [[ "$src" == libre://* ]]; then
			src="${MIRRORS[0]}/${src#libre://}"
		fi

		# check to see if $src is a candidate for create mode
		local inmirror=false;
		local mirror
		for mirror in "${MIRRORS[@]}"; do
			if [[ "$src" == "$mirror"* ]]; then
				inmirror=true
				break
			fi
		done
		if ! $inmirror; then
			# inhibit create
			mode=download
		fi

		local dlcmd="${DOWNLOADER}"
		[[ $dlcmd = *%u* ]] || dlcmd="$dlcmd %u"
		dlcmd="${dlcmd//\%o/\"\$dst\"}"
		dlcmd="${dlcmd//\%u/\"\$src\"}"

		{ eval "$dlcmd"; } >&2 && exit 0
	fi

	# Mode: create #########################################################

	if [[ $mode =~ create ]]; then
		PKGEXT=${dst##*/}
		export PKGEXT=${PKGEXT%.part}
		export PKGDEST=${dst%/*}
		export pkg_file=$dst

		cd "$BUILDFILEDIR"
		"$makepkg" "${makepkg_opts[@]}" -p "$srcbuild" >&2 || exit $?
	fi
}

# sets the variables BUILDFILE, makepkg_opts, extra_opts, mode
parse_options() {
	declare -i ret=0
	local {shrt,long}{1,2}

	# makepkg options
	local makepkg_orig="$(which makepkg)"
	shrt1=($(LC_ALL=C "${makepkg_orig}" -h | sed -rn 's/^ +-(.)(,| [^<]).*/\1/p'))
	shrt2=($(LC_ALL=C "${makepkg_orig}" -h | sed -rn 's/^ +-(.) <.*/\1/p'))
	long1=($(LC_ALL=C "${makepkg_orig}" -h | sed -rn -e 's/^ +(-., )?--(\S*) [^<].*/\2/p'))
	long2=($(LC_ALL=C "${makepkg_orig}" -h | sed -rn 's/^ +--(\S*) <.*/\1/p'))

	# librefetch options
	shrt1+=(C D g S M h)
	shrt2+=(p)
	long1+=(geninteg srcbuild makepkg help)
	long2+=()

	# Feed the options through getopt (sanitize them)
	local shrt="$({ printf '%s\0' "${shrt1[@]}"; printf '%s:\0' "${shrt2[@]}"; } | sort -zu | xargs -0 printf '%s')"
	local long="$({ printf '%s\0' "${long1[@]}"; printf '%s:\0' "${long2[@]}"; } | sort -zu | xargs -0 printf '%s,')"
	local args="$(getopt -n "$cmd" -o "$shrt" -l "${long%,}" -- "$@")"
	ret=$?
	eval set -- "$args"
	unset shrt long args

	# Parse the options.
	local opt optarg have_optarg
	while [[ $# -gt 0 ]]; do
		opt=$1; shift
		have_optarg=false

		if { [[ $opt == --?* ]] && in_array "${opt#--}" "${long2[@]}"; } \
		|| { [[ $opt == -?   ]] && in_array "${opt#-}"  "${shrt2[@]}"; }
		then
			optarg=$1; shift
			have_optarg=true
		fi

		case "$opt" in
			-C) mode=create;;
			-D) mode=download;;
			-g|--geninteg) mode=checksums;;
			-S|--srcbuild) mode=srcbuild;;
			-M|--makepkg) mode=makepkg;;
			-p) BUILDFILE="$(realpath -Lm -- "$optarg")";;
			-h|--help) mode=help;;
			--) break;;
			*)
				makepkg_opts+=("$opt")
				if $have_optarg; then makepkg_opts+=("$optarg"); fi
				;;
		esac
	done
	extra_opts+=("$@")

	# check the number of extra_opts
	case "$mode" in
		help) # don't worry about it
			:;;
		checksums|srcbuild|makepkg) # don't take any extra arguments
			if [[ ${#extra_opts[@]} != 0 ]]; then
				print "%s: found extra non-flag arguments: %s" "$cmd" "${extra_opts[*]}" >&2
				ret=1
			fi
			;;
		*download*|*create*) # take 1 or 2 extra arguments
			if [[ ${#extra_opts[@]} != 1 ]] && [[ ${#extra_opts[@]} != 2 ]]; then
				print "%s: %d non-flag arguments found, expected 1 or 2: %s" "$cmd" ${#extra_opts[@]} >&2
				ret=1
			fi
			;;
	esac

	return $ret
}

# Modify makepkg ###############################################################

# an ERE
makepkg_modify='
/create_package\(\) \{/,/^\}$/ {
	/pkg_file=/d                  # allow us to set pkg_file
	s/"?\$\{comp_files\[@\]\}"?// # do not include .{PKGINFO,INSTALL,CHANGELOG}
	# This is long/gross.  What it does:
	#  - pass --format=ustar to bsdtar, to inhibit it using the pax format
	#  - take the files that would be included in the tarball, and use
	#    find/sort/--files-from to order them for bsdtar
	s/bsdtar(.*) - ([^|]*) \|/find \2 -print0 | LC_ALL=C sort --zero-terminated | bsdtar --null --files-from - --format=ustar --no-recursion \1 - |/
	s/create_signature .*/&; return $?/ # do not procede to create symlinks
}

/tidy_install\(\) \{/,/^\}$/ {
	/for .*PURGE_TARGETS/itidy_install_purge
	/for .*PURGE_TARGETS/,/done/d
	/^\}$/ifind . -exec touch --no-dereference --date="1990-01-01 0:0:0 +0" -- {} +
}

/download_sources\(\) \{/ {
	arm -rf "$srcdir"\nmkdir "$srcdir"
}

s|Making package:|Making source:|
s|Checking runtime dependencies\.\.\.|Checking source dependencies...|
 /Checking buildtime dependencies\.\.\./d

s|srcdir=.*|&-libre|
s|pkgdirbase=.*|&-libre|
s|check_build_status$|:|
'

tidy_install_purge() {
	local pt
	for pt in "${PURGE_TARGETS[@]}"; do
		if [[ ${pt} = "${pt%/}" ]]; then
			if [[ ${pt} = "${pt//\/}" ]]; then
				find . ! -type d -name "${pt}" -exec rm -f -- '{}' +
			else
				rm -f "${pt}"
			fi
		else
			if [[ ${pt%/} = "${pt//\/}" ]]; then
				find . -type d -name "${pt%/}" -exec rm -rf -- '{}' +
			else
				rm -rf "${pt}"
			fi
		fi
	done
}

modified_makepkg() {
	local makepkg_orig=$1
	local makepkg_mine="$(mktemp --tmpdir "${cmd}.XXXXXXXXXXX.makepkg")"
	{
		echo '#!/bin/bash'
		declare -f tidy_install_purge
		sed -r "$makepkg_modify" < "$makepkg_orig"
	} > "$makepkg_mine"
	chmod 755 "$makepkg_mine"
	realpath -es "$makepkg_mine"
}

# Modify PKGBUILD ##############################################################

# a string to be appended
pkgbuild_append='
# do not do split packages
if [[ ${#pkgname[@]} -gt 1 ]]; then
  if [[ -n $pkgbase ]]; then
    pkgname=("$pkgbase")
  else
    pkgname=("$pkgname")
  fi
fi

# copy source variables
source=("${mksource[@]}")       ; unset "source_${CARCH}"
noextract=("${mknoextract[@]}")

declare algo
for algo in "${known_hash_algos[@]}"; do
  eval "${algo}sums=(\"\${mk${algo}sums[@]}\")"
  unset "${algo}sums_${CARCH}"
done

depends=()                      ; unset "depends_${CARCH}"
checkdepends=()                 ; unset "checkdepends_${CARCH}"
makedepends=("${mkdepends[@]}") ; unset "makedepends_${CARCH}"

####
options=(!strip docs libtool staticlibs emptydirs !zipman purge !upx)
PURGE_TARGETS=(.bzr/ .cvs/ .git/ .hg/ .svn/ .makepkg/)

####
if ! declare -f mksource >/dev/null; then
  mksource() { :; }
fi
prepare() { :; }
build() { mksource; }
check() { :; }
package() { cp -a "$srcdir"/*/ "$pkgdir/"; }
'

modified_pkgbuild() {
	local pkgbuild=$1
	local srcbuild="$(mktemp "${pkgbuild%/*}/${cmd}.XXXXXXXXXXX.PKGBUILD.2.SRCBUILD")"
	printf '%s' "$pkgbuild_append" | cat "$pkgbuild" - > "$srcbuild"
	printf '%s\n' "$srcbuild"
}


# Modify SRCBUILD ##############################################################

modified_srcbuild() {
	local orig=$1
	local srcbuild="$(mktemp "${orig%/*}/${cmd}.XXXXXXXXXXX.SRCBUILD.2.SRCBUILD")"
	sed -e '/PKGDEST=/d' -e '/PKGEXT=/d' < "$orig" > "$new"
	printf '%s\n' "$new"
}

main "$@"