summaryrefslogtreecommitdiff
path: root/db-import-keyring
blob: ef37a5a1d1fd11449844c63c04da398d054a203a (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
#!/bin/bash
# Imports to [libre] specific packages from upstream Arch repos.
#
# License: GPLv3

set -eu -o pipefail
shopt -s extglob globstar nullglob
source "$(librelib messages)"
setup_traps

# usage: expac_file <file.db> <expac_args>
#
# Uses the ${WORKDIR} global
expac_file() {
	local dbfile=$1
	local args=("${@:2}")

	local reponame=${dbfile##*/}
	reponame=${reponame%%.*}

	mkdir -p -- "${WORKDIR}/expac/root"
	cat >"${WORKDIR}/expac/pacman.conf" <<-EOT
	[options]
	RootDir = ${WORKDIR}/expac/root
	DBPath = ${WORKDIR}/expac/root

	[${reponame}]
	Server = file://$(realpath --no-symlinks -- "${dbfile%/*}")
	EOT

	fakeroot pacman --config="${WORKDIR}/expac/pacman.conf" -Syy >/dev/null
	# expac exits with non-zero on emtpy databases, so ignore errors
	expac --config="${WORKDIR}/expac/pacman.conf" --sync "${args[@]}" || true
}

# usage: get_repo_rsync_dir <upstream> <tag>
get_repo_rsync_dir() {
	if ! grep -q ARCHMIRROR -- "${LOCAL_CONFIG%/*}/config.local.$1"; then
		msg "db-import-keyring.conf: Doesn't look like an upstream: %s" "$1" >&2
		exit $EXIT_NOTCONFIGURED
	fi
	(
		source "${LOCAL_CONFIG%/*}/config.local.$1" >&2
		printf '%s\n' "${ARCHMIRROR}/$(repo=${2%-*} arch=${2##*-} envsubst '$repo $arch' <<<"$ARCHPATH")"
	)
}

main() {
	if [[ $# -ne 0 ]]; then
		msg 'usage: %s' "${0##*/}"
		exit $EXIT_INVALIDARGUMENT
	fi

	local config_dir
	config_dir="$(dirname "$(readlink -e "$0")")"
	source "${config_dir}/config" # for FTP_BASE
	source "${config_dir}/db-import-keyring.conf" # for KEYRINGS

	WORKDIR=$(mktemp -dt "${0##*/}.XXXXXXXXXX")
	readonly WORKDIR
	trap "rm -rf -- ${WORKDIR@Q}" EXIT

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

	local fromspec upstream tag pkgname repo repo_rsync_dir from_pkgver to_pkgver filename
	for fromspec in "${KEYRINGS[@]}"; do
		msg "Processing %s" "$fromspec"
		IFS=/ read -r upstream tag pkgname <<<"$fromspec"
		repo_rsync_dir=$(get_repo_rsync_dir "$upstream" "$tag")
		repo=${tag%-*}

		# Download the upstream database
		msg2 'Downloading %s' "${repo_rsync_dir}/${repo}.db"
		mkdir -p -- "${WORKDIR}/dbs/${upstream}"
		rsync --no-motd -mrtLH --no-p "${repo_rsync_dir}/${repo}.db" "${WORKDIR}/dbs/${upstream}/${tag}.db"

		# Compare it against the libre database
		from_pkgver=$(expac_file "${WORKDIR}/dbs/${upstream}/${tag}.db" '%v' "$pkgname")
		to_pkgver=$(expac_file "${FTP_BASE}/libre/os/x86_64/libre.db" '%v' "$pkgname")

		# And download a new keyring if necessary
		if [[ "$from_pkgver" == "$to_pkgver" ]]; then
			msg2 "Up to date: %s == %s" "$from_pkgver" "$to_pkgver"
		else
			msg2 "Updating: %s != %s" "$from_pkgver" "$to_pkgver"
			filename=$(expac_file "${WORKDIR}/dbs/${upstream}/${tag}.db" '%f' "$pkgname")
			mkdir -p -- "${WORKDIR}/staging/libre/"
			rsync --no-motd -mrtLH --no-p "${repo_rsync_dir}/${filename}"{,.sig} "${WORKDIR}/staging/libre/"
		fi
	done

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

	msg "Running db-update..."
	STAGING=${WORKDIR}/staging db-update
}

main "$@"