scripts/installs/ghostty.sh
2025-04-14 09:22:15 +02:00

310 lines
8.9 KiB
Bash

#!/bin/sh
# ╭───────────────╮
# │ env functions │
# ╰───────────────╯
# ───────────────────────────────────< 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"
}
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_info "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_info "Root access confirmed."
_sudo=""
fi
}
# ──────────────────────< Check if the given command exists silently >──────────────────────
command_exists() {
command -v "$@" >/dev/null 2>&1
}
_cd() {
cd "$1" || echo_error "Could not cd into $1!"
}
# ─< Distribution detection and installation >────────────────────────────────────────
# checks for variable dist={debian,ubuntu,fedora,arch,suse}
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 | linuxmint | elementary) dist="ubuntu" ;;
debian | kali | zorin | parrot | deepin | raspbian | devuan) dist="debian" ;;
fedora | nobara | rhel | centos) dist="fedora" ;;
arch | manjaro | garuda | endeavour | artix) dist="arch" ;;
opensuse*) dist="suse" ;;
*)
if [ "${ID_LIKE#*debian}" != "$ID_LIKE" ]; then
dist="debian"
echo_note "Detected Debian-based distribution"
elif [ "${ID_LIKE#*ubuntu}" != "$ID_LIKE" ]; then
dist="ubuntu"
echo_note "Detected Ubuntu-based distribution"
elif [ "${ID_LIKE#*arch}" != "$ID_LIKE" ]; then
dist="arch"
echo_note "Detected Arch-based distribution"
elif [ "${ID_LIKE#*fedora}" != "$ID_LIKE" ]; then
dist="fedora"
echo_note "Detected Fedora-based distribution"
elif [ "${ID_LIKE#*suse}" != "$ID_LIKE" ]; then
dist="suse"
echo_note "Detected SUSE-based distribution"
else
echo_error "Unsupported distribution: $ID"
echo_note "Please report this issue with your distribution details"
exit 1
fi
;;
esac
else
echo_error "Unable to detect distribution. /etc/os-release not found."
exit 1
fi
}
# ╭────────────────────────────────────╮
# │ insert your scripts/functions here │
# ╰────────────────────────────────────╯
# ─────────────────────────────────────< dependencies >─────────────────────────────────────
deps_common="git curl tar"
deps_arch="$deps_common gtk4 libadwaita"
deps_deb="$deps_common libgtk-4-dev libadwaita-1-dev"
deps_fedora="$deps_common gtk4-devel zig libadwaita-devel"
deps_suse="$deps_common gtk4-tools libadwaita-devel pkgconf-pkg-config zig"
checkDeps=""
checkDeps() {
get_packager
case "$dist" in
debian | ubuntu)
pkg="apt-get"
echo "Detected packagemanager ${RED}${pkg}"
echo_info "Updating sources.."
$_sudo apt-get update
echo_info "Checking dependencies now.."
sleep 3
for dep in $deps_deb; do
if ! command_exists $dep; then
echo_info "Installing $dep.."
$_sudo apt-get install "$dep" --assume-yes </dev/tty
fi
done
checkDeps="done"
;;
fedora)
pkg="dnf"
echo "Detected packagemanager ${RED}${pkg}"
echo_info "Updating sources.."
$_sudo dnf update
echo_info "Checking dependencies now.."
sleep 3
for dep in $deps_fedora; do
if ! command_exists $dep; then
echo_info "Installing $dep.."
$_sudo dnf install "$dep" </dev/tty
fi
done
checkDeps="done"
;;
suse)
pkg="zypper"
echo "Detected packagemanager ${RED}${pkg}"
echo_info "Updating sources.."
$_sudo zypper refresh
echo_info "Checking dependencies now.."
sleep 3
for dep in $deps_suse; do
if ! command_exists $dep; then
echo_info "Installing $dep.."
$_sudo zypper install "$dep" -y </dev/tty
fi
done
checkDeps="done"
;;
arch)
pkg="pacman"
echo "Detected packagemanager ${RED}${pkg}"
echo_info "Updating sources.."
$_sudo pacman -Syy
echo_info "Checking dependencies now.."
sleep 3
for dep in $deps_arch; do
if ! command_exists $dep; then
echo_info "Installing $dep.."
$_sudo pacman -S "$dep" --noconfirm </dev/tty
fi
done
checkDeps="done"
;;
*)
echo "Something went wrong with your Distribution ${RED}${dist}"
;;
esac
echo_note "Distributional dependence check complete"
}
checkZig() {
if ! command_exists zig; then
echo_warning "Zig was not found, trying to install it now.."
sleep 3
case "$dist" in
arch)
$_sudo pacman -S zig --noconfirm
;;
suse)
$_sudo zypper install zig
;;
*)
installZig
;;
esac
else
echo_note "Zig was already found on the system.."
fi
}
installZig() {
if ! command_exists curl; then
echo_error "Most basic things (like curl) are not there!"
echo_error "Aborting now!"
exit 1
fi
if [ -h /bin/zig ] && [ ! -x /bin/zig ]; then
echo_warning "Found zig at /bin/zig, removing it now.."
$_sudo rm -rf /bin/zig
fi
# zigDir=$(mktemp -d)
zigDir="$HOME/.local/zig"
if [ ! -d $zigDir ]; then
echo_note "Zig build directory getting setup.."
mkdir $zigDir
fi
cd "$zigDir" || echo_error "$zigDir is not a valid directory!"
echo_info "Downloading zig 13.0.. This could take a while.."
curl -fsSL https://ziglang.org/download/0.13.0/zig-linux-x86_64-0.13.0.tar.xz -o zig.tar.xz || echo_error "Could not download zig 13.0.."
echo_info "Uncompressing archive.."
sleep 1
tar -xf ./zig.tar.xz
echo_info "Linking zig for the installation.."
mv zig-* zig >/dev/null 2>&1
$_sudo ln -rs ./zig/zig /bin/zig
}
cloneGhostty() {
if [ ! "$checkDeps" = "done" ]; then
echo_error "Dependencies are not installed, aborting now!"
exit 1
fi
# buildDir=$(mktemp -d)
buildDir="$HOME/.local/ghostty-build"
if [ -d "$buildDir" ] && [ -n "$(ls -A "$buildDir")" ]; then
echo_warning "Ghostty build directory already exists at ${buildDir}, do you want to remove it? (y/n)"
read -r removeBuildDir </dev/tty
case "$removeBuildDir" in
[yY]) $_sudo rm -rf "$buildDir" ;;
*)
echo_error "Aborting now!"
exit 1
;;
esac
else
mkdir -p "$buildDir"
fi
echo_info "Cloning ghostty repository now.."
git clone https://github.com/ghostty-org/ghostty.git "$buildDir" || echo_error "Could not clone ghostty repository!"
_cd "$buildDir"
echo_info "Preparations are done!"
buildGhostty
}
buildGhostty() {
echo_info "Building ghostty now.."
zig build -p /usr -Doptimize=ReleaseFast
}
ErrorExit() {
echo_error "Fatal error!"
echo_error "${1}"
echo_error "Exiting now!"
exit 1
}
# ───────────────────────────────< main function to execute >───────────────────────────────
main() {
if check_root; then
checkDeps || ErrorExit "Step 1/3"
checkZig || ErrorExit "Step 2/3"
cloneGhostty || ErrorExit "Step 3/3"
else
echo_error "Something went terribly wrong!"
fi
}
main </dev/tty