imports/pika.sh
2025-06-25 20:36:50 +02:00

200 lines
4.1 KiB
Bash
Executable file

#!/usr/bin/env bash
# ─< Check if the given command exists silently >─────────────────────────────────────────
command-exists() {
command -v "$@" >/dev/null 2>&1
}
# ─< Check if the user is root and set sudo variable if necessary >───────────────────────
check_root() {
if [ "$(id -u)" -ne 0 ]; then
if command-exists sudo; then
echo "User is not root. Using sudo for privileged operations."
_sudo="sudo -E"
else
echo "No sudo found and you're not root! Can't install packages."
return 1
fi
else
echo "Root access confirmed."
_sudo=""
fi
}
get-packager() {
local packagers
packagers=(
apt
dnf
pacman
zypper
apk
)
for packager in "${packagers[@]}"; do
if command-exists $packager; then
case $packager in
apt)
pkg-install() {
if command-exists nala; then
$_sudo nala install --assume-yes "$@"
else
$_sudo apt install --assume-yes "$@"
fi
}
update-func() {
if command-exists nala; then
$_sudo nala update
$_sudo nala upgrade --assume-yes --full
else
$_sudo apt-get update
$_sudo apt-get upgrade --assume-yes
fi
}
;;
dnf)
pkg-install() {
$_sudo dnf -y install "$@"
}
update-func() {
$_sudo dnf update -y
}
;;
pacman)
pkg-install() {
if command-exists paru; then
paru --noconfirm -S "$@"
elif command-exists yay; then
yay --noconfirm -S "$@"
else
$_sudo pacman --noconfirm -S "$@"
fi
}
update-func() {
if command-exists paru; then
paru -Syyu
elif command-exists yay; then
yay -Syyu
else
$_sudo pacman -Syyu
fi
}
;;
zypper)
pkg-install() {
$_sudo zypper install "$@"
}
update-func() {
$_sudo zypper ref
$_sudo zypper update
}
;;
apk)
pkg-install() {
$_sudo apk add "$@"
}
update-func() {
$_sudo apk update
$_sudo apk upgrade
}
;;
esac
fi
done
}
source-script() {
local i import
i=$1
import="$(mktemp)"
if command-exists curl; then
curl -fsSL $i -o $import
else
echo "curl is required, but missing.."
exit 1
fi
bash "$import"
sleep 0.3
rm "$import"
}
script-exists() {
local install_url=https://git.k4li.de/scripts/installs/raw/branch/main
local script_url arr=()
arr=("$@")
for pkg in "${arr[@]}"; do
script_url="${install_url}/${pkg}.sh"
if curl -fsSL $script_url &>/dev/null; then
return 0
else
return 69
fi
done
}
install-func() {
local pkg
for pkg in "$@"; do
case "$pkg" in
nvim) pkg=neovim ;;
esac
if script-exists $pkg; then
source-script "https://git.k4li.de/scripts/installs/raw/branch/main/$pkg.sh"
else
pkg-install "$pkg"
fi
done
exit 0
}
help-func() {
echo "How to use?"
echo
echo "pika [argument] [options]"
echo "argument=[i|install]"
echo "options:"
echo "i|install: <pkg name or space separated list"
}
# setup-env() {
# local script=https://git.k4li.de/scripts/imports/raw/branch/main/distros.sh
#
# if ! command-exists pkg-install && ! command-exists check-and-install; then
# source-script $script
# fi
# }
check_root
get-packager
# NOTE:
# arguments to parse:
# i or install = installs the package after wards either with the pika scripts,
# or directly via the packagemanager, if no script is available under this name.
#
for arg in "$@"; do
if [ -n "$arg" ]; then
case $arg in
install | i)
shift
install-func "$@"
;;
update | u)
shift
update-func "${@:-}"
;;
*)
help-func
;;
esac
fi
done