#!/bin/bash
set -e

# Configuration
BLOG_DIR="blog"
TEMPLATE_FILE="$BLOG_DIR/template.html"
INDEX_FILE="$BLOG_DIR/index.html"
RSS_FILE="rss.xml"
SITE_URL="https://josie.lol"

# Ensure required commands are available
if ! command -v pandoc &> /dev/null; then
    echo "Error: pandoc is not installed. Please install it with:"
    echo "  sudo pacman -S pandoc  # For Arch Linux"
    exit 1
fi

echo "🔄 Building blog posts from Markdown..."

# Create RSS feed header if it doesn't exist
if [ ! -f "$RSS_FILE" ]; then
    cat > "$RSS_FILE" << EOF
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>josie.lol Blog</title>
  <link>${SITE_URL}</link>
  <description>Thoughts on cloud-native tech, infrastructure, security, and developer experience</description>
  <atom:link href="${SITE_URL}/${RSS_FILE}" rel="self" type="application/rss+xml" />
  <language>en-us</language>
  <lastBuildDate>$(date -R)</lastBuildDate>
  
<!-- POSTS_START -->
<!-- POSTS_END -->

</channel>
</rss>
EOF
    echo "📝 Created new RSS feed file"
fi

# Initialize or read the posts array
declare -A posts
post_files=()

# Process each markdown file in the blog directory
for md_file in $(find "$BLOG_DIR" -name "*.md" | sort -r); do
    filename=$(basename "$md_file")
    html_filename="${filename%.md}.html"
    
    # Skip processing template and index files
    if [[ "$filename" == "template.md" || "$filename" == "index.md" ]]; then
        continue
    fi
    
    echo "🔨 Processing: $filename"
    
    # Extract metadata from markdown file
    title=$(grep -m 1 "^title:" "$md_file" | sed 's/^title: *//')
    date=$(grep -m 1 "^date:" "$md_file" | sed 's/^date: *//')
    author=$(grep -m 1 "^author:" "$md_file" | sed 's/^author: *//')
    description=$(grep -m 1 "^description:" "$md_file" | sed 's/^description: *//')
    tags_line=$(grep -m 1 "^tags:" "$md_file" | sed 's/^tags: *//')
    
    # Format tags as comma-separated list for meta tags (for <meta> tags)
    tags_meta=$(echo "$tags_line" | sed 's/\[//g' | sed 's/\]//g')
    
    # Create an ISO date for meta tags
    date_iso="${date}T12:00:00Z"
    
    # Generate HTML tags for each tag and save to a file
    tags_html_file=$(mktemp)
    
    # Extract tags from the [tag1, tag2, ...] format
    if [[ "$tags_line" =~ \[(.*)\] ]]; then
        IFS=', ' read -r -a tag_array <<< "${BASH_REMATCH[1]}"
        for tag in "${tag_array[@]}"; do
            # Remove any quotes around the tag
            tag=$(echo "$tag" | sed 's/^"//g' | sed 's/"$//g' | sed "s/^'//g" | sed "s/'$//g")
            echo -n "<span class=\"text-xs mr-2 mb-2 px-2 py-1 bg-purple-900 bg-opacity-30 rounded-md\">$tag</span>" >> "$tags_html_file"
        done
    fi
    
    # Convert markdown to HTML using pandoc and save to a file
    content_file=$(mktemp)
    
    # Use pandoc with proper options for code blocks, ensuring proper class names for language
    pandoc -f markdown -t html --no-highlight "$md_file" > "$content_file"
    
    # Process the generated HTML to fix code blocks with custom styling
    final_content_file=$(mktemp)
    
    # Fix code blocks - ensure they have proper language classes and attributes
    cat "$content_file" | sed -E 's/<pre><code class="language-([^"]+)">/<pre class="language-\1"><code class="language-\1" data-language="\1">/' | \
                      sed -E 's/<pre><code>/<pre><code class="no-language" data-language="bash">/' > "$final_content_file"
    
    # Process images with captions (standard Markdown images are converted to figures with captions)
    image_content_file=$(mktemp)
    
    # This regex matches the pattern for <p><img src="..." alt="..." /></p> and transforms it to figure with caption
    # It extracts the alt text as the caption and adds it to a figcaption element
    cat "$final_content_file" | sed -E 's|<p><img src="([^"]+)" alt="([^"]+)" /></p>|<figure>\n  <img src="\1" alt="\2" class="w-full rounded-md shadow-lg" />\n  <figcaption>\2</figcaption>\n</figure>|g' > "$image_content_file"
    
    # Replace the content file with the improved version
    mv "$image_content_file" "$final_content_file"
    mv "$final_content_file" "$content_file"
    
    # Copy template to a working file
    working_file=$(mktemp)
    cp "$TEMPLATE_FILE" "$working_file"
    
    # Replace simple placeholders
    sed -i "s/TITLE_PLACEHOLDER/$title/g" "$working_file"
    sed -i "s/DESCRIPTION_PLACEHOLDER/$description/g" "$working_file"
    sed -i "s/AUTHOR_PLACEHOLDER/$author/g" "$working_file"
    sed -i "s/DATE_PLACEHOLDER/$date/g" "$working_file"
    sed -i "s/DATE_ISO_PLACEHOLDER/$date_iso/g" "$working_file"
    sed -i "s/TAGS_PLACEHOLDER/$tags_meta/g" "$working_file"
    sed -i "s/URL_PLACEHOLDER/$html_filename/g" "$working_file"
    
    # Read the tags HTML and content files
    tags_html=$(cat "$tags_html_file")
    
    # Create a temp file for the final HTML
    final_html=$(mktemp)
    
    # Process the working file line by line until we find CONTENT_PLACEHOLDER
    content_found=false
    while IFS= read -r line; do
        if [[ "$line" == *"TAGS_HTML_PLACEHOLDER"* ]]; then
            # This line contains the tags placeholder, replace it
            echo "${line/TAGS_HTML_PLACEHOLDER/$tags_html}" >> "$final_html"
        elif [[ "$line" == *"CONTENT_PLACEHOLDER"* && "$content_found" == false ]]; then
            # Found the content placeholder
            content_found=true
            
            # Insert the entire content from the file
            cat "$content_file" >> "$final_html"
        else
            # Skip lines with CONTENT_PLACEHOLDER if we've already processed it
            if [[ "$line" != *"CONTENT_PLACEHOLDER"* ]]; then
                # Regular line, just copy it
                echo "$line" >> "$final_html"
            fi
        fi
    done < "$working_file"
    
    # Copy the final HTML to the target location
    cp "$final_html" "$BLOG_DIR/$html_filename"
    
    # Clean up temp files
    rm "$tags_html_file" "$content_file" "$working_file" "$final_html"
    
    echo "✅ Generated: $BLOG_DIR/$html_filename"
    
    # Store paths and metadata for index and RSS
    # Store HTML tags for each tag (can't store directly in associative array due to special chars)
    tags_html_file=$(mktemp)
    cat > "$tags_html_file" << EOF
$(cat "$BLOG_DIR/$html_filename" | grep -o '<span class="text-xs mr-2 mb-2.*rounded-md">.*</span>' || echo "")
EOF
    
    posts["$html_filename"]="$title|$date|$description|$tags_html_file"
    post_files+=("$html_filename")
done

# Sort post files by date (assuming YYYY-MM-DD format in filename or metadata)
IFS=$'\n' sorted_files=($(
    for file in "${post_files[@]}"; do
        date=$(echo "${posts[$file]}" | cut -d'|' -f2)
        echo "$date|$file"
    done | sort -r | cut -d'|' -f2
))
unset IFS

# Update blog index.html with post list
if [ -f "$INDEX_FILE" ]; then
    echo "🔄 Updating blog index..."
    
    # Create a temp file for the rebuilt index
    temp_index=$(mktemp)
    
    # Variables to track if we're in the post list section
    in_post_list=false
    
    # Read the index file line by line
    while IFS= read -r line; do
        if [[ "$line" == *"<!-- BLOG_POST_LIST_START -->"* ]]; then
            echo "$line" >> "$temp_index"
            in_post_list=true
            
            # Add all posts
            for file in "${sorted_files[@]}"; do
                post_data="${posts[$file]}"
                IFS='|' read -r title date description tags_html_file <<< "$post_data"
                
                # Get tags HTML from file
                tags_html=$(cat "$tags_html_file")
                
                cat >> "$temp_index" << EOF
        <div class="py-4 border-b border-gray-700 dark:border-gray-600">
          <p class="text-sm text-gray-500 mb-2">$date</p>
          <h2 class="text-xl mb-2">
            <a href="$file" class="text-purple-500 dark:text-purple-400 no-underline transition-colors duration-200 hover:text-black dark:hover:text-white hover:underline">$title</a>
          </h2>
          <p class="text-gray-300 mb-2">$description</p>
          <div class="flex flex-wrap">
            $tags_html
          </div>
        </div>
EOF
            done
        elif [[ "$line" == *"<!-- BLOG_POST_LIST_END -->"* ]]; then
            echo "$line" >> "$temp_index"
            in_post_list=false
        elif [[ "$in_post_list" == false ]]; then
            echo "$line" >> "$temp_index"
        fi
    done < "$INDEX_FILE"
    
    # Replace the original index file
    cp "$temp_index" "$INDEX_FILE"
    rm "$temp_index"
    
    echo "✅ Updated blog index page"
fi

# Update RSS feed with posts
if [ -f "$RSS_FILE" ]; then
    echo "🔄 Updating RSS feed..."
    
    # Create a temp file for the rebuilt RSS
    temp_rss=$(mktemp)
    
    # Variables to track if we're in the posts section
    in_posts=false
    
    # Read the RSS file line by line
    while IFS= read -r line; do
        if [[ "$line" == *"<!-- POSTS_START -->"* ]]; then
            echo "$line" >> "$temp_rss"
            in_posts=true
            
            # Add all posts
            for file in "${sorted_files[@]}"; do
                post_data="${posts[$file]}"
                IFS='|' read -r title date description tags_html_file <<< "$post_data"
                
                # Convert date to RFC822 format for RSS
                rfc_date=$(date -d "$date" -R 2>/dev/null || date -R)
                
                cat >> "$temp_rss" << EOF
  <item>
    <title>$title</title>
    <link>${SITE_URL}/blog/$file</link>
    <description><![CDATA[$description]]></description>
    <pubDate>$rfc_date</pubDate>
    <guid>${SITE_URL}/blog/$file</guid>
  </item>
EOF
            done
        elif [[ "$line" == *"<!-- POSTS_END -->"* ]]; then
            echo "$line" >> "$temp_rss"
            in_posts=false
        elif [[ "$line" == *"<lastBuildDate>"* ]]; then
            # Update the lastBuildDate
            echo "  <lastBuildDate>$(date -R)</lastBuildDate>" >> "$temp_rss"
        elif [[ "$in_posts" == false ]]; then
            echo "$line" >> "$temp_rss"
        fi
    done < "$RSS_FILE"
    
    # Replace the original RSS file
    cp "$temp_rss" "$RSS_FILE"
    rm "$temp_rss"
    
    echo "✅ Updated RSS feed"
fi

# Clean up any remaining temp files
for file in "${post_files[@]}"; do
    post_data="${posts[$file]}"
    IFS='|' read -r title date description tags_html_file <<< "$post_data"
    
    # Remove temp tag file if it exists
    if [[ -f "$tags_html_file" ]]; then
        rm "$tags_html_file"
    fi
done

echo "🎉 Blog build completed successfully!" 