summaryrefslogtreecommitdiff
path: root/src/librefetch/librefetch
blob: 9ba2a84e534be5ffb7ee1cd392f51e4a92ba2727 (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
#!/bin/bash -euE
# librefetch
# 
# Copyright 2013 Luke Shumaker <lukeshu@sbcglobal.net>
#
# 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/>.

# Overview:
# For 'create' (default) mode, the program flow is easily summarized as:
#     librefetch_extract() {
#         download_sources
#         check_source_integrity
#         extract_sources
#     }
#     librefetch_create() {
#         librefetch_extract
#         run_function_safe 'mksource'
#         create_package
#     }

unset CDPATH
export TEXTDOMAIN='libretools'
export TEXTDOMAINDIR='/usr/share/locale'

# Constants
declare -r confdir='/etc'
declare -r BUILDSCRIPT='PKGBUILD' # The default for ${BUILDFILE}

usage() {
	print "Usage: ${0##*/} [OPTIONS] SOURCEURL OUTFILE"
	print "Downloads or creates a liberated source tarball."
	echo
	print "Options:"
	print "  -c            Force create mode (don't download)"
	print "  -d            Force download mode (don't create)"
	print "  -e            Extract only (don't run \`mksource\` or \`tar\`)"
	print "  -g            Generate checksums for the \`mksource\` array"
	print "  -m            Do not use color"
	print "  -p            Do Use an alternative build script (instead of 'PKGBUILD')"
	print "  -h            Show this message"
}

# The semantics of ${BUILDDIR} and ${startdir} are a little confusing.
#   - ${startdir} is the directory that the program is run from.
#     This is either where output files are created, or where they are linked to
#      if the relevent *DEST is set.
#   - ${BUILDDIR} is the directory that ${srcdir} and ${pkgdir} are in.
#
# How ${BUILDDIR} is set varries between makepkg an librefetch:
# (high precedence is higher up; lower number)
#   - makepkg:
#     1. The `BUILDDIR` environmental variable
#     2. ${startdir}
#   - librefetch
#     1. The directory a manually specified (`-p` flag) PKGBUILD is in
#     2. The `BUILDDIR` environmental variable
#     3. ${startdir}
#
# librefetch forces PKGDEST=$startdir; so it does not symlink the output into
# $startdir
main() {
	# Init/Parse command line ##############################################

	# Global variables
	USE_COLOR=y
	startdir=$PWD
	[[ -n ${BUILDDIR:-} ]] || BUILDDIR=$startdir

	local mode='download-or-create'
	while getopts 'cdegmp:h' arg; do
		case $arg in
			c) mode='create';;
			d) mode='download';;
			e) mode='extract';;
			g) mode='generate-checksums';;
			m) USE_COLOR=n;;
			p)
				BUILDFILE="$(readlink -m "$OPTARG")"
				BUILDDIR="${BUILDFILE%/*}"
				;;
			h) usage; return 0;;
			*) usage; return 1;;
		esac
	done
	shift $(($OPTIND - 1))

	# Lock in these variables globally
	declare -rg USE_COLOR
	declare -rg startdir
	declare -rg BUILDDIR

	# Main routine #########################################################

	if [[ $mode = 'generate-checksums' ]]; then
		librefetch_makepkg_init '/dev/null' '/dev/null'
		librefetch_generate_checksums 
		return $?
	else
		if [[ $# != 2 ]]; then
			usage
			return 1;
		fi
		local src="${1#*://}"
		local dst="$(readlink -m "$2")"

		case $mode in
			download)
				librefetch_download "$src" "$dst"
				;;
			extract)
				librefetch_makepkg_init "$src" "$dst"
				librefetch_extract 
				;;
			download-or-create)
				librefetch_makepkg_init "$src" "$dst"
				librefetch_download "$src" "$dst" || librefetch_create "$dst"
				;;
			create)
				librefetch_makepkg_init "$src" "$dst"
				librefetch_create "$dst"
				;;
		esac
	fi
}

librefetch_makepkg_init() {
	local src=$1
	local dst=$2

	import_makepkg_functions

	makepkg_variables
	makepkg_preimport

	set +u
	makepkg_import
	set -u

	# Override certain variables
	declare -rg srcdir="$BUILDDIR/src-libre"
	declare -rg pkgdir="$BUILDDIR/pkg-libre"
	declare -rg PKGDEST=$startdir

	declare -rg PKGEXT="${src##*/}" # this affects the tar command used, and
	                                # pkg_file
	declare -rg pkg_file=$dst # but we already know what we want this to be
	                          # (so PKGEXT only affects tar)
	declare -rg changelog='' # don't include a special changelog file in the tarball
	declare -rg install=''   # don't include an install script in the tarball

	# copy the mk* source-related arrays to the standard names
	set +u
	for var in source noextract md5sums sha1sums sha256sums sha384sums sha512sums; do
		copy_array "mk${var}" "${var}"
		[[ -n "${!var}" ]] || eval "${var}=''"
		declare -rga "$var"
	done
	set -u
}

# Emulates the behavior of bleeding /usr/share/libretools/conf.sh
# without requiring bleeding libretools
load_conf_libretools_librefetch() {
	. /etc/libretools.d/librefetch.conf
	local ret=0
	for VAR in MIRROR DOWNLOADER; do
		echo "$VAR: ${!VAR:-}"
		if [[ -z ${!VAR:-} ]]; then
			echo "Configure '$VAR' in /etc/libretools.d/librefetch.conf"
			ret=1
		fi >>/dev/stderr
	done
	if [[ $ret != 0 ]]; then
		return 1
	fi
}

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

copy_array() {
	local src=$1
	local dst=$2
	eval "$(printf '%s=("${%s[@]}")' "${dst}" "${src}")"
}

print() {
	local fmt=$1
	shift
	printf -- "$(gettext "$fmt")\n" "$@"
}

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

librefetch_download() {
	local src=$1
	local dst=$2
	load_conf_libretools_librefetch || exit 1

	local url="${MIRROR}/${src}"

	local dlcmd="${DOWNLOADER}"
	dlcmd="${dlcmd//\%o/\"$dst\"}"
	dlcmd="${dlcmd//\%u/\"$url\"}"
	eval "$dlcmd"
}

librefetch_extract() {
	mkdir -p "$srcdir"
	# The 'full' is for compatability with makepkg-git-2abe1f1; whose design
	# I disagree with, but I'll play along.
	# (it could be any value, except 'fast')
	download_sources 'full'
	check_source_integrity
	extract_sources
}	

librefetch_create() {
	local dst=$1
	librefetch_extract

	rm -rf "$pkgdir"
	mkdir -p "$pkgdir"

	if declare -f mksource >/dev/null; then
		run_function_safe "mksource"
	else
		mv "$srcdir"/*/ "$pkgdir"
	fi
	create_package
}

librefetch_generate_checksums() {
	mkdir -p "$srcdir"	
	chmod a-s "$srcdir"
	cd_safe "$srcdir"
	# The 'fast' is for compatability with makepkg-git-2abe1f1; whose design
	# I disagree with, but I'll play along.
	download_sources fast
	generate_checksums | sed -e 's/^[a-z]/mk&/' -e 's/^\s/  &/' && (exit $PIPESTATUS)
}

# Hack to load functions defined in makepkg
import_makepkg_functions() {
	# The file we are reading from
	local makepkg="$(which "${MAKEPKG:-makepkg}")"
	# The temporary file that we will include
	local makepkg_lib="$(mktemp --tmpdir makepkg-lib.XXXXXXXXXX.sh)"

	# Extract all functions defined in $makepkg
	sed -n '/^[a-z0-9_]*() {/,/^}$/p' < $makepkg >> $makepkg_lib
	# Extract variable initialization as `makepkg_variables()`
	{
		echo 'makepkg_variables() {'
		sed -n '/^# Options/,/^\s*$/p'
		echo '}'
	} < $makepkg >> $makepkg_lib

	# Extract the init routine between parsing options and first looking at
	# the PKGBUILD as `makepkg_preimport()`
	{
		echo 'makepkg_preimport() {'
		sed -n '/^# setup signal traps/,/^unset pkgname/p' | sed '$d'
		echo '}'
	} < $makepkg >> $makepkg_lib
	# Extract the PKGBUILD inclusion routine as `makepkg_import()`
	# from the main routine
	{
		echo 'makepkg_import() {'
		sed -n '/^unset pkgname/,/^epoch=/p'
		echo '}'
	} < $makepkg >> $makepkg_lib

	# Modify `create_package()` suit our needs.
	sed -ri '
		/create_package() \{/,/^\}$/ {
			/PKGINFO/d
			/pkg_file=/d
			/check_package/d
			s/"?\$\{comp_files\[@\]\}"?//
		}
		' $makepkg_lib

	# Modify `get_filepath()` to work with `set -e`, as it is frequently
	# expected to not output a value.
	sed -i '
		/get_filepath() {/,/^}$/ {
			s/return 1/return 0/
		}
		' $makepkg_lib

	# Modify everything to make `[[ -z VARIABLE ]]` and `[[ -n VARIABLE ]]`
	# tests work with `set -u`
	sed -ri 's/\[\[ -([nz]) "?\$\{?(!?[A-Za-z0-9_]+)\}?"? /[[ -\1 ${\2:-} /g' $makepkg_lib

	. $makepkg_lib
	echo rm -- $makepkg_lib

	# makepkg-git defines these *_safe functions that makepkg-4.0.3 doesn't use; if
	# ${MAKEPKG} doesn't define them, define them as no-op wrappers for our use.
	for func in cd run_function; do
		if ! declare -f "${func}_safe" >/dev/null; then
			eval "${func}_safe() { ${func} \"\$@\"; }"
		fi
	done
}

main "$@"