name: zsh_setup
id: org.themobileprof.zsh_setup
version: 1.0.0
description: Install and configure Zsh with Oh My Zsh, Agnoster theme, powerline fonts, and essential plugins
tags: [installer, development, devops-ops, composite, zsh, shell]
provides:
  - zsh_installed
  - ohmyzsh_installed
requires:
  - check_command_exists
  - vim_dev_setup
  - git_setup
size_kb: 20

flows:
  main:
    start: detect_environment
    steps:
      detect_environment:
        type: action
        message: "Detecting environment..."
        command: |
          if [ -n "$TERMUX_VERSION" ] || [ -d "$PREFIX" ]; then
            echo "TERMUX"
          else
            echo "LINUX"
          fi
        next: install_zsh

      install_zsh:
        type: action
        message: "Installing Zsh shell..."
        command: |
          if command -v zsh >/dev/null 2>&1; then
            echo "✅ Zsh already installed: $(zsh --version)"
          else
            echo "Installing Zsh..."
            if [ -n "$TERMUX_VERSION" ] || [ -d "$PREFIX" ]; then
              pkg install -y zsh
            elif command -v apt-get >/dev/null 2>&1; then
              sudo apt-get update && sudo apt-get install -y zsh
            elif command -v yum >/dev/null 2>&1; then
              sudo yum install -y zsh
            elif command -v dnf >/dev/null 2>&1; then
              sudo dnf install -y zsh
            elif command -v pacman >/dev/null 2>&1; then
              sudo pacman -S --noconfirm zsh
            elif command -v brew >/dev/null 2>&1; then
              brew install zsh
            else
              echo "❌ No supported package manager found"
              exit 1
            fi
            echo "✅ Zsh installed: $(zsh --version)"
          fi
        next: install_powerline

      install_powerline:
        type: action
        message: "Installing powerline fonts..."
        command: |
          if [ -n "$TERMUX_VERSION" ] || [ -d "$PREFIX" ]; then
            if ! pkg list-installed 2>/dev/null | grep -q "powerline"; then
              echo "Installing powerline for Termux..."
              pkg install -y powerline
            fi
            echo "✅ Powerline fonts ready for Termux"
          else
            if command -v apt-get >/dev/null 2>&1; then
              if ! dpkg -l | grep -q fonts-powerline; then
                echo "Installing powerline fonts..."
                sudo apt-get install -y fonts-powerline 2>/dev/null || {
                  git clone --depth=1 https://github.com/powerline/fonts.git /tmp/powerline-fonts
                  cd /tmp/powerline-fonts && ./install.sh
                  cd - > /dev/null
                  rm -rf /tmp/powerline-fonts
                }
              fi
            else
              if [ ! -d "$HOME/.local/share/fonts" ]; then
                echo "Installing fonts manually..."
                git clone --depth=1 https://github.com/powerline/fonts.git /tmp/powerline-fonts
                cd /tmp/powerline-fonts && ./install.sh
                cd - > /dev/null
                rm -rf /tmp/powerline-fonts
              fi
            fi
            echo "✅ Powerline fonts installed"
          fi
        next: install_ohmyzsh

      install_ohmyzsh:
        type: action
        message: "Installing Oh My Zsh..."
        command: |
          if [ -d "$HOME/.oh-my-zsh" ]; then
            echo "✅ Oh My Zsh already installed"
          else
            echo "Installing Oh My Zsh..."
            sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
            
            if [ -d "$HOME/.oh-my-zsh" ]; then
              echo "✅ Oh My Zsh installed successfully"
            else
              echo "❌ Oh My Zsh installation failed"
              exit 1
            fi
          fi
        next: install_plugins

      install_plugins:
        type: action
        message: "Installing Zsh plugins..."
        command: |
          ZSH_CUSTOM="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}"
          
          if [ ! -d "$ZSH_CUSTOM/plugins/zsh-autosuggestions" ]; then
            echo "Installing zsh-autosuggestions..."
            git clone https://github.com/zsh-users/zsh-autosuggestions "$ZSH_CUSTOM/plugins/zsh-autosuggestions"
            echo "✅ zsh-autosuggestions installed"
          else
            echo "✅ zsh-autosuggestions already installed"
          fi
          
          if [ ! -d "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" ]; then
            echo "Installing zsh-syntax-highlighting..."
            git clone https://github.com/zsh-users/zsh-syntax-highlighting "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting"
            echo "✅ zsh-syntax-highlighting installed"
          else
            echo "✅ zsh-syntax-highlighting already installed"
          fi
          
          if [ ! -d "$ZSH_CUSTOM/plugins/zsh-completions" ]; then
            echo "Installing zsh-completions..."
            git clone https://github.com/zsh-users/zsh-completions "$ZSH_CUSTOM/plugins/zsh-completions"
            echo "✅ zsh-completions installed"
          else
            echo "✅ zsh-completions already installed"
          fi
        next: configure_zshrc

      configure_zshrc:
        type: action
        message: "Configuring .zshrc..."
        command: |
          if [ -f "$HOME/.zshrc" ]; then
            cp "$HOME/.zshrc" "$HOME/.zshrc.backup.$(date +%Y%m%d%H%M%S)"
            echo "✅ Backed up existing .zshrc"
          fi
          
          cat > "$HOME/.zshrc" << 'ZSHRC_END'
          # Path to oh-my-zsh installation
          export ZSH="$HOME/.oh-my-zsh"

          # Theme - Agnoster with powerline symbols
          ZSH_THEME="agnoster"

          # Enable command auto-correction
          ENABLE_CORRECTION="true"

          # Display red dots whilst waiting for completion
          COMPLETION_WAITING_DOTS="true"

          # Command execution time stamp shown in history
          HIST_STAMPS="yyyy-mm-dd"

          # Plugins to load
          plugins=(
            git docker golang python node npm pip sudo
            command-not-found colored-man-pages colorize
            cp extract history jsontools web-search z
            zsh-autosuggestions zsh-syntax-highlighting zsh-completions
          )

          # Load Oh My Zsh
          source $ZSH/oh-my-zsh.sh

          # User configuration
          export EDITOR='vim'
          export VISUAL='vim'
          export LANG=en_US.UTF-8
          export LC_ALL=en_US.UTF-8

          # History configuration
          export HISTSIZE=10000
          export SAVEHIST=10000
          export HISTFILE=~/.zsh_history
          setopt HIST_IGNORE_ALL_DUPS
          setopt HIST_FIND_NO_DUPS
          setopt HIST_SAVE_NO_DUPS
          setopt SHARE_HISTORY

          # Custom aliases
          alias zshconfig="$EDITOR ~/.zshrc"
          alias ll='ls -lah'
          alias la='ls -A'
          alias l='ls -CF'
          alias ..='cd ..'
          alias ...='cd ../..'
          alias ....='cd ../../..'
          alias grep='grep --color=auto'
          alias df='df -h'
          alias free='free -h'
          alias mkdir='mkdir -pv'
          alias wget='wget -c'
          alias myip='curl -s https://ipinfo.io/ip'

          # Git aliases
          alias gs='git status'
          alias ga='git add'
          alias gc='git commit'
          alias gp='git push'
          alias gl='git log --oneline --graph --decorate'
          alias gd='git diff'
          alias gco='git checkout'
          alias gb='git branch'

          # Development shortcuts
          alias py='python3'
          alias serve='python3 -m http.server'
          alias json='python3 -m json.tool'

          # Safe operations
          alias rm='rm -i'
          alias cp='cp -i'
          alias mv='mv -i'

          # Functions
          function extract() {
            if [ -f "$1" ]; then
              case "$1" in
                *.tar.bz2) tar xjf "$1" ;;
                *.tar.gz) tar xzf "$1" ;;
                *.bz2) bunzip2 "$1" ;;
                *.rar) unrar x "$1" ;;
                *.gz) gunzip "$1" ;;
                *.tar) tar xf "$1" ;;
                *.tbz2) tar xjf "$1" ;;
                *.tgz) tar xzf "$1" ;;
                *.zip) unzip "$1" ;;
                *.Z) uncompress "$1" ;;
                *.7z) 7z x "$1" ;;
                *) echo "'$1' cannot be extracted via extract()" ;;
              esac
            else
              echo "'$1' is not a valid file"
            fi
          }

          function mkcd() {
            mkdir -p "$1" && cd "$1"
          }

          # Load clipilot if available
          if [ -f "$HOME/.clipilot/clipilot.sh" ]; then
            source "$HOME/.clipilot/clipilot.sh"
          fi
          ZSHRC_END
          
          if [ -n "$TERMUX_VERSION" ] || [ -d "$PREFIX" ]; then
            cat >> "$HOME/.zshrc" << 'TERMUX_END'

          # Termux-specific configuration
          alias update='pkg update && pkg upgrade'
          alias cleanup='pkg autoclean && pkg clean'

          # Termux API aliases (if available)
          if command -v termux-clipboard-get >/dev/null 2>&1; then
            alias pbcopy='termux-clipboard-set'
            alias pbpaste='termux-clipboard-get'
            alias notify='termux-notification'
            alias battery='termux-battery-status'
          fi
          TERMUX_END
          fi
          
          echo "✅ .zshrc configured with Agnoster theme and plugins"
        next: customize_theme

      customize_theme:
        type: action
        message: "Customizing Agnoster theme..."
        command: |
          ZSH_CUSTOM="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}"
          mkdir -p "$ZSH_CUSTOM"
          
          cat > "$ZSH_CUSTOM/agnoster-custom.zsh" << 'AGNOSTER_END'
          # Agnoster theme customizations
          
          # Shorten directory path in prompt
          prompt_dir() {
            prompt_segment blue $CURRENT_FG '%2~'
          }
          
          # Add newline before prompt
          PROMPT='
          '$PROMPT
          
          # Fix for wide characters and emoji support
          setopt COMBINING_CHARS
          AGNOSTER_END
          
          echo "✅ Agnoster theme customized"
        next: set_default_shell

      set_default_shell:
        type: action
        message: "Setting Zsh as default shell..."
        command: |
          CURRENT_SHELL=$(basename "$SHELL")
          
          if [ "$CURRENT_SHELL" = "zsh" ]; then
            echo "✅ Zsh is already the default shell"
          else
            ZSH_PATH=$(which zsh)
            
            if [ -n "$TERMUX_VERSION" ] || [ -d "$PREFIX" ]; then
              echo "Setting Zsh as default shell in Termux..."
              chsh -s "$ZSH_PATH" 2>/dev/null || {
                echo "⚠  Add to ~/.bash_profile:"
                echo "   if [ -x $ZSH_PATH ]; then exec $ZSH_PATH; fi"
              }
            else
              if grep -q "^$ZSH_PATH$" /etc/shells; then
                sudo chsh -s "$ZSH_PATH" "$USER" || {
                  echo "⚠  Run manually: chsh -s $ZSH_PATH"
                }
              else
                echo "$ZSH_PATH" | sudo tee -a /etc/shells >/dev/null
                sudo chsh -s "$ZSH_PATH" "$USER" || {
                  echo "⚠  Run manually: chsh -s $ZSH_PATH"
                }
              fi
            fi
            
            echo "✅ Zsh configured as default shell"
            echo "   Log out and log back in for changes to take effect"
          fi
        next: show_summary

      show_summary:
        type: terminal
        message: |
          
          ==========================================
            Zsh Setup Complete!
          ==========================================
          
          Installed components:
            ✅ Zsh: $(zsh --version)
            ✅ Oh My Zsh framework
            ✅ Agnoster theme with powerline fonts
            ✅ zsh-autosuggestions plugin
            ✅ zsh-syntax-highlighting plugin
            ✅ zsh-completions plugin
            ✅ 20+ useful Oh My Zsh plugins
          
          Features enabled:
            • Powerline-enabled Agnoster theme
            • Auto-suggestions as you type
            • Syntax highlighting
            • Git integration and aliases
            • Intelligent command completion
            • Directory history (z plugin)
            • Colored man pages
            • 50+ useful aliases
          
          Quick start:
            • Start Zsh now: exec zsh
            • Edit config: zshconfig
            • Update Oh My Zsh: omz update
            • View aliases: alias
          
          Next steps:
            1. Start Zsh: exec zsh
            2. Ensure terminal supports UTF-8 and powerline fonts
            3. Explore aliases with 'alias' command
            4. Try auto-suggestions by typing and using → arrow key

metadata:
  author: TheMobileProf
  license: MIT
