#!/bin/bash
# Script to reorganize frontmatter according to standard industry practice
# Groups fields logically: Post metadata, Publication info, Categorization, Display

set -e

echo "🔄 Organizing frontmatter according to standard industry practice..."

# Function to reorganize frontmatter in a markdown file
organize_frontmatter() {
    local md_file="$1"

    if [[ ! -f "$md_file" ]]; then
        return 0
    fi

    echo "📝 Organizing: $md_file"

    # Find frontmatter boundaries
    local frontmatter_end=$(grep -n "^---$" "$md_file" | sed -n '2p' | cut -d: -f1)
    if [[ -z "$frontmatter_end" ]]; then
        echo "  ⚠️  No frontmatter found, skipping"
        return 0
    fi

    # Create backup
    cp "$md_file" "$md_file.organized.backup"

    # Extract frontmatter content (between the --- markers)
    local frontmatter_content=$(sed -n '2,'$((frontmatter_end-1))'p' "$md_file")

    # Extract values using grep
    local id=$(echo "$frontmatter_content" | grep '^id:' || echo "")
    local title=$(echo "$frontmatter_content" | grep '^title:' || echo "")
    local slug=$(echo "$frontmatter_content" | grep '^slug:' || echo "")
    local subtitle=$(echo "$frontmatter_content" | grep '^subtitle:' || echo "")
    local excerpt=$(echo "$frontmatter_content" | grep '^excerpt:' || echo "")

    local author=$(echo "$frontmatter_content" | grep '^author:' || echo "")
    local date=$(echo "$frontmatter_content" | grep '^date:' || echo "")
    local published=$(echo "$frontmatter_content" | grep '^published:' || echo "")
    local featured=$(echo "$frontmatter_content" | grep '^featured:' || echo "")

    local category=$(echo "$frontmatter_content" | grep '^category:' || echo "")
    local tags=$(echo "$frontmatter_content" | grep '^tags:' || echo "")

    local read_time=$(echo "$frontmatter_content" | grep '^read_time:' || echo "")
    local sort_order=$(echo "$frontmatter_content" | grep '^sort_order:' || echo "")
    local css_class=$(echo "$frontmatter_content" | grep '^css_class:' || echo "")
    local css_style=$(echo "$frontmatter_content" | grep '^css_style:' || echo "")
    local category_description=$(echo "$frontmatter_content" | grep '^category_description:' || echo "")
    local category_published=$(echo "$frontmatter_content" | grep '^category_published:' || echo "")

    # Create new organized frontmatter
    {
        echo "---"
        echo "# Post metadata"
        [[ -n "$id" ]] && echo "$id"
        [[ -n "$title" ]] && echo "$title"
        [[ -n "$slug" ]] && echo "$slug"
        [[ -n "$subtitle" ]] && echo "$subtitle"
        [[ -n "$excerpt" ]] && echo "$excerpt"

        echo ""
        echo "# Publication info"
        [[ -n "$author" ]] && echo "$author"
        [[ -n "$date" ]] && echo "$date"
        [[ -n "$published" ]] && echo "$published"
        [[ -n "$featured" ]] && echo "$featured"

        echo ""
        echo "# Categorization"
        [[ -n "$category" ]] && echo "$category"
        [[ -n "$tags" ]] && echo "$tags"

        echo ""
        echo "# Display"
        [[ -n "$read_time" ]] && echo "$read_time"
        [[ -n "$sort_order" ]] && echo "$sort_order"
        [[ -n "$css_class" ]] && echo "$css_class"
        [[ -n "$css_style" ]] && echo "$css_style"
        [[ -n "$category_description" ]] && echo "$category_description"
        [[ -n "$category_published" ]] && echo "$category_published"
        echo "---"

        # Add the rest of the file content
        tail -n +$((frontmatter_end+1)) "$md_file"
    } > "$md_file.tmp"

    mv "$md_file.tmp" "$md_file"
    echo "  ✅ Organized with standard industry format"
}

# Process all markdown files
find site/content -name "*.md" -type f | while read -r md_file; do
    organize_frontmatter "$md_file"
done

echo "✅ Frontmatter organization completed!"
echo "📋 Backup files created with .organized.backup extension"
