installs/pika.sh
2025-06-25 20:48:56 +02:00

346 lines
6.8 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# ─< Check if the given command exists silently >─────────────────────────────────────────
command-exists() {
command -v "$@" >/dev/null 2>&1
}
readonly _q='?'
readonly _a=''
readonly _o='◌'
readonly _O='●'
readonly _mark='✓'
readonly _warn='!'
readonly _cross='✗'
show_cursor() {
printf "\033[?25h"
}
up() {
printf "\033[A"
}
down() {
printf "\033[B"
}
bol() {
printf "\r"
}
eol() {
printf "\033[999C"
}
cl() {
printf "\033[2K"
}
upclear() {
up
bol
cl
}
line() {
printf "\n"
}
pen() {
local new_line="\n"
local text="${*: -1}"
local args=("${@:1:$#-1}")
local format_code=""
local reset_code="\033[0m"
for arg in "${args[@]}"; do
arg=${arg,,}
case "$arg" in
-n) new_line="" ;;
bold) format_code+="\033[1m" ;;
italic) format_code+="\033[3m" ;;
underline) format_code+="\033[4m" ;;
black) format_code+="\033[30m" ;;
red) format_code+="\033[31m" ;;
green) format_code+="\033[32m" ;;
yellow) format_code+="\033[33m" ;;
blue) format_code+="\033[34m" ;;
purple) format_code+="\033[35m" ;;
cyan) format_code+="\033[36m" ;;
white) format_code+="\033[37m" ;;
grey | gray) format_code+="\033[90m" ;;
[0-9]*)
if [[ "$arg" =~ ^[0-9]+$ ]] && [ "$arg" -ge 0 ] && [ "$arg" -le 255 ]; then
format_code+="\033[38;5;${arg}m"
fi
;;
*) ;;
esac
done
printf "%b%s%b%b" "${format_code}" "${text}" "${reset_code}" "${new_line}"
}
confirm() {
local default="y"
local hint="[Y/n]"
local prompt
local response
while [[ $# -gt 0 ]]; do
case "$1" in
--default-no)
default="n"
hint="[y/N]"
shift
;;
--default-yes)
shift
;;
*) break ;;
esac
done
prompt=$(
pen -n blue "${_q:-?} "
pen -n "$1"
pen gray " $hint"
pen -n blue "${_a:-} "
)
show_cursor
while true; do
read -r -p "$prompt" response
response="${response:-$default}"
case "$response" in
[Yy] | [Yy][Ee][Ss])
upclear
pen -n blue "${_a:-} "
pen "yes"
return 0
;;
[Nn] | [Nn][Oo])
upclear
pen -n blue "${_a:-} "
pen "no"
return 1
;;
[qQ] | quit)
upclear
pen red bold "User exited the session with exit code 69!"
pen red "This is $(pen bold red 'NOT') an accident!"
exit 69
;;
*)
echo
warn "Please answer yes or no."
;;
esac
done
}
# ─< 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
}
check-pika() {
local url tmp bin
url="https://git.k4li.de/scripts/installs/raw/branch/main/pika.sh"
tmp="$(mktemp)"
bin=/usr/local/bin/pika
if ! command-exists pika; then
pen "The 'pika' binary was not found on this system."
if confirm "Do you want to install it now?" </dev/tty; then
curl -o $tmp -fsSL "$url"
if [ -f "$tmp" ]; then
pen bold red "Copying binary now to $bin"
$_sudo cp $tmp $bin
fi
fi
else
return 0
fi
}
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
if check-pika; then
get-packager
fi
# 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