This commit is contained in:
2025-08-09 08:58:36 +01:00
parent 7d4775e2b3
commit 0c512d7a4b

View File

@@ -1,35 +1,14 @@
#!/bin/bash #!/bin/bash
set -euo pipefail set -euo pipefail
# -- utils --------------------------------------------------------- # -------- Platform detection --------
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]$ ]]
}
# -- platform detection --------------------------------------------
detect_platform() { detect_platform() {
if [[ "$OSTYPE" == "darwin"* ]]; then if [[ "$OSTYPE" == "darwin"* ]]; then
OS="macos"; PKG_MGR="brew" OS="macos"; PKG_MGR="brew"
if ! command -v brew &>/dev/null; then if ! command -v brew &>/dev/null; then
if prompt_yn "Homebrew not found. Install it now? (y/N): " N; then
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)" eval "$(/opt/homebrew/bin/brew shellenv)"
else
echo "Homebrew is required to install Fish on macOS. Exiting."; exit 1
fi
fi fi
elif command -v apt &>/dev/null; then elif command -v apt &>/dev/null; then
OS="debian"; PKG_MGR="apt" OS="debian"; PKG_MGR="apt"
@@ -44,7 +23,7 @@ detect_platform() {
fi fi
} }
# -- debian repo/key ----------------------------------------------- # -------- 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
sudo apt update && sudo apt install -y gnupg sudo apt update && sudo apt install -y gnupg
@@ -59,7 +38,7 @@ add_fish_repo_debian() {
sudo apt update sudo apt update
} }
# -- install/update fish ------------------------------------------- # -------- Install/Update fish --------
install_fish() { install_fish() {
case "$OS" in case "$OS" in
debian) add_fish_repo_debian; sudo apt install -y fish ;; debian) add_fish_repo_debian; sudo apt install -y fish ;;
@@ -69,49 +48,40 @@ install_fish() {
esac esac
} }
update_fish() { update_fish_if_available() {
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
sudo apt install -y fish sudo apt install -y fish
fi fi
fi
;; ;;
dnf|yum) dnf|yum)
sudo "$PKG_MGR" check-update || true sudo "$PKG_MGR" check-update || true
if "$PKG_MGR" list updates | grep -q "^fish"; then if "$PKG_MGR" list updates | grep -q "^fish"; then
if prompt_yn "Fish update available. Update now? (y/N): " N; then
sudo "$PKG_MGR" update -y fish sudo "$PKG_MGR" update -y fish
fi fi
fi
;; ;;
pacman) pacman)
sudo pacman -Sy --noconfirm sudo pacman -Sy --noconfirm
if pacman -Qu | grep -q "^fish"; then if pacman -Qu | grep -q "^fish"; then
if prompt_yn "Fish update available. Update now? (y/N): " N; then
sudo pacman -S --noconfirm fish sudo pacman -S --noconfirm fish
fi fi
fi
;; ;;
brew) brew)
brew update brew update
if brew outdated | grep -q "^fish"; then if brew outdated | grep -q "^fish"; then
if prompt_yn "Fish update available. Update now? (y/N): " N; then
brew upgrade fish brew upgrade fish
fi fi
fi
;; ;;
esac esac
} }
# -- fisher & plugins ---------------------------------------------- # -------- Fisher & plugins (stdin isolated) --------
install_fisher() { install_fisher() {
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
# prevent fisher from reading our script via stdin
fish /tmp/install_fisher.fish </dev/null fish /tmp/install_fisher.fish </dev/null
} }
@@ -119,27 +89,26 @@ install_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
# QUIET mode: suppress noisy file listing. Remove redirection if you want verbosity.
if [[ "${FISH_VERBOSE:-0}" == "1" ]]; then
fish /tmp/fish_plugins.fish </dev/null
else
fish /tmp/fish_plugins.fish </dev/null >/dev/null 2>&1 fish /tmp/fish_plugins.fish </dev/null >/dev/null 2>&1
fi
} }
# -- main ----------------------------------------------------------- # -------- 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." update_fish_if_available
update_fish
else else
install_fish install_fish
fi fi
if prompt_yn "Install Fisher plugin manager and recommended plugins? (y/N): " N; then # Always ensure Fisher and plugins are present (idempotent)
install_fisher install_fisher
install_fish_plugins install_fish_plugins
echo "Fisher and plugins installed!"
else
echo "Skipping plugin installation."
fi
echo "✅ Fish shell setup complete on ${OS}!" echo "✅ Fish + Fisher + plugins ensured on ${OS}."