487 lines
16 KiB
Bash
Executable File
487 lines
16 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# =============================================================================
|
|
# install.sh — zsh + Oh My Zsh bootstrap with a curated ~/.zshrc
|
|
#
|
|
# Run it straight from your own host, nothing to download first:
|
|
# bash <(curl -Lso- https://ad.derty.sbs/leo/dotfiles/raw/branch/main/install.sh)
|
|
#
|
|
# Flags:
|
|
# -y, --yes do not ask for confirmation
|
|
# -n, --dry-run print what would happen, change nothing
|
|
# --no-chsh do not change the login shell
|
|
# --no-plugins skip the zsh-users plugins (offline / slow mirrors)
|
|
# --config-only print the bundled ~/.zshrc to stdout and exit
|
|
# -h, --help this text
|
|
#
|
|
# Environment:
|
|
# GITHUB_MIRROR e.g. https://ghfast.top/https://github.com (proxy prefix)
|
|
# ZSH_THEME theme to use instead of robbyrussell
|
|
# =============================================================================
|
|
set -Eeuo pipefail
|
|
|
|
SCRIPT_VERSION="1.0.0"
|
|
DOTFILES_URL="${DOTFILES_URL:-https://ad.derty.sbs/leo/dotfiles/raw/branch/main}"
|
|
GITHUB="${GITHUB_MIRROR:-https://github.com}"
|
|
MARKER='# managed-by: install.sh'
|
|
|
|
ASSUME_YES=0
|
|
DRY_RUN=0
|
|
DO_CHSH=1
|
|
DO_PLUGINS=1
|
|
CONFIG_ONLY=0
|
|
ZSHRC_BACKUP=""
|
|
|
|
if [[ -t 1 ]]; then
|
|
C_RST=$'\033[0m'; C_B=$'\033[1m'; C_G=$'\033[32m'
|
|
C_Y=$'\033[33m'; C_R=$'\033[31m'; C_C=$'\033[36m'
|
|
else
|
|
C_RST=; C_B=; C_G=; C_Y=; C_R=; C_C=
|
|
fi
|
|
|
|
info() { printf '%s==>%s %s\n' "$C_C" "$C_RST" "$*"; }
|
|
ok() { printf '%s ok%s %s\n' "$C_G" "$C_RST" "$*"; }
|
|
warn() { printf '%swarn%s %s\n' "$C_Y" "$C_RST" "$*" >&2; }
|
|
die() { printf '%sfail%s %s\n' "$C_R" "$C_RST" "$*" >&2; exit 1; }
|
|
|
|
usage() {
|
|
cat <<EOF
|
|
zsh + Oh My Zsh installer ${SCRIPT_VERSION}
|
|
|
|
One-liner:
|
|
bash <(curl -Lso- ${DOTFILES_URL}/install.sh)
|
|
|
|
Options:
|
|
-y, --yes do not ask for confirmation
|
|
-n, --dry-run print what would happen, change nothing
|
|
--no-chsh do not change the login shell
|
|
--no-plugins skip the zsh-users plugins (offline / slow mirrors)
|
|
--config-only print the bundled ~/.zshrc to stdout and exit
|
|
-v, --version print version
|
|
-h, --help this text
|
|
|
|
Environment:
|
|
GITHUB_MIRROR proxy prefix, e.g. https://ghfast.top/https://github.com
|
|
ZSH_THEME theme to use instead of robbyrussell
|
|
EOF
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# The bundled config. Single source of truth: `install.sh --config-only`
|
|
# regenerates the zshrc file kept in the repo next to this script.
|
|
# ---------------------------------------------------------------------------
|
|
emit_zshrc() {
|
|
cat <<'__ZSHRC__'
|
|
# ~/.zshrc
|
|
# managed-by: install.sh — re-run the installer to update this file.
|
|
# Machine-local additions belong in ~/.zshrc.local (never overwritten).
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Oh My Zsh
|
|
# ---------------------------------------------------------------------------
|
|
export ZSH="${ZSH:-$HOME/.oh-my-zsh}"
|
|
export ZSH_CUSTOM="${ZSH_CUSTOM:-$ZSH/custom}"
|
|
ZSH_THEME="${ZSH_THEME:-robbyrussell}"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# History
|
|
# ---------------------------------------------------------------------------
|
|
HISTFILE="$HOME/.zsh_history"
|
|
HISTSIZE=50000
|
|
SAVEHIST=50000
|
|
setopt EXTENDED_HISTORY # timestamps in the history file
|
|
setopt HIST_IGNORE_ALL_DUPS # no duplicate lines
|
|
setopt HIST_IGNORE_SPACE # commands starting with a space stay out
|
|
setopt HIST_REDUCE_BLANKS
|
|
setopt SHARE_HISTORY # several shells share one history
|
|
setopt APPEND_HISTORY
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Options & completion
|
|
# ---------------------------------------------------------------------------
|
|
setopt AUTO_CD # `..`, `~/x` without cd
|
|
setopt AUTO_PUSHD
|
|
setopt PUSHD_IGNORE_DUPS
|
|
setopt PUSHD_SILENT
|
|
setopt INTERACTIVE_COMMENTS
|
|
setopt NO_BEEP
|
|
setopt PROMPT_SUBST
|
|
|
|
zstyle ':completion:*' menu select
|
|
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'
|
|
[[ -n "${LS_COLORS:-}" ]] && zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Plugins (all bundled with Oh My Zsh unless installed by the script)
|
|
# ---------------------------------------------------------------------------
|
|
plugins=(git z extract sudo command-not-found)
|
|
|
|
[[ -d "$ZSH_CUSTOM/plugins/zsh-completions" ]] && plugins+=(zsh-completions)
|
|
[[ -d "$ZSH_CUSTOM/plugins/zsh-autosuggestions" ]] && plugins+=(zsh-autosuggestions)
|
|
|
|
if [[ -f "$ZSH/oh-my-zsh.sh" ]]; then
|
|
source "$ZSH/oh-my-zsh.sh"
|
|
else
|
|
# Oh My Zsh missing: stay usable instead of exploding.
|
|
PROMPT='%F{cyan}%n@%m%f %F{blue}%~%f %# '
|
|
autoload -Uz compinit && compinit -i
|
|
fi
|
|
|
|
# Syntax highlighting must be sourced last of the highlighters.
|
|
[[ -d "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" ]] &&
|
|
source "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Key bindings (emacs style + history substring search on arrows)
|
|
# ---------------------------------------------------------------------------
|
|
if [[ -o interactive ]]; then
|
|
bindkey -e
|
|
bindkey '^[[A' history-search-backward
|
|
bindkey '^[[B' history-search-forward
|
|
bindkey '^[[1;5C' forward-word # ctrl-right
|
|
bindkey '^[[1;5D' backward-word # ctrl-left
|
|
bindkey '^[[H' beginning-of-line # home
|
|
bindkey '^[[F' end-of-line # end
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Environment
|
|
# ---------------------------------------------------------------------------
|
|
export EDITOR="${EDITOR:-vim}"
|
|
export VISUAL="${VISUAL:-$EDITOR}"
|
|
export PAGER="${PAGER:-less}"
|
|
export LESS='-R -F -X -i -M'
|
|
|
|
typeset -U path
|
|
path=("$HOME/.local/bin" "$HOME/bin" $path)
|
|
export PATH
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Aliases
|
|
# ---------------------------------------------------------------------------
|
|
alias ll='ls -alFh'
|
|
alias la='ls -Ah'
|
|
alias l='ls -CFh'
|
|
alias ..='cd ..'
|
|
alias ...='cd ../..'
|
|
alias ....='cd ../../..'
|
|
alias reload='source ~/.zshrc'
|
|
alias zshconfig='$EDITOR ~/.zshrc'
|
|
alias szsh='$EDITOR ~/.zshrc.local'
|
|
|
|
alias g='git'
|
|
alias gs='git status -sb'
|
|
alias ga='git add'
|
|
alias gc='git commit -m'
|
|
alias gd='git diff'
|
|
alias gco='git checkout'
|
|
alias gb='git branch'
|
|
alias gl='git log --oneline --graph --decorate -20'
|
|
alias gp='git pull --rebase --autostash'
|
|
alias gP='git push'
|
|
|
|
alias df='df -h'
|
|
alias du='du -sh'
|
|
alias ports='ss -tulpn 2>/dev/null || netstat -tulpn'
|
|
alias myip='curl -fsS https://ifconfig.me; echo'
|
|
alias week='date +%V'
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Functions
|
|
# ---------------------------------------------------------------------------
|
|
# mkcd <dir> — create and enter a directory.
|
|
mkcd() {
|
|
[[ -n "${1:-}" ]] || { print -u2 'usage: mkcd <dir>'; return 1; }
|
|
mkdir -p -- "$1" && cd -- "$1"
|
|
}
|
|
|
|
# serve [port] — quick static HTTP server in the current directory.
|
|
serve() {
|
|
local port="${1:-8000}"
|
|
if command -v python3 >/dev/null 2>&1; then
|
|
print -r -- "http://0.0.0.0:$port (ctrl-c to stop)"
|
|
python3 -m http.server "$port"
|
|
elif command -v python >/dev/null 2>&1; then
|
|
print -r -- "http://0.0.0.0:$port (ctrl-c to stop)"
|
|
python -m SimpleHTTPServer "$port"
|
|
else
|
|
print -u2 'serve: python not found'
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# port <n> — what is listening on that port.
|
|
port() {
|
|
[[ -n "${1:-}" ]] || { print -u2 'usage: port <number>'; return 1; }
|
|
lsof -nP -iTCP:"$1" -sTCP:LISTEN 2>/dev/null || ss -tulpn 2>/dev/null | grep -F ":$1 "
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Machine-local overrides (the installer never touches this file)
|
|
# ---------------------------------------------------------------------------
|
|
[[ -f "$HOME/.zshrc.local" ]] && source "$HOME/.zshrc.local"
|
|
__ZSHRC__
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# arg parsing
|
|
# ---------------------------------------------------------------------------
|
|
while (($#)); do
|
|
case "$1" in
|
|
-y|--yes) ASSUME_YES=1 ;;
|
|
-n|--dry-run) DRY_RUN=1 ;;
|
|
--no-chsh) DO_CHSH=0 ;;
|
|
--no-plugins) DO_PLUGINS=0 ;;
|
|
--config-only) CONFIG_ONLY=1 ;;
|
|
-v|--version) printf '%s %s\n' install.sh "$SCRIPT_VERSION"; exit 0 ;;
|
|
-h|--help) usage; exit 0 ;;
|
|
*) die "unknown option: $1 (try --help)" ;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
if (( CONFIG_ONLY )); then
|
|
emit_zshrc
|
|
exit 0
|
|
fi
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# helpers
|
|
# ---------------------------------------------------------------------------
|
|
run() {
|
|
if (( DRY_RUN )); then
|
|
printf ' [dry-run]'; printf ' %q' "$@"; printf '\n'
|
|
return 0
|
|
fi
|
|
"$@"
|
|
}
|
|
|
|
confirm() {
|
|
local prompt="$1" reply
|
|
(( ASSUME_YES )) && return 0
|
|
if [[ -r /dev/tty ]]; then
|
|
printf '%s %s[Y/n]%s ' "$prompt" "$C_B" "$C_RST" >/dev/tty
|
|
read -r reply </dev/tty || reply=""
|
|
else
|
|
warn "no terminal for prompts — assuming yes: $prompt"
|
|
return 0
|
|
fi
|
|
[[ -z "$reply" || "$reply" == [Yy]* ]]
|
|
}
|
|
|
|
# sudo prefix; guarantees the array is never empty under `set -u`
|
|
SUDO=()
|
|
if (( EUID != 0 )); then
|
|
if command -v sudo >/dev/null 2>&1; then
|
|
SUDO=(sudo)
|
|
else
|
|
die "root privileges are needed to install packages, and sudo is not available — re-run as root"
|
|
fi
|
|
fi
|
|
|
|
as_root() {
|
|
if (( EUID == 0 )); then run "$@"; else run "${SUDO[@]}" "$@"; fi
|
|
}
|
|
|
|
PKG=""
|
|
detect_pkg() {
|
|
local p
|
|
for p in apt-get dnf yum pacman zypper apk brew; do
|
|
if command -v "$p" >/dev/null 2>&1; then PKG="$p"; return 0; fi
|
|
done
|
|
return 1
|
|
}
|
|
|
|
pkgs_install() {
|
|
(( $# )) || return 0
|
|
info "installing package(s): $*"
|
|
case "$PKG" in
|
|
apt-get)
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
as_root apt-get update -qq
|
|
as_root apt-get install -y -qq --no-install-recommends "$@" ;;
|
|
dnf) as_root dnf install -y "$@" ;;
|
|
yum) as_root yum install -y "$@" ;;
|
|
pacman) as_root pacman -Sy --needed --noconfirm "$@" ;;
|
|
zypper) as_root zypper --non-interactive install "$@" ;;
|
|
apk) as_root apk add --no-cache "$@" ;;
|
|
brew) run brew install "$@" ;;
|
|
*) die "no supported package manager found; install manually: $*" ;;
|
|
esac
|
|
}
|
|
|
|
clone_or_pull() { # url dest
|
|
local url="$1" dest="$2"
|
|
if [[ -d "$dest/.git" ]]; then
|
|
run git -C "$dest" pull --ff-only --quiet || warn "could not update $dest (offline?)"
|
|
elif [[ -e "$dest" ]]; then
|
|
warn "$dest exists but is not a git checkout — leaving it alone"
|
|
else
|
|
run git clone --depth=1 --quiet "$url" "$dest"
|
|
fi
|
|
}
|
|
|
|
write_zshrc() {
|
|
local target="$HOME/.zshrc" tmp
|
|
tmp=$(mktemp "${TMPDIR:-/tmp}/zshrc.XXXXXX")
|
|
|
|
emit_zshrc >"$tmp"
|
|
grep -qF 'oh-my-zsh.sh' "$tmp" || { rm -f "$tmp"; die "generated config is incomplete"; }
|
|
|
|
if [[ -f "$target" ]] && cmp -s "$tmp" "$target"; then
|
|
ok "~/.zshrc already up to date"
|
|
rm -f "$tmp"
|
|
return 0
|
|
fi
|
|
|
|
if (( DRY_RUN )); then
|
|
if [[ -e "$target" || -L "$target" ]]; then
|
|
if grep -qF "$MARKER" "$target" 2>/dev/null; then
|
|
printf ' [dry-run] would replace a previously generated %s\n' "$target"
|
|
else
|
|
printf ' [dry-run] would back up %s -> %s.backup.<timestamp>\n' "$target" "$target"
|
|
fi
|
|
fi
|
|
printf ' [dry-run] would write %s (%s bytes)\n' "$target" "$(wc -c <"$tmp" | tr -d ' ')"
|
|
rm -f "$tmp"
|
|
return 0
|
|
fi
|
|
|
|
if [[ -e "$target" || -L "$target" ]]; then
|
|
if grep -qF "$MARKER" "$target" 2>/dev/null; then
|
|
info "replacing a previously generated ~/.zshrc"
|
|
else
|
|
ZSHRC_BACKUP="$target.backup.$(date +%Y%m%d-%H%M%S)"
|
|
cp -a "$target" "$ZSHRC_BACKUP"
|
|
ok "backed up your ~/.zshrc -> $ZSHRC_BACKUP"
|
|
fi
|
|
fi
|
|
|
|
install -m 0644 "$tmp" "$target"
|
|
rm -f "$tmp"
|
|
ok "wrote $target"
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 1. zsh, git, curl
|
|
# ---------------------------------------------------------------------------
|
|
install_dependencies() {
|
|
info "checking dependencies"
|
|
detect_pkg || true
|
|
|
|
local missing=() p
|
|
for p in zsh git curl; do
|
|
command -v "$p" >/dev/null 2>&1 || missing+=("$p")
|
|
done
|
|
|
|
if (( ${#missing[@]} == 0 )); then
|
|
ok "zsh, git and curl are already installed"
|
|
return 0
|
|
fi
|
|
|
|
[[ -n "$PKG" ]] || die "${missing[*]} missing and no supported package manager found"
|
|
|
|
if [[ "$PKG" == brew ]] || (( EUID == 0 )) || [[ -n "${SUDO[0]:-}" ]]; then
|
|
pkgs_install "${missing[@]}"
|
|
else
|
|
die "cannot install ${missing[*]} without sudo or root"
|
|
fi
|
|
|
|
for p in zsh git; do
|
|
command -v "$p" >/dev/null 2>&1 || (( DRY_RUN )) || die "$p still missing after install"
|
|
done
|
|
ok "dependencies ready"
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 2. Oh My Zsh
|
|
# ---------------------------------------------------------------------------
|
|
install_omz() {
|
|
local omz="$HOME/.oh-my-zsh"
|
|
info "Oh My Zsh at $omz"
|
|
if [[ -d "$omz/.git" ]]; then
|
|
run git -C "$omz" pull --ff-only --quiet || warn "could not update Oh My Zsh (offline?)"
|
|
ok "Oh My Zsh present, updated"
|
|
elif [[ -d "$omz" ]]; then
|
|
warn "$omz exists but is not a git checkout — keeping it as is"
|
|
else
|
|
run git clone --depth=1 --quiet "$GITHUB/ohmyzsh/ohmyzsh.git" "$omz"
|
|
ok "Oh My Zsh cloned"
|
|
fi
|
|
}
|
|
|
|
install_plugins() {
|
|
(( DO_PLUGINS )) || { info "skipping plugins (--no-plugins)"; return 0; }
|
|
local dest="$HOME/.oh-my-zsh/custom/plugins"
|
|
info "zsh-users plugins"
|
|
run mkdir -p "$dest"
|
|
clone_or_pull "$GITHUB/zsh-users/zsh-autosuggestions.git" "$dest/zsh-autosuggestions"
|
|
clone_or_pull "$GITHUB/zsh-users/zsh-syntax-highlighting.git" "$dest/zsh-syntax-highlighting"
|
|
clone_or_pull "$GITHUB/zsh-users/zsh-completions.git" "$dest/zsh-completions"
|
|
ok "plugins ready"
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# 3. login shell
|
|
# ---------------------------------------------------------------------------
|
|
set_login_shell() {
|
|
(( DO_CHSH )) || { info "skipping login shell change (--no-chsh)"; return 0; }
|
|
local zshbin
|
|
zshbin=$(command -v zsh 2>/dev/null || true)
|
|
[[ -n "$zshbin" ]] || (( DRY_RUN )) || die "zsh binary not found"
|
|
|
|
if [[ "${SHELL:-}" == "$zshbin" ]]; then
|
|
ok "login shell is already $zshbin"
|
|
return 0
|
|
fi
|
|
|
|
if [[ -r /etc/shells ]] && ! grep -qxF "$zshbin" /etc/shells; then
|
|
info "adding $zshbin to /etc/shells"
|
|
if (( DRY_RUN )); then
|
|
printf ' [dry-run] would append %s to /etc/shells\n' "$zshbin"
|
|
else
|
|
printf '%s\n' "$zshbin" | as_root tee -a /etc/shells >/dev/null
|
|
fi
|
|
fi
|
|
|
|
if (( DRY_RUN )); then
|
|
printf ' [dry-run] would run: chsh -s %s %s\n' "$zshbin" "${USER:-$(id -un)}"
|
|
return 0
|
|
fi
|
|
|
|
if ! as_root chsh -s "$zshbin" "${USER:-$(id -un)}" 2>/dev/null; then
|
|
warn "could not change the login shell automatically"
|
|
warn "do it yourself with: chsh -s $zshbin"
|
|
else
|
|
ok "login shell set to $zshbin"
|
|
fi
|
|
}
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# main
|
|
# ---------------------------------------------------------------------------
|
|
main() {
|
|
printf '%s\n' "${C_B}zsh + Oh My Zsh installer ${SCRIPT_VERSION}${C_RST}"
|
|
printf ' config source: %s\n' "$DOTFILES_URL"
|
|
(( DRY_RUN )) && printf ' %s(dry run — nothing will be changed)%s\n' "$C_Y" "$C_RST"
|
|
printf '\n'
|
|
|
|
if (( EUID == 0 )); then
|
|
warn "running as root — dotfiles will be written to root's home ($HOME)"
|
|
fi
|
|
|
|
confirm "Install zsh, Oh My Zsh and the bundled ~/.zshrc?" || { info "aborted, nothing changed"; exit 0; }
|
|
|
|
install_dependencies
|
|
install_omz
|
|
install_plugins
|
|
write_zshrc
|
|
set_login_shell
|
|
|
|
printf '\n%s done %s\n' "$C_G" "$C_RST"
|
|
ok "restart your terminal, or run: exec zsh"
|
|
[[ -n "$ZSHRC_BACKUP" ]] && info "your previous config is kept at $ZSHRC_BACKUP"
|
|
info "put machine-specific settings in ~/.zshrc.local"
|
|
}
|
|
|
|
main "$@"
|