#!/bin/bash
# Upload documentation to DokuWiki
# Usage: ./upload-to-dokuwiki.sh

DOKUWIKI_BASE="http://atlantis.vish.local:8399"
REPO_ROOT="/home/homelab/organized/repos/homelab"

echo "🚀 Starting DokuWiki documentation upload..."

# Function to convert basic Markdown to DokuWiki syntax
convert_md_to_dokuwiki() {
    local input_file="$1"
    local temp_file=$(mktemp)

    # Basic conversions
    sed -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/^\([0-9]\+\)\. \(.*\)/  - \2/g' \
        -e 's/- \[x\] \(.*\)/  \* ✅ \1/g' \
        -e 's/- \[ \] \(.*\)/  \* ☐ \1/g' \
        "$input_file" > "$temp_file"

    echo "$temp_file"
}

# Function to create DokuWiki page
create_dokuwiki_page() {
    local page_id="$1"
    local content_file="$2"
    local summary="$3"

    echo "📄 Creating page: $page_id"

    # Try to create the page using curl
    curl -s -X POST "$DOKUWIKI_BASE/doku.php" \
        -d "id=$page_id" \
        -d "do=save" \
        -d "summary=$summary" \
        -d "minor=1" \
        --data-urlencode "wikitext@$content_file" \
        > /dev/null

    if [ $? -eq 0 ]; then
        echo "✅ Successfully created: $page_id"
        echo "🌐 View at: $DOKUWIKI_BASE/doku.php?id=$page_id"
        return 0
    else
        echo "❌ Failed to create: $page_id"
        return 1
    fi
}

# Create main index page
echo ""
echo "📋 Creating main homelab index page..."
cat > /tmp/homelab_index.txt << 'EOF'
====== Homelab Documentation ======

//This documentation is automatically mirrored from the homelab Git repository//

===== Quick Navigation =====

  * [[homelab:readme|Main README]] - Repository overview and quick start
  * [[homelab:docs:index|Documentation Index]] - Complete navigation guide
  * [[homelab:docs:admin:gitops_comprehensive_guide|GitOps Deployment Guide]] - Complete deployment procedures
  * [[homelab:documentation_audit_report|Documentation Audit Report]] - Recent improvements
  * [[homelab:operational_status|Operational Status]] - Current system status
  * [[homelab:monitoring_architecture|Monitoring Architecture]] - Monitoring setup

===== Infrastructure =====

  * [[homelab:docs:infrastructure:health_report|Infrastructure Health Report]] - System health status
  * [[homelab:gitops_deployment_guide|GitOps Deployment Guide]] - Deployment procedures

===== Operations =====

  * [[homelab:docs:runbooks:add_new_service|Add New Service]] - Service deployment runbook

===== About =====

This DokuWiki instance mirrors the documentation from the homelab Git repository at https://git.vish.gg/Vish/homelab

**Last Updated:** $(date)
**Source Repository:** https://git.vish.gg/Vish/homelab
**GitOps Status:** ✅ 18 active stacks, 50+ containers
EOF

create_dokuwiki_page "homelab:start" "/tmp/homelab_index.txt" "Created homelab documentation index"

# Convert and upload key documentation files
declare -A docs_map=(
    ["$REPO_ROOT/README.md"]="homelab:readme"
    ["$REPO_ROOT/docs/INDEX.md"]="homelab:docs:index"
    ["$REPO_ROOT/docs/admin/GITOPS_COMPREHENSIVE_GUIDE.md"]="homelab:docs:admin:gitops_comprehensive_guide"
    ["$REPO_ROOT/DOCUMENTATION_AUDIT_REPORT.md"]="homelab:documentation_audit_report"
    ["$REPO_ROOT/docs/infrastructure/INFRASTRUCTURE_HEALTH_REPORT.md"]="homelab:docs:infrastructure:health_report"
    ["$REPO_ROOT/docs/runbooks/add-new-service.md"]="homelab:docs:runbooks:add_new_service"
    ["$REPO_ROOT/GITOPS_DEPLOYMENT_GUIDE.md"]="homelab:gitops_deployment_guide"
    ["$REPO_ROOT/OPERATIONAL_STATUS.md"]="homelab:operational_status"
    ["$REPO_ROOT/MONITORING_ARCHITECTURE.md"]="homelab:monitoring_architecture"
)

successful=0
total=0

for file_path in "${!docs_map[@]}"; do
    page_id="${docs_map[$file_path]}"
    total=$((total + 1))

    if [ -f "$file_path" ]; then
        echo ""
        echo "📄 Converting: $(basename "$file_path")"

        # Convert Markdown to DokuWiki
        converted_file=$(convert_md_to_dokuwiki "$file_path")

        # Add header with source info
        temp_with_header=$(mktemp)
        cat > "$temp_with_header" << EOF
====== $(basename "$file_path") ======

//This page is automatically mirrored from the homelab Git repository//
//Last updated: $(date)//
//Source: $file_path//

EOF
        cat "$converted_file" >> "$temp_with_header"

        # Upload to DokuWiki
        if create_dokuwiki_page "$page_id" "$temp_with_header" "Updated from repository"; then
            successful=$((successful + 1))
        fi

        # Cleanup
        rm -f "$converted_file" "$temp_with_header"
    else
        echo "⚠️  File not found: $file_path"
    fi
done

echo ""
echo "🎯 Upload Summary:"
echo "✅ Successful: $successful/$total"
echo "❌ Failed: $((total - successful))/$total"

if [ $successful -gt 0 ]; then
    echo ""
    echo "🌐 DokuWiki documentation available at:"
    echo "   $DOKUWIKI_BASE/doku.php?id=homelab:start"
    echo "   $DOKUWIKI_BASE/doku.php?id=homelab:readme"
    echo "   $DOKUWIKI_BASE/doku.php?id=homelab:docs:index"
fi

# Cleanup
rm -f /tmp/homelab_index.txt

echo ""
echo "✅ DokuWiki upload completed!"
