#!/bin/sh

# Exit immediately if a command exits with a non-zero status
set -e

# ==========================================
# Initialization & Banner
# ==========================================
printf "=================================================================\n"
printf "Starting Alpine Linux environment setup...\n"
printf "Basic tools will be installed first. Press Ctrl+C at any time to quit.\n"
printf "=================================================================\n\n"

# ==========================================
# Package Installation (Basic)
# ==========================================
echo "--> Installing core packages (nano, zsh, git, etc.)..."
apk add nano zsh oh-my-zsh curl git shadow

# Change default shell to zsh (takes effect on next login)
chsh -s /bin/zsh

# ==========================================
# Zsh Configuration (/etc/zsh/zshrc)
# ==========================================
echo "--> Writing Zsh configuration..."

# Create file or append to it in one single block
cat << 'EOF' >> /etc/zsh/zshrc

# --- Aliases ---
alias e='nano'

# --- Prompt Configuration ---
PROMPT="%n@%m %~ %# "

EOF

# ==========================================
# Optional Package Installation (Docker)
# ==========================================
printf "\nDo you want to install Docker? (y/n): "
read -r response

case "$response" in
    [yY][eE][sS]|[yY]) 
        echo "--> Installing and configuring Docker..."
        apk add docker docker-compose
        rc-update add docker boot
        rc-service docker start || service docker start
        ;;
    *)
        echo "--> Skipping Docker installation."
        ;;
esac

# ==========================================
# Environment Handover
# ==========================================
echo "\n================================================================="
echo "Setup complete! Switching your current session to Zsh..."
echo "================================================================="

# Replaces current sh script process with Zsh to cleanly load the new config
exec zsh -l