#!/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 < — create and enter a directory. mkcd() { [[ -n "${1:-}" ]] || { print -u2 'usage: mkcd '; 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 — what is listening on that port. port() { [[ -n "${1:-}" ]] || { print -u2 'usage: port '; 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/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.\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 "$@"