Files
eos-bash-shared/pkg-name-components
2023-08-11 17:05:12 +03:00

129 lines
3.0 KiB
Bash
Executable File

#!/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 "$@"