-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbo-shell.sh
executable file
·461 lines (387 loc) · 10.9 KB
/
sbo-shell.sh
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# Source this file
# sbo-shell - Simple toolset for managing your SBo packages.
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
#
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <http://unlicense.org/>
# Config
# REPO=${REPO:-"~/repo"} # Set your repository directory here
ACCENT=${ACCENT:-31}
PROMPT=${PROMPT:-"(\e[${ACCENT}mSBo\e[0m) $PS1"}
SPLASH=${SPLASH:-yes}
# End of Config
# Constants
INFO_VARS="PRGNAM
VERSION
HOMEPAGE
DOWNLOAD
MD5SUM
DOWNLOAD_x86_64
MD5SUM_x86_64
REQUIRES
MAINTAINER
EMAIL"
ARCH="$(arch)"
# End of Constants
# Internal functions
# Those functions are supossed to be called only
# from other functions.
## Expanding variables in printf's $1 may result in undefined
## behaviour but allows for passing escape sequences as arguments.
intern_warn() { printf "%s: ${1}\n" "$(intern_color WARNING)"; }
intern_info() { printf "%s: ${1}\n" "$(intern_color INFO)"; }
intern_color() { printf "\e[${ACCENT}m%s\e[0m" "$@"; }
intern_sourced_infop() { [ -n "$INFO" ]; }
intern_print_infovar() {
[ -n "$(eval printf '%s' \"\$$1\")" ] &&
eval "echo \"${1}: $"${1}'"'
}
intern_map() {
# $1 - function name, /dev/stdin list
while read -r var; do $1 $var; done
}
intern_tree() {
# Called by _tree from a subshell
TREEDEPTH=${TREEDEPTH:-0}
if [ "$TREEDEPTH" -eq "0" ]; then
printf "%s\n" "$PRGNAM"
else
printf "%${TREEDEPTH}s%s\n" ' ' "$PRGNAM"
fi
[ -n "$REQUIRES" ] && {
echo "$REQUIRES" | tr ' ' '\n' | while read -r intern_tree_PKG; do
OLDCWD="$PWD"
TREEDEPTH=$(( TREEDEPTH + 2 ))
_source_info "$(_find $intern_tree_PKG)"
intern_tree "$REQUIRES"
cd "$OLDCWD"
done
}
}
intern_test() {
:
}
# End of Internal functions
# User functions
# Functions called by the user themself
## Building and Metainfo
_download_sources() {
# Downloads sources from $DOWNLOAD{,_x86_64}
intern_sourced_infop || {
intern_warn "The .info file is not sourced.\nNothing happened."
return 1
}
if [ "$ARCH" = "x86_64" ] && [ -n "$DOWNLOAD_x86_64" ]; then
intern_info "Downloading for x86_64 architecture."
# shellcheck disable=SC2086
wget $DOWNLOAD_x86_64
else
intern_info "Downloading for NON-x86_64 architecture."
# shellcheck disable=SC2086
wget $DOWNLOAD
fi
}
_info() {
[ -n "$1" ] && (
_source_info "$1"
_info
) && return
intern_sourced_infop || {
intern_warn "The .info file is not sourced.\nNothing happened."
return 1
} || return 1
echo "$INFO_VARS" | while read -r VAR; do
intern_print_infovar "$VAR"
done
}
_slackbuild() {
# Runs slackbuild
intern_sourced_infop || {
intern_warn "The .info file is not sourced.\nNothing happened."
return
}
echo "sudo sh \"${PRGNAM}\".SlackBuild"
sudo sh "${PRGNAM}".SlackBuild
}
_source_info() {
# Sources .info file from current directory
# Optional: $1 - Package to source
if [ -n "$1" ]; then
si_OLDCWD="${PWD}"
_goto "$1"
_source_info
cd "$si_OLDCWD"
unset -v si_OLDCWD
return
fi
if [ -e ./*.info ]; then
_unset_info
. ./*.info &&
INFO=true
else
intern_warn "The info file could not be found.\nNothing happened."
fi
}
_unset_info() {
# Unsets all variables from the .info file
# shellcheck disable=SC2046
unset -v $(echo "$INFO_VARS" | tr '\n' ' ')
INFO=""
}
## End of Building and Metainfo
## Searching and Movement
_changelog() (
# Displays newest entry in the ChangeLog.txt for every installed
# SBo package.
# Optional: $1 - Package which newest entry in ChangeLog.txt
# will be displayed
cd /var/lib/pkgtools/packages
for file in ${1:-*_SBo}; do
grep -n -m 1 "/$(echo $file | sed 's/-[0-9].*//'):" "$REPO/ChangeLog.txt"
done | sort -n
)
_dependencies() {
# Prints dependencies of package whose .info file was sourced.
# Optional: $1 - Package whose dependencies will be printed
[ -n "$1" ] && (
_source_info "$1"
printf '%s\n' "$REQUIRES"
) && return
intern_sourced_infop || {
intern_warn "The .info file is not sourced.\nNothing happened."
return
}
printf '%s\n' "$REQUIRES"
}
_find() {
# Searches for package in the repository tree.
# $1 - Package to search
# Optional: -n - Non-exact match
set -f
for arg in "$@"; do
case "$arg" in
-n) NONSTRICT=1 ;;
*)
if [ -z "$find_PKG" ]; then
find_PKG="$arg"
else
intern_warn "Bad argument list: \"$*\"\nNothing happened"
set +f
unset -v NONSTRICT find_PKG arg
return 1
fi
;;
esac
done
[ -z "$find_PKG" ] && {
intern_warn "Find what?\nNothing happened."
return
}
grep $(
if [ -z "$NONSTRICT" ]; then
printf "^.*/%s$" "$find_PKG"
else
printf "^.*/.*%s" "$find_PKG"
fi
) $REPO/PKGLIST | sort -u
unset -v NONSTRICT find_PKG arg
set +f
}
_goto() {
# Changes current directory to package's tree.
# $1 - Package to go to (either 'package' or 'category/package')
gt_OLDCWD="${PWD}"
if [ -z "$1" ]; then
intern_warn "Go to where?\nNothing happened."
return
fi
if [ -d "$REPO/$1" ]; then
cd "$REPO/$1"
else
cd "$REPO"/$(_find "$1")
fi
[ "$?" -gt "0" ] && {
intern_warn "This package doesn't seem to exitst.\nNothing happened."
cd "$gt_OLDCWD"
}
unset -v gt_OLDCWD
}
_repo() {
# Changes cwd to $REPO
cd "$REPO" && echo "$REPO"
}
_tree() {
set -f
for arg in "$@"; do
case "$arg" in
-f) FLAT=1 ;;
*)
if [ -z "$tree_PKG" ]; then
tree_PKG="$arg"
else
intern_warn "Bad argument list: \"$*\"\nNothing happened"
set +f
unset -v FLAT tree_PKG arg
return 1
fi
;;
esac
done
set +f
( # Using subshell so we won't overwritte the .info variables.
[ -n "$tree_PKG" ] && _source_info "$tree_PKG"
[ -z "$REQUIRES" ] && return 1 # Ends subshell
if [ -z "$FLAT" ]; then
intern_tree "$REQUIRES"
else
intern_tree "$REQUIRES" | sort -u | sed 's/\s*//' | awk '!x[$0]++'
fi
) || unset -v FLAT tree_PKG arg
[ -z "$tree_PKG" ] && return 1
unset -v FLAT tree_PKG arg
}
_version_cmp() (
# For every installed SBo package, check if it's version
# compared to the SBo repository tree, if it's out-of-date
# then print the package name, local version and repo version.
cd /var/lib/pkgtools/packages
# Optional: $1 - Instead of checking every SBo package, check $1
PKGS="$(echo ${1}*_SBo)"
set -f
for pkg_full in $PKGS; do
[ -e "$pkg_full" ] || {
intern_warn "Package \"$1\" does not exist.\nNothing happened."
return
}
# Pkg name (e.g. xclip)
version_cmp_PKG="${pkg_full%%-[0-9]*}"
# Pkg name truncated right (e.g. xclip-0.13)
TR="${pkg_full%-*-*}"
# Pkg local version
VERSION_LOCAL="${TR##*-}"
# Sources $VERSION (Pkg's repository version)
. "$REPO/$(_find $version_cmp_PKG)/${version_cmp_PKG}.info" 2>/dev/null || {
intern_warn "$version_cmp_PKG is not in the repository, skipping."
continue
}
[ "$VERSION_LOCAL" != "$VERSION" ] &&
printf "%s: %s %s\n" "$version_cmp_PKG" "$VERSION_LOCAL" "$VERSION"
done
set +f
)
## End of Searching and Movement
## Miscellaneous
_end() {
# Ends the sbo-shell session, unsets all that has been set.
PS1="$OLDPS1"
# Variables
unset -v OLDPS1 REPO INFO
# From .info files
_unset_info
unset -f \
intern_info \
intern_warn \
intern_sourced_infop \
intern_color \
intern_map \
intern_print_infovar \
intern_tree \
intern_test \
_download_sources \
_info \
_pull \
_slackbuild \
_source_info \
_unset_info \
_changelog \
_dependencies \
_find \
_goto \
_repo \
_tree \
_version_cmp \
_help \
_end \
}
_help() {
# Prints help
cat <<EOF
sbo-shell 0.2 - Things not really tested
Building and Metainfo
_download_sources - Downloads sources from the sourced .info file.
_info [PKG] - Displays all variables set in the .info file. If PKG
was passed and it is a valid package name then print
it's .info file.
_slackbuild - Builds slackbuild as a root.
_source_info [PKG] - Sources the .info file from current directory. If PKG
was passed and it is a valid package name then source
it's .info file.
_unset_info - Unsets all variables sourced from the .info files.
Searching and Movement
_changelog [PKG] - Prints the newest entry in $REPO/ChangeLog.txt
either for every installed SBo package or for PKG if
it is a valid SBo package name.
_dependencies [PKG] - Prints dependencies from the sourced .info file. If
PKG was passed then use it's .info file.
_find [-n] PKG - Searches for exact-match PKG in the repository. If -n
was passed, searches for non-exact match instead.
_goto PKG - Changes current directory to the designated package's
repo tree.
_repo - Changes current directory to the SBo repo tree.
_tree [-f] [PKG] - Prints dependency tree for package which .info file
is currently sourced. If PKG was passed and it is a
valid package name then use it's .info file. If '-f'
was passed then prints "flatten" dependency tree.
_version_cmp [PKG] - Prints every installed SBo out-of-date package. If
PKG was passed then check only it.
Miscellaneous
_end - Ends sbo-shell.
_help - Displays this message.
_pull - Git-pulls repository and rebuilds the package list.
EOF
}
_pull() (
# Does git pull and recreates package list
cd "$REPO"
git pull
find . -mindepth 2 -maxdepth 2 -name "*" | sed 's/^\.\///' > PKGLIST
)
## End of Miscellaneous
# End of User functions
# Setting PS1
OLDPS1="$PS1"
PS1="$PROMPT"
unset -v PROMPT
# End of Setting PS1
# Checking whether the $REPO is set
if [ -z "$REPO" ]; then
cat <<EOF >&2
You have to set the \$REPO variable.
sbo-shell did not start.
EOF
_end
else
[ "$SPLASH" = "no" ] ||
printf "Welcome to sbo-shell, type $(intern_color _help) to see all available commands.\n"
fi
# End of Checking whether the $REPO is set