124 lines
4 KiB
Bash
124 lines
4 KiB
Bash
#!/usr/bin/env bash
|
||
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 "🔍 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)
|
||
download_url="https://github.com/$REPO/releases/download/$latest_release/$BINARY_NAME-x86_64-unknown-linux-musl.tar.gz"
|
||
|
||
# Skip if already up-to-date
|
||
if [[ "$current_version" == "$latest_version" ]]; then
|
||
echo "✅ Already on the latest version ($latest_version). No action needed."
|
||
exit 0
|
||
fi
|
||
|
||
# Download and install
|
||
echo "⬇️ Downloading $BINARY_NAME $latest_version..."
|
||
curl -sL "$download_url" | tar -xz -C "$TMP_DIR"
|
||
|
||
echo "🛠️ 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 "🎉 Successfully installed $BINARY_NAME $latest_version to $INSTALL_DIR/"
|
||
echo "💡 Ensure '$INSTALL_DIR' is in your PATH:"
|
||
echo " export PATH=\"$INSTALL_DIR:\$PATH\""
|
||
else
|
||
echo "❌ Installation failed!" >&2
|
||
exit 1
|
||
fi
|
||
}
|
||
|
||
# ─< 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_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
|
||
}
|
||
|
||
# ─< 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) generic ;;
|
||
debian) generic ;;
|
||
fedora) generic ;;
|
||
alpine) generic ;;
|
||
arch | manjaro | garuda | endeavour)
|
||
if command_exists paru; then
|
||
paru -S --noconfirm zellij
|
||
elif command_exists yay; then
|
||
yay -S --noconfirm zellij
|
||
fi
|
||
;;
|
||
opensuse*) generic ;;
|
||
*)
|
||
if [ "${ID_LIKE#*debian}" != "$ID_LIKE" ]; then
|
||
generic
|
||
elif [ "${ID_LIKE#*ubuntu}" != "$ID_LIKE" ]; then
|
||
generic
|
||
elif [ "${ID_LIKE#*arch}" != "$ID_LIKE" ]; then
|
||
$_sudo pacman -S zellij
|
||
elif [ "${ID_LIKE#*fedora}" != "$ID_LIKE" ]; then
|
||
generic
|
||
elif [ "${ID_LIKE#*suse}" != "$ID_LIKE" ]; then
|
||
generic
|
||
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
|
||
}
|
||
|
||
if check_root; then
|
||
get_packager
|
||
fi
|