-
Notifications
You must be signed in to change notification settings - Fork 5
/
pkg-name-components
executable file
·128 lines (107 loc) · 2.98 KB
/
pkg-name-components
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
#!/bin/bash
# Show parts of a given package file name.
DIE() {
echo "$progname: error: $1" >&2
Usage
exit 0
}
Usage() {
cat <<EOF
Usage:
$progname -h|--help
Shows this help.
$progname [--real] "parts" "package-file-name"
--real
Give output as in real parts of the package name.
parts
String of upper case letters consisting of: N V R A C E.
N = pkgname
V = pkgver
R = pkgrel
A = arch
C = compressor
E = epoch
These letters specify what the output will contain.
The output values are separated by the pipe '|' character.
package-file-name
Name of a package file.
Examples:
$ $progname NEVRAC foo-bar-2:22.04.23-1-any.pkg.tar.zst
foo-bar|2|22.04.23|1|any|zst
$ $progname --real NEVRAC foo-bar-2:22.04.23-1-any.pkg.tar.zst
foo-bar-2:22.04.23-1-any.zst
EOF
}
SeparateParts() {
local pkg="$1"
local all=$(echo "$pkg" | sed -E 's/([a-zA-Z0-9@_+][a-zA-Z0-9@._+-]+)-([0-9:]*[^:/\-\ \t]+)-([0-9.]+)-([a-z0-9_]+)\.pkg\.tar\.([a-z0-9]+)$/\1 \2 \3 \4 \5/')
# name........................... epoch..pkgver...... pkgrel. arch...... compress.
all=($(echo $all))
local N=${all[0]}
local V=${all[1]} # may include epoch!
local R=${all[2]}
local A=${all[3]}
local C=${all[4]}
local E="0" # default (=unspecified) epoch is 0
local sep=""
local result=""
local ix item
case "$real_out" in
yes) sep="-" ;; # for most components
no) sep="|" ;; # for all
esac
if [ "${V%:*}" != "$V" ] ; then
E="${V%:*}"
V="${V#*:}"
fi
for ((ix=0; ix < ${#parts}; ix++)) ; do
item="${parts:$ix:1}"
case "$item" in
N) result+="$N$sep" ;;
V) result+="$V$sep" ;;
R) result+="$R$sep" ;;
A) result+="$A$sep" ;;
C) result+="$C$sep" ;;
E) case "$real_out" in
yes) [ $E -ne 0 ] && result+="$E:" ;; # don't show epoch if it is zero
no) result+="$E$sep" ;;
esac
;;
*) DIE "given parameter '$parts' includes an unsupported value '$item'." ;;
esac
done
[ -n "$result" ] || DIE "no result!"
result="${result:: -1}"
[ -n "$result" ] || DIE "result is empty!"
echo "$result"
}
Main() {
local real_out=no
local parts
local pkg
local progname="$(basename "$0")"
case "$1" in
-h | --help)
Usage
return 0
;;
--real)
real_out=yes
shift
;;
-*)
Usage
return 1
;;
esac
parts="$1"
pkg="$2"
if [ -n "$pkg" ] ; then
SeparateParts "$pkg"
else
while read pkg ; do
SeparateParts "$pkg"
done
fi
}
Main "$@"