void-utils/xif

54 lines
1.0 KiB
Plaintext
Raw Normal View History

2024-02-28 15:13:50 +01:00
#!/bin/sh
2024-02-28 16:23:22 +01:00
# vim: set ts=4 sw=4 et:
2024-02-28 15:13:50 +01:00
2024-02-28 16:23:22 +01:00
[ -z "$*" ] && echo "xif: empty search term" && exit 1
2024-02-28 15:13:50 +01:00
filter='/^.[i*]. /d'
filter1='s/^...\? //'
filter2='s/-[^- ]* / /'
XRS="xbps-query -Rs"
command -v xrs >/dev/null && XRS="xrs"
XI="xbps-install"
command -v xi >/dev/null && XI="xi"
2024-02-28 16:23:22 +01:00
which_sudo() {
if [ "$(id -u)" = "0" ]; then
return
elif command -v sudo >/dev/null && sudo -l | grep -q -e ' ALL$' -e xbps-install; then
echo sudo
elif command -v doas >/dev/null && [ -f /etc/doas.conf ]; then
echo doas
else
echo su
fi
}
2024-02-28 15:13:50 +01:00
install() {
2024-02-28 16:23:22 +01:00
echo "xif: $SUDO $XI $@"
echo
$SUDO $XI $@
2024-02-28 15:13:50 +01:00
}
pkgs() {
2024-02-28 16:23:22 +01:00
echo $@ | tr ' ' '\n' | xargs -i $XRS {} | uniq |\
sed "$filter;$filter1;$filter2" | fzf --layout=reverse -m |\
2024-02-28 15:13:50 +01:00
cut -d' ' -f1 | tr '\n' ' ' | sed "s/ $//"
}
2024-02-28 17:29:15 +01:00
SUDO=$(which_sudo)
command -v fzf >/dev/null || $SUDO xbps-install -S fzf
2024-02-28 16:23:22 +01:00
PKGS="$(pkgs $@)"
[ -z "$PKGS" ] && exit 130
install -S $PKGS
2024-02-28 15:13:50 +01:00
status=$?
if [ $status -eq 16 ]; then
install -u xbps &&
2024-02-28 16:23:22 +01:00
install -S $PKGS
2024-02-28 15:13:50 +01:00
else
exit $status
fi