#!/bin/bash
# Deploy MkDocs with Gitea integration

set -e
echo "=== MkDocs Deployment ==="
date

# Install Python and MkDocs
apt-get update
apt-get install -y python3-pip python3-venv git

# Create MkDocs directory
mkdir -p /var/www/mkdocs
cd /var/www/mkdocs

# Create Python virtual environment
python3 -m venv venv
source venv/bin/activate

# Install MkDocs and Material theme
pip install mkdocs mkdocs-material pymdown-extensions

# Clone the Gitea repo
git clone https://git.firefrostgaming.com/firefrost-gaming/firefrost-documentation.git site
cd site

# Create MkDocs configuration
cat > mkdocs.yml << 'MKDOCSEOF'
site_name: Firefrost Gaming Documentation
site_url: https://docs.firefrostgaming.com
site_description: Official documentation for Firefrost Gaming Network
site_author: Firefrost Gaming Team

theme:
  name: material
  palette:
    - scheme: slate
      primary: blue
      accent: cyan
  features:
    - navigation.tabs
    - navigation.sections
    - navigation.expand
    - search.suggest
    - search.highlight

markdown_extensions:
  - pymdownx.highlight
  - pymdownx.superfences
  - pymdownx.tabbed
  - admonition
  - toc:
      permalink: true

nav:
  - Home: index.md
  - Getting Started: getting-started.md
  - Server Rules: rules.md
  - Modpacks: modpacks.md

MKDOCSEOF

# Create initial content structure
mkdir -p docs
cat > docs/index.md << 'INDEXEOF'
# Welcome to Firefrost Gaming

**Fire + Frost = Where Passion Meets Precision** 🔥❄️

This is the official documentation for Firefrost Gaming Network.

## Quick Links

- [Getting Started](getting-started.md)
- [Server Rules](rules.md)
- [Available Modpacks](modpacks.md)

## About Us

Firefrost Gaming is a subscription-based Minecraft server network offering:

- **Fire Path**: Competitive PvP gameplay
- **Frost Path**: Collaborative building and exploration

---

*Documentation last updated: $(date)*
INDEXEOF

cat > docs/getting-started.md << 'STARTEOF'
# Getting Started

Welcome to Firefrost Gaming! This guide will help you get started.

## How to Join

1. Subscribe at [firefrostgaming.com](https://firefrostgaming.com)
2. Download the modpack
3. Connect to the server

## Choosing Your Path

### Fire Path 🔥
Competitive gameplay with PvP elements

### Frost Path ❄️
Collaborative building and exploration

STARTEOF

cat > docs/rules.md << 'RULESEOF'
# Server Rules

## General Rules

1. Be respectful to all players
2. No griefing
3. No cheating or exploits

## Moderation

Our team enforces these rules to ensure a positive experience for everyone.

RULESEOF

cat > docs/modpacks.md << 'MODPACKSEOF'
# Available Modpacks

## Active Servers

### All The Mods 10
- Location: NC1 Charlotte
- IP: 216.239.104.134:25565

### Stoneblock 4
- Location: TX1 Dallas
- IP: 38.68.14.26:25565

*More servers coming soon!*
MODPACKSEOF

# Build the site
mkdocs build

# Set permissions
chown -R www-data:www-data /var/www/mkdocs

# Create Nginx configuration
cat > /etc/nginx/sites-available/mkdocs << 'NGINXEOF'
server {
    listen 80;
    listen [::]:80;
    server_name docs.firefrostgaming.com;

    root /var/www/mkdocs/site/site;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    # Cache static assets
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 7d;
        add_header Cache-Control "public, immutable";
    }
}
NGINXEOF

# Enable site
ln -sf /etc/nginx/sites-available/mkdocs /etc/nginx/sites-enabled/

# Test and reload Nginx
nginx -t
systemctl reload nginx

# Get SSL certificate
apt-get install -y certbot python3-certbot-nginx
certbot --nginx -d docs.firefrostgaming.com --non-interactive --agree-tos --email admin@firefrostgaming.com --redirect

# Create rebuild script for Git updates
cat > /usr/local/bin/rebuild-docs.sh << 'REBUILDEOF'
#!/bin/bash
cd /var/www/mkdocs/site
git pull origin main
source /var/www/mkdocs/venv/bin/activate
mkdocs build
echo "Documentation rebuilt: $(date)" >> /var/log/mkdocs-rebuild.log
REBUILDEOF

chmod +x /usr/local/bin/rebuild-docs.sh

# Create webhook listener (simple version)
cat > /usr/local/bin/gitea-webhook-listener.sh << 'WEBHOOKEOF'
#!/bin/bash
# Simple webhook listener on port 9000

while true; do
    echo "Listening for webhook..." | nc -l -p 9000 > /dev/null
    /usr/local/bin/rebuild-docs.sh
    sleep 2
done
WEBHOOKEOF

chmod +x /usr/local/bin/gitea-webhook-listener.sh

echo ""
echo "=== MkDocs Deployed ==="
echo "Site: https://docs.firefrostgaming.com"
echo "Git Repo: https://git.firefrostgaming.com/firefrost-gaming/firefrost-documentation"
echo "Rebuild: /usr/local/bin/rebuild-docs.sh"
echo ""
echo "Next: Configure Gitea webhook to http://38.68.14.26:9000"
