This commit is contained in:
2025-08-09 08:45:37 +01:00
parent e4505c13df
commit 7d4775e2b3

View File

@@ -1,158 +1,145 @@
#!/bin/bash #!/bin/bash
set -euo pipefail
set -e # -- utils ---------------------------------------------------------
prompt_yn() {
# usage: prompt_yn "Question (y/N): " N
local prompt="${1:-Proceed (y/N): }"
local default="${2:-N}"
local ans=""
if [ -t 0 ] && [ -t 1 ]; then
# interactive: read from the TTY (not stdin)
read -r -p "$prompt" ans </dev/tty || true
else
# non-interactive (e.g., curl|bash): use default
echo "$prompt$default"
ans="$default"
fi
[[ ${ans:-$default} =~ ^[Yy]$ ]]
}
# 🔍 Detect OS and Package Manager # -- platform detection --------------------------------------------
detect_platform() { detect_platform() {
if [[ "$OSTYPE" == "darwin"* ]]; then if [[ "$OSTYPE" == "darwin"* ]]; then
OS="macos" OS="macos"; PKG_MGR="brew"
if ! command -v brew &>/dev/null; then if ! command -v brew &>/dev/null; then
echo "🍺 Homebrew not found. Install it now? (y/N): " if prompt_yn "Homebrew not found. Install it now? (y/N): " N; then
read -r confirm /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
if [[ "$confirm" =~ ^[Yy]$ ]]; then echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" eval "$(/opt/homebrew/bin/brew shellenv)"
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile else
eval "$(/opt/homebrew/bin/brew shellenv)" echo "Homebrew is required to install Fish on macOS. Exiting."; exit 1
else fi
echo "❌ Homebrew is required to install Fish on macOS. Exiting."
exit 1
fi
fi
PKG_MGR="brew"
elif command -v apt &>/dev/null; then
OS="debian"
PKG_MGR="apt"
elif command -v dnf &>/dev/null; then
OS="rhel"
PKG_MGR="dnf"
elif command -v yum &>/dev/null; then
OS="rhel"
PKG_MGR="yum"
elif command -v pacman &>/dev/null; then
OS="arch"
PKG_MGR="pacman"
else
echo "❌ Unsupported platform."
exit 1
fi fi
elif command -v apt &>/dev/null; then
OS="debian"; PKG_MGR="apt"
elif command -v dnf &>/dev/null; then
OS="rhel"; PKG_MGR="dnf"
elif command -v yum &>/dev/null; then
OS="rhel"; PKG_MGR="yum"
elif command -v pacman &>/dev/null; then
OS="arch"; PKG_MGR="pacman"
else
echo "Unsupported platform."; exit 1
fi
} }
# 🔐 Install gpg if not present (Debian only) # -- debian repo/key -----------------------------------------------
install_gpg_if_needed() { install_gpg_if_needed() {
if [[ "$OS" == "debian" ]] && ! command -v gpg &>/dev/null; then if [[ "${OS}" == "debian" ]] && ! command -v gpg &>/dev/null; then
echo "🔑 gpg not found. Installing..." sudo apt update && sudo apt install -y gnupg
sudo apt update && sudo apt install -y gnupg fi
fi
} }
# 📦 Add Fish shell repo for Debian/Ubuntu
add_fish_repo_debian() { add_fish_repo_debian() {
echo "Adding Fish shell repo for Debian/Ubuntu..." echo 'deb http://download.opensuse.org/repositories/shells:/fish:/release:/4/Debian_12/ /' \
echo 'deb http://download.opensuse.org/repositories/shells:/fish:/release:/4/Debian_12/ /' | \ | sudo tee /etc/apt/sources.list.d/fish.list >/dev/null
sudo tee /etc/apt/sources.list.d/fish.list curl -fsSL https://download.opensuse.org/repositories/shells:fish:release:4/Debian_12/Release.key \
curl -fsSL https://download.opensuse.org/repositories/shells:fish:release:4/Debian_12/Release.key | \ | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/fish.gpg >/dev/null
gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/fish.gpg > /dev/null sudo apt update
sudo apt update
} }
# 🐟 Install Fish shell # -- install/update fish -------------------------------------------
install_fish() { install_fish() {
echo "🐟 Installing Fish shell for $OS..." case "$OS" in
case "$OS" in debian) add_fish_repo_debian; sudo apt install -y fish ;;
debian) rhel) sudo "$PKG_MGR" install -y fish ;;
add_fish_repo_debian arch) sudo pacman -Sy --noconfirm fish ;;
sudo apt install -y fish macos) brew install fish ;;
;; esac
rhel)
sudo $PKG_MGR install -y fish
;;
arch)
sudo pacman -Sy --noconfirm fish
;;
macos)
brew install fish
;;
esac
} }
# ♻️ Update Fish shell if needed
update_fish() { update_fish() {
echo "🔄 Checking for updates to Fish shell..." case "$PKG_MGR" in
case "$PKG_MGR" in apt)
apt) sudo apt update
sudo apt update if apt list --upgradable 2>/dev/null | grep -q "^fish/"; then
if apt list --upgradable 2>/dev/null | grep -q "^fish/"; then if prompt_yn "Fish update available. Update now? (y/N): " N; then
read -rp "An update is available. Update Fish? (y/N): " confirm sudo apt install -y fish
[[ "$confirm" =~ ^[Yy]$ ]] && sudo apt install -y fish fi
else fi
echo "Fish is already up to date." ;;
fi dnf|yum)
;; sudo "$PKG_MGR" check-update || true
dnf|yum) if "$PKG_MGR" list updates | grep -q "^fish"; then
sudo $PKG_MGR check-update || true if prompt_yn "Fish update available. Update now? (y/N): " N; then
if $PKG_MGR list updates | grep -q "^fish"; then sudo "$PKG_MGR" update -y fish
read -rp "An update is available. Update Fish? (y/N): " confirm fi
[[ "$confirm" =~ ^[Yy]$ ]] && sudo $PKG_MGR update -y fish fi
else ;;
echo "Fish is already up to date." pacman)
fi sudo pacman -Sy --noconfirm
;; if pacman -Qu | grep -q "^fish"; then
pacman) if prompt_yn "Fish update available. Update now? (y/N): " N; then
sudo pacman -Sy --noconfirm sudo pacman -S --noconfirm fish
if pacman -Qu | grep -q "^fish"; then fi
read -rp "An update is available. Update Fish? (y/N): " confirm fi
[[ "$confirm" =~ ^[Yy]$ ]] && sudo pacman -S --noconfirm fish ;;
else brew)
echo "Fish is already up to date." brew update
fi if brew outdated | grep -q "^fish"; then
;; if prompt_yn "Fish update available. Update now? (y/N): " N; then
brew) brew upgrade fish
brew update fi
if brew outdated | grep -q "^fish"; then fi
read -rp "An update is available. Update Fish? (y/N): " confirm ;;
[[ "$confirm" =~ ^[Yy]$ ]] && brew upgrade fish esac
else
echo "Fish is already up to date."
fi
;;
esac
} }
# 🎣 Install Fisher plugin manager (stdin isolated) # -- fisher & plugins ----------------------------------------------
install_fisher() { install_fisher() {
echo "Installing Fisher plugin manager..." curl -fsSL https://git.mycod.in/apt/fish_install/raw/branch/main/install_fisher.fish \
curl -fsSL https://git.mycod.in/apt/fish_install/raw/branch/main/install_fisher.fish -o /tmp/install_fisher.fish -o /tmp/install_fisher.fish
chmod +x /tmp/install_fisher.fish chmod +x /tmp/install_fisher.fish
fish /tmp/install_fisher.fish </dev/null # prevent fisher from reading our script via stdin
fish /tmp/install_fisher.fish </dev/null
} }
# 🧩 Install plugins via external .fish script (stdin isolated)
install_fish_plugins() { install_fish_plugins() {
echo "Installing recommended Fish plugins..." curl -fsSL https://git.mycod.in/apt/fish_install/raw/branch/main/fish_plugins.fish \
curl -fsSL https://git.mycod.in/apt/fish_install/raw/branch/main/fish_plugins.fish -o /tmp/fish_plugins.fish -o /tmp/fish_plugins.fish
chmod +x /tmp/fish_plugins.fish chmod +x /tmp/fish_plugins.fish
fish /tmp/fish_plugins.fish </dev/null # QUIET mode: suppress noisy file listing. Remove redirection if you want verbosity.
fish /tmp/fish_plugins.fish </dev/null >/dev/null 2>&1
} }
# 🏁 Main script # -- main -----------------------------------------------------------
detect_platform detect_platform
install_gpg_if_needed install_gpg_if_needed
if command -v fish &>/dev/null; then if command -v fish &>/dev/null; then
echo "Fish shell is already installed." echo "Fish shell is already installed."
update_fish update_fish
else else
install_fish install_fish
fi fi
# Ask if user wants to install Fisher and plugins if prompt_yn "Install Fisher plugin manager and recommended plugins? (y/N): " N; then
read -rp "Do you want to install Fisher plugin manager and some plugins? (y/N): " install_plugins install_fisher
if [[ "$install_plugins" =~ ^[Yy]$ ]]; then install_fish_plugins
install_fisher echo "Fisher and plugins installed!"
install_fish_plugins
echo "Fisher and plugins installed!"
else else
echo "Skipping plugin installation." echo "Skipping plugin installation."
fi fi
echo "✅ Fish shell setup complete on $OS!" echo "✅ Fish shell setup complete on ${OS}!"