~dricottone/huttese-apk

ref: ff4e086d7e18386d0296bdd712f7659c0ab8f2d1 huttese-apk/pkgkit -rwxr-xr-x 4.4 KiB
ff4e086d — Drew DeVault Add -openrc, fix sources for git.sr.ht 5 years ago
                                                                                
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
#!/bin/sh -eu
usage() {
	printf "%s\n\n" "Usage: $0 [command...] [flags...] [args...]"
	printf "%s\n" "$0 add-repo [-s] <name> <key>"
	printf "\t%s\n" "Trusts <key> and adds the local repo to /etc/apk/repositories"
	printf "\t%s\n\n" "-s: set this key as default in abuild.conf"
	printf "%s\n" "$0 build [-cu] [-v <pkgver>] <packages...>"
	printf "\t%s\n" "Builds packages"
	printf "\t%s\n" "-c: update checksums"
	printf "\t%s\n" "-u: update pkgrel"
	printf "\t%s\n\n" "-v <pkgver>: update pkgver"
	printf "%s\n" "$0 upload <ssh server> <remote path> <packages...>"
	printf "\t%s\n" "Uploads packages via rsync and updates remote APKINDEX"
}

get_pkgvar() (
	var="$1"
	set +u
	. APKBUILD
	eval 'printf "%s\n" "$'"$var"'"'
)

find_repos() (
	find . -maxdepth 1 -type d -not -name . -not -name .git
)

find_pkg_repo() (
	pkg=$1
	for repo in $(find_repos)
	do
		if [ -d "$repo"/"$pkg" ]
		then
			echo "$repo"
			return 0
		fi
	done
	return 1
)

update_pkgrel() (
	pkg="$1"
	pkgver="$(get_pkgvar pkgver)"
	prev_ver=$(apk search -x "$pkg" | cut -c$((${#pkg}+2))-)
	prev_pkgver=$(printf "%s" "$prev_ver" | cut -d'-' -f1)
	prev_pkgrel=$(printf "%s" "$prev_ver" | cut -d'-' -f2 | cut -d'r' -f2-)
	if [ "$prev_pkgver" == "$pkgver" ]
	then
		pkgrel=$((prev_pkgrel + 1))
		sed -e "s/pkgrel=.*/pkgrel=$pkgrel/" -i APKBUILD
	fi
)

update_pkgver() {
	pkgver="$1"
	prev_pkgver=$(apk search -x ${project} | cut -d '-' -f2)
	sed -e "s/pkgver=.*/pkgver=$pkgver/" -i APKBUILD
	if [ "$prev_pkgver" != "$pkgver" ]
	then
		sed -e "s/pkgrel=.*/pkgrel=0/" -i APKBUILD
	fi
}

add_repo() {
	set_default=0
	while getopts s flag
	do
		case $flag in
			s) set_default=1 ;;
			*) usage && exit 1 ;;
		esac
	done
	shift $((OPTIND-1))
	echo "$@"
	if [ $# -ne 2 ]
	then
		usage
		exit 1
	fi
	repo="$1"
	key="$2"
	mkdir -p ~/packages/$repo
	echo ~/packages/$repo | cat - /etc/apk/repositories > /tmp/repositories
	sudo mv /tmp/repositories /etc/apk/repositories
	sudo openssl rsa -in $key -pubout \
		-out /etc/apk/keys/$(basename "$key").pub
	if [ $set_default -eq 1 ]
	then
		echo "PACKAGER_PRIVKEY="'"'$key'"' >> \
			~/.abuild/abuild.conf
	fi
}

build() (
	checksum=0
	updpkgrel=0
	pkgver=""
	while getopts cuv: flag
	do
		case $flag in
			c) checksum=1 ;;
			u) updpkgrel=1 ;;
			v) pkgver="$OPTARG" ;;
			*) usage && exit 1 ;;
		esac
	done
	shift $((OPTIND-1))
	if [ $# -eq 0 ]
	then
		echo "No packages specified."
		exit 1
	fi
	for pkg in $@
	do
		repo="$(find_pkg_repo "$pkg")"
		(
			cd "$repo"/"$pkg"
			if [ $updpkgrel -eq 1 ]
			then
				update_pkgrel "$pkg"
			fi
			if [ -n "$pkgver" ]
			then
				update_pkgver "$pkgver"
			fi
			if [ $checksum -eq 1 ]
			then
				abuild checksum
			fi
			abuild -r
		)
	done
)

upload_pkg() (
	repo="$1"
	pkg="$2"
	arch="$(uname -m)"

	path="$remote_path/$repo/$arch"
	ssh "$remote" mkdir -p "$path"

	prev_ver=$(apk search -x "$pkg" | cut -c$((${#pkg}+2))-)
	prev_pkgver=$(printf "%s" "$prev_ver" | cut -d'-' -f1)
	prev_pkgrel=$(printf "%s" "$prev_ver" | cut -d'-' -f2 | cut -d'r' -f2-)
	pkgver="$(get_pkgvar pkgver)"
	pkgrel="$(get_pkgvar pkgrel)"

	set -x
	ssh "$remote" rm -f "$path/$pkg-$prev_pkgver-r$prev_pkgrel.apk"
	rsync --blocking-io -rsP \
		~/packages/"$repo/$arch/$pkg-$pkgver-r$pkgrel.apk" "$remote":"$path/"
	set +x
)

update_index() (
	repo="$1"
	arch="$(uname -m)"
	path="$remote_path/$repo/$arch"

	set -x
	ssh "$remote" \
		find "$path" -type f -name '*.apk' -print0 |
		ssh "$remote" xargs -0 apk index \
			--rewrite-arch "$(uname -m)" \
			-x "$path"/APKINDEX.tar.gz \
			-o "$path"/APKINDEX.unsigned.tar.gz
	rsync --blocking-io -rP \
		"$remote:$path/APKINDEX.unsigned.tar.gz" \
		/tmp/APKINDEX.tar.gz
	abuild-sign /tmp/APKINDEX.tar.gz
	rsync --blocking-io -rP /tmp/APKINDEX.tar.gz "$remote:$path/"
	set +x
)

upload() (
	if [ $# -lt 2 ]
	then
		usage
		exit 1
	fi
	remote="$1"
	remote_path="$2"
	shift 2
	if [ $# -eq 0 ]
	then
		echo "No packages specified"
		exit 1
	fi
	repos=""
	for pkg in $@
	do
		repo=$(find_pkg_repo "$pkg")
		if ! echo "$repos" | grep "$repo" >/dev/null
		then
			repos="$repo $repos"
		fi
		(
			cd "$repo"/"$pkg"
			arch="$(get_pkgvar arch)"
			subpkgs="$pkg:_:$arch $(get_pkgvar subpackages)"
			for subpkg in $subpkgs
			do
				name="$(echo "$subpkg" | cut -d':' -f1)"
				upload_pkg "$repo" "$name"
			done
		)
	done
	for repo in $repos
	do
		update_index $repo
	done
)

if [ $# -lt 1 ]
then
	usage
	exit 1
fi
cmd="$1"
shift

case "$cmd" in
	add-repo)
		add_repo "$@"
		;;
	build)
		build "$@"
		;;
	upload)
		upload "$@"
		;;
	help|-h)
		usage
		;;
esac