#!/bin/bash

# Simple DokuWiki Synchronization Script

echo "📚 Creating DokuWiki structure..."

# Create local DokuWiki structure
rm -rf /tmp/dokuwiki_sync
mkdir -p /tmp/dokuwiki_sync/homelab

# Function to convert markdown to DokuWiki format
convert_md_to_dokuwiki() {
    local input_file="$1"
    local output_file="$2"

    # Basic markdown to DokuWiki conversion
    sed -e 's/^# /====== /g' \
        -e 's/^## /===== /g' \
        -e 's/^### /==== /g' \
        -e 's/^#### /=== /g' \
        -e 's/^##### /== /g' \
        -e 's/^###### /= /g' \
        -e 's/====== \(.*\)/====== \1 ======/g' \
        -e 's/===== \(.*\)/===== \1 =====/g' \
        -e 's/==== \(.*\)/==== \1 ====/g' \
        -e 's/=== \(.*\)/=== \1 ===/g' \
        -e 's/== \(.*\)/== \1 ==/g' \
        -e 's/= \(.*\)/= \1 =/g' \
        -e 's/\*\*\([^*]*\)\*\*/\*\*\1\*\*/g' \
        -e 's/\*\([^*]*\)\*/\/\/\1\/\//g' \
        -e 's/`\([^`]*\)`/%%\1%%/g' \
        -e 's/```\([^`]*\)```/<code>\n\1\n<\/code>/g' \
        -e 's/^\* /  \* /g' \
        -e 's/^- /  \* /g' \
        -e 's/^\([0-9]\+\)\. /  - /g' \
        "$input_file" > "$output_file"
}

# Create main start page
cat > /tmp/dokuwiki_sync/homelab/start.txt << 'EOF'
====== Homelab Documentation ======

===== Organized Documentation Structure =====

==== 🔧 Administration ====
  * [[homelab:admin:start|Administration Overview]]
  * [[homelab:admin:gitops-comprehensive-guide|GitOps Comprehensive Guide]]
  * [[homelab:admin:deployment-documentation|Deployment Documentation]]
  * [[homelab:admin:operational-status|Operational Status]]
  * [[homelab:admin:development|Development Guide]]

==== 🏗️ Infrastructure ====
  * [[homelab:infrastructure:start|Infrastructure Overview]]
  * [[homelab:infrastructure:ssh-guide|SSH Access Guide]]
  * [[homelab:infrastructure:networking|Networking Guide]]
  * [[homelab:infrastructure:monitoring|Monitoring Setup]]

==== 🎯 Services ====
  * [[homelab:services:start|Services Overview]]
  * [[homelab:services:service-index|Service Index]]
  * [[homelab:services:dashboard-setup|Dashboard Setup]]

==== 🚀 Getting Started ====
  * [[homelab:getting-started:start|Getting Started Overview]]
  * [[homelab:getting-started:beginner-quickstart|Beginner Quickstart]]
  * [[homelab:getting-started:what-is-homelab|What Is Homelab]]

==== 🛠️ Troubleshooting ====
  * [[homelab:troubleshooting:start|Troubleshooting Overview]]
  * [[homelab:troubleshooting:common-issues|Common Issues]]
  * [[homelab:troubleshooting:emergency-guide|Emergency Guide]]

===== System Information =====

**Repository**: https://git.vish.gg/Vish/homelab
**Wiki**: https://git.vish.gg/Vish/homelab/wiki
**DokuWiki**: http://atlantis.vish.local:8399/doku.php?id=homelab:start

Last updated: February 2026
EOF

processed_count=0

# Process admin docs
if [[ -d "docs/admin" ]]; then
    mkdir -p /tmp/dokuwiki_sync/homelab/admin

    # Create admin start page
    cat > /tmp/dokuwiki_sync/homelab/admin/start.txt << 'EOF'
====== Administration ======

===== System Management & Operations =====

==== Core Administration ====
  * [[homelab:admin:gitops-comprehensive-guide|GitOps Comprehensive Guide]] - Complete deployment procedures
  * [[homelab:admin:deployment-documentation|Deployment Documentation]] - Step-by-step deployment
  * [[homelab:admin:operational-status|Operational Status]] - Current system status
  * [[homelab:admin:development|Development Guide]] - Development procedures

==== Documentation & Integration ====
  * [[homelab:admin:agents|Agent Memory]] - AI agent context
  * [[homelab:admin:dokuwiki-integration|DokuWiki Integration]] - External wiki setup
  * [[homelab:admin:gitea-wiki-integration|Gitea Wiki Integration]] - Native wiki setup

[[homelab:start|← Back to Home]]
EOF

    # Convert admin markdown files
    for file in docs/admin/*.md; do
        if [[ -f "$file" ]]; then
            filename=$(basename "$file" .md)
            dokuwiki_name=$(echo "$filename" | tr '[:upper:]' '[:lower:]' | sed 's/_/-/g')
            convert_md_to_dokuwiki "$file" "/tmp/dokuwiki_sync/homelab/admin/${dokuwiki_name}.txt"
            ((processed_count++))
            echo "✅ Converted: admin/$filename"
        fi
    done
fi

# Process other directories
for dir in infrastructure services getting-started troubleshooting security hardware advanced runbooks; do
    if [[ -d "docs/$dir" ]]; then
        mkdir -p "/tmp/dokuwiki_sync/homelab/$dir"

        # Create start page for each directory
        cat > "/tmp/dokuwiki_sync/homelab/$dir/start.txt" << EOF
====== $(echo $dir | tr '[:lower:]' '[:upper:]' | tr '-' ' ') ======

===== Documentation for $dir =====

[[homelab:start|← Back to Home]]
EOF

        for file in "docs/$dir"/*.md; do
            if [[ -f "$file" ]]; then
                filename=$(basename "$file" .md)
                dokuwiki_name=$(echo "$filename" | tr '[:upper:]' '[:lower:]' | sed 's/_/-/g')
                convert_md_to_dokuwiki "$file" "/tmp/dokuwiki_sync/homelab/$dir/${dokuwiki_name}.txt"
                ((processed_count++))
                echo "✅ Converted: $dir/$filename"
            fi
        done
    fi
done

echo ""
echo "📊 DokuWiki Sync Summary:"
echo "✅ Files processed: $processed_count"
echo "📁 Structure created in: /tmp/dokuwiki_sync/homelab/"

echo ""
echo "📋 Ready to transfer to Atlantis server"
echo "🌐 DokuWiki will be available at: http://atlantis.vish.local:8399/doku.php?id=homelab:start"
echo ""
echo "✅ DokuWiki sync preparation completed!"
