installs/zellij.sh
2025-05-12 15:55:43 +02:00

124 lines
3.4 KiB
Bash
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}
# WHY:
# This import will give you the following variables:
# _sudo="sudo -E" <- only if non root user
# distro = <distro name, like 'arch', 'debian', 'fedora'..>
# arch = bool
# fedora = bool
# opensuse = bool....
# You can then use it for, `if $arch; then`
# Also this gives you the _install command, which installs a package pased on the packagemanager/distro used.
# CAUTION:
# This only wokrs for generic package names, like neovim, or vim, or tmux etc..
# not every package packagemanager has the same packagenames for their packages..
getImports() {
i="https://git.k4li.de/scripts/imports/raw/branch/main/distros.sh"
import="$(mktemp)"
if command_exists curl; then
curl -fsSL $i -o $import
else
echo "curl is required, but missing.."
exit 1
fi
source "$import"
sleep 0.3
rm "$import"
echo_warning "cleaned $import"
}
generic() {
set -euo pipefail
# Configuration
INSTALL_DIR="${HOME}/.local/bin" # Prefer ~/.local/bin (add to PATH if not already)
BINARY_NAME="zellij"
REPO="zellij-org/zellij"
TMP_DIR=$(mktemp -d)
# Cleanup on exit
trap 'rm -rf "$TMP_DIR"' EXIT
# Ensure install directory exists
mkdir -p "$INSTALL_DIR"
# Check if zellij is already installed
current_version=""
if command -v "$BINARY_NAME" >/dev/null 2>&1; then
current_version=$($BINARY_NAME --version | awk '{print $2}')
echo " Found existing $BINARY_NAME (version: $current_version)"
fi
# Fetch latest release from GitHub
echo_info "🔍 Checking latest release of $REPO..."
latest_release=$(curl -s "https://api.github.com/repos/$REPO/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
latest_version=${latest_release#v} # Remove 'v' prefix (e.g., v0.42.2 → 0.42.2)
# Detect system architecture
ARCH=$(uname -m)
case "$ARCH" in
x86_64)
ARCH="x86_64"
;;
aarch64 | arm64)
ARCH="aarch64"
;;
*)
echo_error "❌ Unsupported architecture: $ARCH" >&2
exit 1
;;
esac
download_url="https://github.com/$REPO/releases/download/$latest_release/$BINARY_NAME-$ARCH-unknown-linux-musl.tar.gz"
# Skip if already up-to-date
if [[ "$current_version" == "$latest_version" ]]; then
echo_note "✅ Already on the latest version ($latest_version). No action needed."
exit 0
fi
# Download and install
echo_info "⬇️ Downloading $BINARY_NAME $latest_version for $ARCH..."
curl -sL "$download_url" | tar -xz -C "$TMP_DIR"
echo_info "🛠️ Installing to $INSTALL_DIR..."
mv -f "$TMP_DIR/$BINARY_NAME" "$INSTALL_DIR/"
# Verify
if [[ -f "$INSTALL_DIR/$BINARY_NAME" ]]; then
chmod +x "$INSTALL_DIR/$BINARY_NAME"
echo_info "🎉 Successfully installed $BINARY_NAME $latest_version to $INSTALL_DIR/"
echo_warning "💡 Ensure '$INSTALL_DIR' is in your PATH:"
echo_warning " export PATH=\"$INSTALL_DIR:\$PATH\""
else
echo_error "❌ Installation failed!" >&2
exit 1
fi
}
setup() {
if getImports; then
case "$@" in
--silent | -s)
echo_warning "Executing script silently.."
silent=true
;;
*)
silent=false
;;
esac
fi
}
if setup "$@"; then
case "$distro" in
arch | opensuse) _install zellij ;;
*) generic ;;
esac
fi