# File Analysis Container - ZSH Configuration with Oh My Zsh

# Skip interactive first-time setup
ZSH_DISABLE_COMPFIX=true

# If running as root or system user without home, create minimal config
if [[ ! -d "$HOME" ]] || [[ ! -w "$HOME" ]]; then
    # Minimal non-OMZ setup for system users
    HISTFILE=/tmp/.zsh_history_$$
    HISTSIZE=10000
    SAVEHIST=10000
    
    autoload -Uz compinit && compinit -d /tmp/.zcompdump_$$
    autoload -U colors && colors
    
    PROMPT='%F{red}[🔍]%f %F{cyan}%~%f $ '
    
    # Load plugins if available
    [[ -f /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh ]] && \
        source /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh
    [[ -f /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ]] && \
        source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
else
    # Oh My Zsh setup for regular users
    export ZSH="$HOME/.oh-my-zsh"
    
    # Install Oh My Zsh if not present
    if [[ ! -d "$ZSH" ]]; then
        echo "Installing Oh My Zsh..."
        RUNZSH=no CHSH=no sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" || {
            # Fallback if curl fails
            RUNZSH=no CHSH=no sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" 2>/dev/null
        }
    fi
    
    # Oh My Zsh theme - using agnoster-like theme for security work
    ZSH_THEME="robbyrussell"
    
    # Custom theme for file analysis work
    if [[ -d "$ZSH" ]]; then
        # Plugins to load
        plugins=(git docker command-not-found colored-man-pages)
        
        # Load Oh My Zsh
        source $ZSH/oh-my-zsh.sh 2>/dev/null || true
        
        # Custom prompt with analysis indicator
        PROMPT='%F{red}🔍%f %F{cyan}%~%f $(git_prompt_info)%# '
        RPROMPT='%F{yellow}%*%f'
    else
        # Fallback if OMZ installation failed
        autoload -U colors && colors
        PROMPT='%F{red}[🔍]%f %F{cyan}%~%f $ '
    fi
fi

# History configuration
HISTFILE=${HISTFILE:-~/.zsh_history}
HISTSIZE=10000
SAVEHIST=10000
setopt SHARE_HISTORY
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_FIND_NO_DUPS
setopt HIST_REDUCE_BLANKS
setopt HIST_VERIFY

# Navigation
setopt AUTO_CD
setopt AUTO_PUSHD
setopt PUSHD_IGNORE_DUPS
setopt PUSHD_SILENT

# Completion
autoload -Uz compinit
if [[ -w "$HOME" ]]; then
    compinit
else
    compinit -d /tmp/.zcompdump_$$
fi

zstyle ':completion:*' menu select
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'
setopt COMPLETE_IN_WORD
setopt AUTO_MENU

# Load system plugins if available
[[ -f /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh ]] && \
    source /usr/share/zsh-autosuggestions/zsh-autosuggestions.zsh
[[ -f /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh ]] && \
    source /usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

# Aliases for file analysis
alias ls='ls --color=auto'
alias ll='ls -lah'
alias la='ls -A'
alias l='ls -CF'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'

# Tool aliases
alias fd='fdfind'
alias bat='batcat'  # Ubuntu names it batcat
alias rg='rg --color=auto'
alias analyse='fhelp'
alias help='fhelp'

# Help alias (? needs special handling in zsh)
if [[ -n "$ZSH_VERSION" ]]; then
    alias \?='fhelp'
else
    alias '?'='fhelp'
fi

# Quick navigation
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'

# Analysis shortcuts
alias pdfinfo='pdfid.py'
alias pdfparse='pdf-parser.py'
alias scanpdf='pdf-parser.py'

# Git aliases (if git plugin not loaded)
if ! type git &>/dev/null || ! alias gs &>/dev/null; then
    alias gs='git status'
    alias ga='git add'
    alias gc='git commit'
    alias gp='git push'
    alias gl='git log --oneline'
fi

# Environment
export EDITOR=vim
export VISUAL=vim

# Welcome message (only on interactive shells)
if [[ -o interactive ]] && [[ -f /opt/README ]]; then
    # Only show welcome once per session
    if [[ -z "$_WELCOME_SHOWN" ]]; then
        echo ""
        echo "\033[1;36m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
        echo "\033[1;31m  File Analysis Container\033[0m \033[1;33m(zsh with Oh My Zsh)\033[0m"
        echo "\033[1;36m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\033[0m"
        echo ""
        echo "  \033[1;32mfhelp\033[0m or \033[1;32m?\033[0m          - Help system"
        echo "  \033[1;32mfhelp cheat <tool>\033[0m - Quick examples"
        echo "  \033[1;32mfhelp tools pdf\033[0m    - Find PDF tools"
        echo ""
        echo "  Shells: \033[0;36mbash\033[0m (default), \033[0;36mzsh\033[0m (current), \033[0;36mfish\033[0m"
        echo ""
        export _WELCOME_SHOWN=1
    fi
fi
