~sircmpwn/sr.ht-pkgbuilds

ref: eb016e0678dcb7c4643a8b42e39f7df7ef3b683f sr.ht-pkgbuilds/pkgkit -rwxr-xr-x 3.1 KiB View raw
eb016e06Eli Schwartz git.sr.ht: upgrade to latest version and add the API service 2 months 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
#!/usr/bin/env -S bash -eu
usage() {
	printf "%s\n\n" "Usage: $0 [command...] [flags...] [args...]"
	printf "%s\n" "$0 build [-ciu] [-v <pkgver>] <packages...>"
	printf "\t%s\n" "Builds packages"
	printf "\t%s\n" "-c: update checksums"
	printf "\t%s\n" "-i: install package"
	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> <repo name> <packages...>"
	printf "\t%s\n" "Uploads packages via rsync and updates remote pkg db"
}

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

get_subpkgs() {
	set +u
	. PKGBUILD
	eval 'printf "%s\n" "'"${pkgname[@]}"'"'
}

update_pkgrel() (
	pkg="$1"

	pkgver=$(pacman -Si "$pkg" | grep Version | cut -d ':' -f2 | cut -d ' ' -f2)
	prev_pkgver=$(printf "%s" "$pkgver" | cut -d'-' -f1)
	prev_pkgrel=$(printf "%s" "$pkgver" | cut -d'-' -f2)
	pkgver=$(get_pkgvar pkgver)

	if [ "$prev_pkgver" == "$pkgver" ]
	then
		pkgrel=$((prev_pkgrel + 1))
		sed -e "s/pkgrel=.*/pkgrel=$pkgrel/" -i PKGBUILD
	fi
)

update_pkgver() {
	pkg="$1"
	pkgver="$2"
	prev_pkgver=$(pacman -Si "$pkg" | grep Version | cut -d ':' -f2 | cut -d ' ' -f2)
	prev_pkgver=$(printf "%s" "$prev_pkgver" | cut -d'-' -f1)
	sed -e "s/pkgver=.*/pkgver=$pkgver/" -i PKGBUILD
	if [ "$prev_pkgver" != "$pkgver" ]
	then
		sed -e "s/pkgrel=.*/pkgrel=1/" -i PKGBUILD
	fi
}

build() (
	checksum=0
	updpkgrel=0
	install=0
	pkgver=""
	while getopts ciuv: flag
	do
		case $flag in
			c) checksum=1 ;;
			i) install=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
		(
			cd "$pkg"
			if [ $updpkgrel -eq 1 ]
			then
				update_pkgrel "$pkg"
			fi
			if [ -n "$pkgver" ]
			then
				update_pkgver "$pkg" "$pkgver"
			fi
			if [ $checksum -eq 1 ]
			then
				updpkgsums
			fi
			rm -f *.sig *.pkg.tar.xz
			makepkg --config ../makepkg.conf -s --noconfirm
			if [ $install -eq 1 ]
			then
				sudo pacman --noconfirm -U *.pkg.tar.xz
			fi
		)
	done
)

upload_pkg() (
	repo="$1"
	pkg="$2"

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

	arch="$(get_pkgvar arch)"
	if [ "$arch" == "i686" ]
	then
		arch="x86_64"
	fi

	pkgver="$(get_pkgvar pkgver)"
	pkgrel="$(get_pkgvar pkgrel)"

	set -x
	rsync --blocking-io -rsP \
		"$pkg-$pkgver-$pkgrel-$arch".pkg.tar.xz "$remote":"$path/"

	rsync --blocking-io -rsP \
		"$pkg-$pkgver-$pkgrel-$arch".pkg.tar.xz{,.sig} \
		"$remote":"$path/"
	ssh "$remote" repo-add -n -R \
		"$path/$repo.db.tar.gz" \
		"$path/$pkg-$pkgver-$pkgrel-$arch.pkg.tar.xz"
	set +x
)

upload() (
	if [ $# -lt 3 ]
	then
		usage
		exit 1
	fi
	remote="$1"
	remote_path="$2"
	repo="$3"
	shift 3
	if [ $# -eq 0 ]
	then
		echo "No packages specified"
		exit 1
	fi
	for pkg in $@
	do
		(
			cd "$pkg"
			for subpkg in $(get_subpkgs)
			do
				name="$(echo "$subpkg" | cut -d':' -f1)"
				upload_pkg "$repo" "$name"
			done
		)
	done
)

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

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