summaryrefslogtreecommitdiffstats
path: root/repofish
blob: 6b820f6ed913ce2606bb34d077a90a6c147d34cd (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
#!/bin/sh

## You can set these variables here or in your enviroment

#SRC_PATH='/path/to/packages/sources'
#REPO_PATH='/path/to/archlinux/repo'
#REPO_NAME='repo_name'
#ARCHIVE_PATH='/path/to/archlinux/archive/repo'

## Search variables
URL_PREFIX='https://aur.archlinux.org/'
SEARCH_STR='rpc/?v=5&type=search&by=name-desc&arg='

add_packages() {
	declare -a pkg=("${!1}")
	echo -e '\033[1;34m:: \033[1;37mAdding following packages: '"${pkg[@]}"'\033[m'
	for i in "${pkg[@]}"; do
		if [ ! -d "${SRC_PATH}/$i" ]; then
			cd ${SRC_PATH}
			git clone ${URL_PREFIX}$i.git
		else
			echo -e '\033[1;33mWARNING:\033[m Package '"$i"'  already present.'
		fi
	done
}

clone_package() {
	if [ ! -d "${SRC_PATH}/$(echo $1 | awk -F '/' '{print $NF}' | cut -d '.' -f 1)" ]; then
		echo -e '\033[1;34m:: \033[1;37mCloning url: '"$1"'\033[m'
		cd ${SRC_PATH}
		git clone $1
	else
		echo -e '\033[1;33mWARNING:\033[m Package already present.'
	fi
}

rm_packages() {
	declare -a pkg=("${!1}")
	echo -e '\033[1;34m:: \033[1;37mRemoving following packages: '"${pkg[@]}"'\033[m'
	for i in "${pkg[@]}"; do
		if [ -d "${SRC_PATH}/$i" ]; then
			rm -rf ${SRC_PATH}/$i
		else
			echo -e '\033[1;33mWARNING:\033[m Package '"$i"' not present.'
		fi
	done
}

check_package() {
	if [ -d "${SRC_PATH}/$1" ]; then
		${EDITOR} ${SRC_PATH}/$1/PKGBUILD
	else
		echo -e '\033[1;33mWARNING:\033[m Package '"$1"' not found.'
	fi
}

list_packages() {
	echo -e '\033[1;34m:: \033[1;37mPackages locally available:\033[m'
	find ${SRC_PATH}/* -type d -prune | awk -F '/' '{print $NF}'
}

clean_packages() {
	if [ "$1" != '' ]; then
		declare -a pkg=("${!1}")
		echo -e '\033[1;34m:: \033[1;37mCleaning following packages: '"${pkg[@]}"'\033[m'
		for i in "${pkg[@]}"; do
			find ${SRC_PATH} -type f -iname "*$i*.pkg.tar.xz" -exec rm {} \;
		done
	else
		echo -e '\033[1;34m:: \033[1;37mCleaning all packages\033[m'
		find ${SRC_PATH} -type f -iname "*.pkg.tar.xz" -exec rm {} \;
	fi
}

build_packages() {
	if [ "$1" != '' ]; then
		declare -a pkg=("${!1}")
		echo -e '\033[1;34m:: \033[1;37mBuilding following packages: '"${pkg[@]}"'\033[m'
		for i in "${pkg[@]}"; do
			cd $(find ${SRC_PATH}/* -type d -prune -iname "*$i")
			makepkg -Cs
		done
	else
		echo -e '\033[1;34m:: \033[1;37mBuilding all packages\033[m'
		for i in $(find ${SRC_PATH}/* -type d -prune)
		do
			cd $i
			makepkg -Cs
		done
	fi
}

upgrade_packages() {
	if [ "$1" != '' ]; then
		declare -a pkg=("${!1}")
		echo -e '\033[1;34m:: \033[1;37mUpgrading following packages: '"${pkg[@]}"'\033[m'
		for i in "${pkg[@]}"; do
			cd $(find ${SRC_PATH}/* -type d -prune -iname "*$i")
			git fetch
			git diff origin/master
			git merge
			makepkg -Cs
		done
	else
		echo -e '\033[1;34m:: \033[1;37mUpgrading all packages\033[m'
		for i in $(find ${SRC_PATH}/* -type d -prune)
		do
			cd $i
			git fetch
			git diff origin/master
			git merge
			makepkg -Cs
		done
	fi
}

update_packages() {
	if [ "$1" != '' ]; then
		declare -a pkg=("${!1}")
		echo -e '\033[1;34m:: \033[1;37mUpdating following packages: '"${pkg[@]}"'\033[m'
		for i in "${pkg[@]}"; do
			cd $(find ${SRC_PATH}/* -type d -prune -iname "*$i")
			git fetch
			git diff origin/master
			git merge
		done
	else
		echo -e '\033[1;34m:: \033[1;37mUpdating all packages\033[m'
		for i in $(find ${SRC_PATH}/* -type d -prune)
		do
			cd $i
			git fetch
			git diff origin/master
			git merge
		done
	fi
}

install_packages() {
	declare -a pkg=("${!1}")
	echo -e '\033[1;34m:: \033[1;37mInstalling following packages: '"${pkg[@]}"'\033[m'
	for i in "${pkg[@]}"; do
		if [ -d "${SRC_PATH}/$i" ]; then
			cd $(find ${SRC_PATH}/* -type d -prune -iname "*$i")
			makepkg -Csi
		else
			echo -e '\033[1;33mWARNING:\033[m Package '"$i"' not found.'
		fi
	done
}

update_repo() {
	if [ "$1" != '' ]; then
		declare -a pkg=("${!1}")
		echo -e '\033[1;34m:: \033[1;37mUpdating following packages in local repository: '"${pkg[@]}"'\033[m'
		for i in "${pkg[@]}"; do
			find ${SRC_PATH} -type f -iname "*$i*pkg.tar.xz" -exec cp {} ${REPO_PATH}/x86_64/ \;
			repo-add ${REPO_PATH}/x86_64/${REPO_NAME}.db.tar.gz ${REPO_PATH}/x86_64/*$i*.pkg.tar.xz
		done
	else
		echo -e '\033[1;34m:: \033[1;37mUpdating all packages in local repository\033[m'
		find ${SRC_PATH} -type f -iname "*pkg.tar.xz" -exec cp {} ${REPO_PATH}/x86_64/ \;
		repo-add ${REPO_PATH}/x86_64/${REPO_NAME}.db.tar.gz ${REPO_PATH}/x86_64/*.pkg.tar.xz
	fi
}

archive_repo_pacakges() {
	if [ "$1" != '' ]; then
		declare -a pkg=("${!1}")
		echo -e '\033[1;34m:: \033[1;37mArchiving following packages in local repository: '"${pkg[@]}"'\033[m'
		for i in "${pkg[@]}"; do
			find ${REPO_PATH}/x86_64/ -type f -iname "*$i*pkg.tar.xz" -exec mv {} ${ARCHIVE_PATH}/x86_64/ \;
		done
	else
		echo -e '\033[1;34m:: \033[1;37mArchiving all packages in local repository\033[m'
		find ${REPO_PATH}/x86_64/ -type f -iname "*pkg.tar.xz" -exec mv {} ${ARCHIVE_PATH}/x86_64/ \;
	fi
}

search_packages() {
	declare -a key=("${!1}")
	LIST_INST=$($0 list | tail -n +2 | tr '\n' '|')
	CURL_JSN=$(curl -s $(echo ${URL_PREFIX}${SEARCH_STR}"${key[@]}" | sed -e 's/ /+/g'))
	OUT_STR=$(jq -r '.results | .[] | [.Name, .Version], .Description' <<< ${CURL_JSN})
	SEARCH_OUT=$(sed -e 's/\\//g' -e 's/\[/\\033\[1\;36m\[/g' -e 's/",/\\033\[1\;33m", \[\\033\[1\;32m/g' \
		-e 's/\]/\\033\[1\;33m\]\\033\[1\;37m\]/g' <<< ${OUT_STR})
	echo -ne '\033[1;34m:: \033[1;37mYou searched using the following keywords (matching package name and/or description): "'
	echo -ne \\033\[1\;33m${key[@]}\\033\[1\;37m\"
	echo -e ${SEARCH_OUT} | sed -e 's/\[ /\n/g' -e 's/ *\] /\n\t/g' -e 's/", / /g' -e 's/"//g' | \
		sed -E '/'"${LIST_INST::-1}"'[^-]/ s/$/ '"$(echo -e \\033\[1\;35m[ Locally available ]\\033\[1\;37m)"'/'
}

set_variables() {
	echo -e '\033[1;33mWARNING:\033[m SRC_PATH variable not set.'
	echo
	echo 'You need to set the following variables in your profile:'
	echo
	echo "  export SRC_PATH='/path/to/packages/sources'"
	echo "  export REPO_PATH='/path/to/archlinux/repo'"
	echo "  export REPO_NAME='repo_name'"
	echo
	echo 'The REPO_PATH and REPO_NAME variables are only needed if you use the local repo feature.'
	echo "Just set the SRC_PATH if you don't intend to use it."
}

print_help() {
	echo ''"$0"' SEPPIOFISH repo tool for Archlinux and AUR helper'
	echo
	echo ' USAGE: '"$0"' COMMAND PKGNAME(s) (default ALL)'
	echo
	echo ' add              adds packages from from AUR'
	echo ' archive          archives packages of local repository'
	echo ' clone (url)      clones git package repository'
	echo ' rm               remove packages'
	echo ' check            check PKGBUILD'
	echo ' list             lists locally available packages'
	echo ' update-repo      updates the packages in the repo'
	echo ' build            builds the packages'
	echo ' upgrade          upgrades and builds the packages'
	echo ' update           updates the packages'
	echo ' install          builds and installs the package'
	echo ' clean            cleans the packages'
	echo ' search           searches packages in the AUR'
	echo ' help             prints this help'
	echo

	if [ "${SRC_PATH}" == '' ];then
		set_variables
	fi
}

if [ "$1" == 'help' ]; then
	if [ "$2" != '' ]; then
		echo -e '\033[1;31mERROR:\033[m too many arguments.'
	else
		print_help
	fi
elif [ "${SRC_PATH}" == '' ];then
	set_variables
elif [ ! -d "${SRC_PATH}" ]; then
	echo -e '\033[1;31mERROR:\033[m SRC_PATH variable set to invalid path.'
elif [ "$1" == '' ]; then
	echo -e '\033[1;33mWARNING:\033[m no command called.'
elif [ "$1" == 'add' ]; then
	if [ "$2" == '' ]; then
		echo -e '\033[1;31mERROR:\033[m missing argument.'
	else
		N=2
		while [ "${!N}" != '' ]; do
			packages[$((N-2))]=${!N}
			let N=N+1
		done
		add_packages packages[@]
	fi
elif [ "$1" == 'clone' ]; then
	if [ "$3" != '' ]; then
		echo -e '\033[1;31mERROR:\033[m too many arguments.'
	elif [ "$2" == '' ]; then
		echo -e '\033[1;31mERROR:\033[m missing argument.'
	else
		clone_package $2
	fi
elif [ "$1" == 'rm' ]; then
	if [ "$2" == '' ]; then
		echo -e '\033[1;31mERROR:\033[m missing argument.'
	else
		N=2
		while [ "${!N}" != '' ]; do
			packages[$((N-2))]=${!N}
			let N=N+1
		done
		rm_packages packages[@]
	fi
elif [ "$1" == 'check' ]; then
	if [ "$3" != '' ]; then
		echo -e '\033[1;31mERROR:\033[m too many arguments.'
	elif [ "$2" == '' ]; then
		echo -e '\033[1;31mERROR:\033[m missing argument.'
	else
		check_package $2
	fi
elif [ "$1" == 'list' ]; then
	if [ "$2" != '' ]; then
		echo -e '\033[1;31mERROR:\033[m too many arguments.'
	else
		list_packages
	fi
elif [ "$1" == 'update-repo' ]; then
	if [ "${REPO_PATH}" == '' ];then
		echo -e '\033[1;31mERROR:\033[m REPO_PATH variable not set.'
	elif [ ! -d "${REPO_PATH}" ]; then
		echo -e '\033[1;31mERROR:\033[m REPO_PATH variable set to invalid path.'
	elif [ "${REPO_NAME}" == '' ];then
		echo -e '\033[1;31mERROR:\033[m REPO_NAME variable not set.'
	elif [ "$2" != '' ]; then
		N=2
		while [ "${!N}" != '' ]; do
			packages[$((N-2))]=${!N}
			let N=N+1
		done
		update_repo packages[@]
	else
		update_repo
	fi
elif [ "$1" == 'archive' ]; then
	if [ "${REPO_PATH}" == '' ];then
		echo -e '\033[1;31mERROR:\033[m REPO_PATH variable not set.'
	elif [ ! -d "${REPO_PATH}" ]; then
		echo -e '\033[1;31mERROR:\033[m REPO_PATH variable set to invalid path.'
	elif [ "${REPO_NAME}" == '' ];then
		echo -e '\033[1;31mERROR:\033[m REPO_NAME variable not set.'
	elif [ "${ARCHIVE_PATH}" == '' ];then
		echo -e '\033[1;31mERROR:\033[m ARCHIVE_PATH variable not set.'
	elif [ ! -d "${ARCHIVE_PATH}" ]; then
		echo -e '\033[1;31mERROR:\033[m ARCHIVE_PATH variable set to invalid path.'
	elif [ "$2" != '' ]; then
		N=2
		while [ "${!N}" != '' ]; do
			packages[$((N-2))]=${!N}
			let N=N+1
		done
		archive_repo_packages packages[@]
	else
		archive_repo_packages
	fi
elif [ "$1" == 'build' ]; then
	if [ "$2" != '' ]; then
		N=2
		while [ "${!N}" != '' ]; do
			packages[$((N-2))]=${!N}
			let N=N+1
		done
		build_packages packages[@]
	else
		build_packages
	fi
elif [ "$1" == 'upgrade' ]; then
	if [ "$2" != '' ]; then
		N=2
		while [ "${!N}" != '' ]; do
			packages[$((N-2))]=${!N}
			let N=N+1
		done
		upgrade_packages packages[@]
	else
		upgrade_packages
	fi
elif [ "$1" == 'update' ]; then
	if [ "$2" != '' ]; then
		N=2
		while [ "${!N}" != '' ]; do
			packages[$((N-2))]=${!N}
			let N=N+1
		done
		update_packages packages[@]
	else
		update_packages
	fi
elif [ "$1" == 'install' ]; then
	if [ "$2" == '' ]; then
		echo -e '\033[1;31mERROR:\033[m missing argument.'
	else
		N=2
		while [ "${!N}" != '' ]; do
			packages[$((N-2))]=${!N}
			let N=N+1
		done
		install_packages packages[@]
	fi
elif [ "$1" == 'clean' ]; then
	if [ "$2" != '' ]; then
		N=2
		while [ "${!N}" != '' ]; do
			packages[$((N-2))]=${!N}
			let N=N+1
		done
		clean_packages packages[@]
	else
		clean_packages
	fi
elif [ "$1" == 'search' ]; then
	if [ "$2" == '' ]; then
		echo -e '\033[1;31mERROR:\033[m missing argument(s).'
	else
		N=2
		while [ "${!N}" != '' ]; do
			keywords[$((N-2))]=${!N}
			let N=N+1
		done
		search_packages keywords[@]
	fi
else
	echo -e '\033[1;31mERROR:\033[m unrecocnized command.'
fi