sh/installs/flatpak.sh
2025-02-25 10:58:12 +01:00

152 lines
3.9 KiB
Bash
Executable file

#!/bin/sh -e
# ANSI color codes
RED='\033[0;31m'
CYAN='\033[0;36m'
YELLOW='\033[0;33m'
LIGHT_GREEN='\033[0;92m'
BOLD='\033[1m'
NC='\033[0m' # No Color
echo_error() {
printf "${BOLD}${RED}ERROR: ${NC}${RED}%s${NC}\n" "$1" >&2
}
echo_info() {
printf "${BOLD}${CYAN}INFO: ${NC}${CYAN}%s${NC}\n" "$1"
}
echo_warning() {
printf "${BOLD}${YELLOW}WARNING: ${NC}${YELLOW}%s${NC}\n" "$1"
}
echo_note() {
printf "${BOLD}${LIGHT_GREEN}NOTE: ${NC}${LIGHT_GREEN}%s${NC}\n" "$1"
}
# ─< Silent execution >─────────────────────────────────────────────────────────────────
silentexec() {
"$@" >/dev/null 2>&1
}
# ─< 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_note "User is not root. Using sudo for privileged operations."
_sudo="sudo"
else
echo_error "No sudo found and you're not root! Can't install packages."
return 1
fi
else
echo_note "Root access confirmed."
_sudo=""
fi
}
flatpak_init() {
if command_exists flatpak; then
echo_info "Flatpak exists, initializing flathub repository!"
flatpak remote-add --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo
else
echo_error "Flatpak was not found!"
fi
}
inst_arch() {
echo_note "Using Arch script, btw!"
if command_exists yay; then
i_arch="yay"
elif command_exists paru; then
i_arch="paru"
elif command_exists pacman; then
i_arch="$_sudo pacman"
fi
if [ -n "$i_arch" ]; then
i_arch -S flatpak
else
echo_error "No packager for installation found.. (not even pacman...)"
fi
}
inst_debian() {
echo_note "Using Debian script!"
if command_exists nala; then
$_sudo nala update
$_sudo nala install flatpak -y
elif command_exists apt-get; then
echo_info "Couldn't find nala, falling back to apt-get"
$_sudo apt-get update
$_sudo apt-get install flatpak -y
fi
}
inst_ubuntu() {
echo_note "Using Ubuntu script!"
if command_exists nala; then
$_sudo nala update
$_sudo nala install flatpak -y
elif command_exists apt-get; then
echo_info "Couldn't find nala, falling back to apt-get"
$_sudo apt-get update
$_sudo apt-get install flatpak -y
fi
}
# ─< Distribution detection and installation >────────────────────────────────────────
get_packager() {
if [ -e /etc/os-release ]; then
echo_info "Detecting distribution..."
. /etc/os-release
# Convert $ID and $ID_LIKE to lowercase
ID=$(printf "%s" "$ID" | tr '[:upper:]' '[:lower:]')
ID_LIKE=$(printf "%s" "$ID_LIKE" | tr '[:upper:]' '[:lower:]')
case "$ID" in
ubuntu | pop) inst_ubuntu ;;
debian) inst_debian ;;
fedora) inst_fedora ;;
alpine) inst_alpine ;;
arch | manjaro | garuda | endeavour) inst_arch ;;
opensuse*) inst_opensuse ;;
*)
# Use standard [ ] syntax for string matching
if [ "${ID_LIKE#*debian}" != "$ID_LIKE" ]; then
inst_debian
elif [ "${ID_LIKE#*ubuntu}" != "$ID_LIKE" ]; then
inst_ubuntu
elif [ "${ID_LIKE#*arch}" != "$ID_LIKE" ]; then
inst_arch
elif [ "${ID_LIKE#*fedora}" != "$ID_LIKE" ]; then
inst_fedora
elif [ "${ID_LIKE#*suse}" != "$ID_LIKE" ]; then
inst_opensuse
else
echo_error "Unsupported distribution: $ID"
exit 1
fi
;;
esac
else
echo_error "Unable to detect distribution. /etc/os-release not found."
exit 1
fi
}
install_flatpak() {
check_root &&
get_packager &&
flatpak_init
}
install_flatpak