#!/bin/bash set -euo pipefail # ============================= # COLORS & LOGGING # ============================= RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' log() { echo -e "${BLUE}[INFO]${NC} $*"; } ok() { echo -e "${GREEN}[OK]${NC} $*"; } warn() { echo -e "${YELLOW}[WARN]${NC} $*"; } err() { echo -e "${RED}[ERROR]${NC} $*" >&2; } # ============================= # CONFIG # ============================= GIST_RAW_BASE="https://gist.githubusercontent.com/weehong/c430fefc6e90428dfe6811e0766decf4/raw" CONFIG_FILES=( ".alias" ".func" ".pathrc" ".sourcerc" ".vimrc" ".zshrc" ) # ============================= # REQUIREMENTS # ============================= check_requirements() { if [[ $EUID -eq 0 ]]; then err "Do not run as root on macOS" exit 1 fi } # ============================= # INSTALLATION FUNCTIONS # ============================= configure_git() { log "Configuring Git..." read -p "Git name (leave empty to skip): " name read -p "Git email (leave empty to skip): " email if [[ -n "$name" ]]; then git config --global user.name "$name" fi if [[ -n "$email" ]]; then git config --global user.email "$email" fi ok "Git configured" } install_oh_my_zsh() { if [[ -d "$HOME/.oh-my-zsh" ]]; then ok "Oh My Zsh already installed" return fi log "Installing Oh My Zsh..." RUNZSH=no CHSH=no KEEP_ZSHRC=yes \ sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" ok "Oh My Zsh installed" } install_plugins() { log "Installing plugins..." local dir="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/plugins" mkdir -p "$dir" [[ -d "$dir/zsh-autosuggestions" ]] || \ git clone https://github.com/zsh-users/zsh-autosuggestions "$dir/zsh-autosuggestions" [[ -d "$dir/zsh-syntax-highlighting" ]] || \ git clone https://github.com/zsh-users/zsh-syntax-highlighting "$dir/zsh-syntax-highlighting" ok "Plugins installed" } install_theme() { log "Installing Spaceship theme..." local themes="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes" local dir="$themes/spaceship-prompt" mkdir -p "$themes" [[ -d "$dir" ]] || \ git clone https://github.com/spaceship-prompt/spaceship-prompt.git "$dir" --depth=1 ln -sf "$dir/spaceship.zsh-theme" "$themes/spaceship.zsh-theme" ok "Theme installed" } download_configs() { log "Downloading custom config files..." local backup="$HOME/.config_backup_$(date +%Y%m%d_%H%M%S)" mkdir -p "$backup" for f in "${CONFIG_FILES[@]}"; do local url="$GIST_RAW_BASE/$f" local target="$HOME/$f" local tmp="${target}.tmp.$$" if [[ -f "$target" ]]; then cp "$target" "$backup/" fi log "Fetching $f ..." if curl -fsSL "$url" -o "$tmp"; then mv "$tmp" "$target" else rm -f "$tmp" warn "Failed to download $f" fi done ok "Configs downloaded (Backup at $backup)" } update_zshrc() { local zshrc="$HOME/.zshrc" log "Checking .zshrc..." if [[ ! -f "$zshrc" ]]; then warn ".zshrc not found. Run option 5 first." return 1 fi ok ".zshrc already comes from the managed gist." ok "Load order is built in: .sourcerc -> .func -> .pathrc -> .alias" } switch_shell() { log "Starting Zsh session..." echo -e "${YELLOW}Type 'exit' to return to this installer menu.${NC}" echo "----------------------------------------" zsh -l echo "----------------------------------------" ok "Returned from Zsh session" } # ============================= # INTERACTIVE MENU # ============================= show_menu() { echo "===========================================" echo "macOS Zsh Setup - Choose what to do" echo "===========================================" echo " 0) Run ALL steps (1-7)" echo " 1) Configure Git" echo " 2) Install Oh My Zsh" echo " 3) Install plugins (autosuggestions, syntax highlighting)" echo " 4) Install Spaceship theme" echo " 5) Download custom configs (~/.alias, .func, .vimrc, etc.)" echo " 6) Check ~/.zshrc load order" echo " 7) Switch to Zsh (Temporary Sub-shell)" echo " 8) Quit" echo "===========================================" } run_choices() { local input read -p "Select: " input input="${input//,/ }" local -a to_run=() local -a to_exclude=() for item in $input; do if [[ "$item" == !* ]]; then to_exclude+=("${item:1}") elif [[ "$item" == "0" ]]; then to_run+=(1 2 3 4 5 6 7) else to_run+=("$item") fi done if [[ ${#to_run[@]} -gt 0 ]]; then for choice in "${to_run[@]}"; do local skip=false if [[ ${#to_exclude[@]} -gt 0 ]]; then for ex in "${to_exclude[@]}"; do if [[ "$choice" == "$ex" ]]; then skip=true break fi done fi $skip && continue case "$choice" in 1) configure_git ;; 2) install_oh_my_zsh ;; 3) install_plugins ;; 4) install_theme ;; 5) download_configs ;; 6) update_zshrc ;; 7) switch_shell ;; 8) exit 0 ;; *) warn "Skipping invalid option: $choice" ;; esac echo done fi } # ============================= # MAIN # ============================= main() { check_requirements while true; do show_menu run_choices read -p "Do you want to run more options? (y/n): " again [[ "$again" =~ ^[Yy]$ ]] || break done ok "macOS configuration complete!" } main "$@"