#!/bin/bash

# Configuration
BLOG_CONTENT_PATH="src/content"
ASSETS_PATH="public/assets"
BLOG_SOURCE="src/content/blog"

# Create necessary directories if they don't exist
mkdir -p "$BLOG_CONTENT_PATH/posts"
mkdir -p "$BLOG_CONTENT_PATH/configurations"
mkdir -p "$BLOG_CONTENT_PATH/projects"
mkdir -p "$ASSETS_PATH/images"

# Function to process markdown files
process_markdown() {
    local file="$1"
    local filename=$(basename "$file")
    
    # Convert Obsidian wiki-links to markdown links
    sed -i 's/\[\[/[/g; s/\]\]/]/g' "$file"
    
    # Update image paths
    sed -i 's/\(!\[.*\](\)attachments\//\1\/assets\/images\//g' "$file"
    
    echo "Processed $filename"
}

# Function to categorize content
categorize_content() {
    local file="$1"
    local filename=$(basename "$file")
    
    # Read the frontmatter to determine the category
    if grep -q "category: configuration" "$file"; then
        cp "$file" "$BLOG_CONTENT_PATH/configurations/"
        process_markdown "$BLOG_CONTENT_PATH/configurations/$filename"
    elif grep -q "category: project" "$file"; then
        cp "$file" "$BLOG_CONTENT_PATH/projects/"
        process_markdown "$BLOG_CONTENT_PATH/projects/$filename"
    else
        cp "$file" "$BLOG_CONTENT_PATH/posts/"
        process_markdown "$BLOG_CONTENT_PATH/posts/$filename"
    fi
}

# Process all markdown files in the blog directory
for file in "$BLOG_SOURCE"/*.md; do
    if [ -f "$file" ]; then
        categorize_content "$file"
    fi
done

# Ensure images are in place
if [ -d "$BLOG_SOURCE/images" ]; then
    cp -r "$BLOG_SOURCE/images/"* "$ASSETS_PATH/images/"
fi

echo "Content processing complete"
