#!/bin/sh

OUTPUT_FILE="/home/aaron/nginx/rss/data/nyaa-combined-feed.xml"
TEMP_FILE="/tmp/nyaa-combined-feed.xml.tmp"
FEEDS_FILE="/opt/rss-aggregator/nyaa-feeds.txt"

LOG_DIR="/var/log/rss"
LOG_FILE="$LOG_DIR/rss-nyaa-$(date '+%Y%m%d-%H%M%S').log"

# Log function
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}

# Start RSS feed
cat > "$TEMP_FILE" << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Combined RSS Feed</title>
    <description>Aggregated RSS feeds from multiple sources</description>
    <link>http://localhost</link>
EOF

echo "    <lastBuildDate>$(date -R)</lastBuildDate>" >> "$TEMP_FILE"

# Process each feed URL
while IFS= read -r url || [ -n "$url" ]; do
  # Skip empty lines and comments
  [ -z "$url" ] && continue
  echo "$url" | grep -q "^#" && continue
  
  log "Fetching: $url"
  
  # Fetch the feed with timeout
  FEED_CONTENT=$(curl -sf --max-time 30 "$url" 2>/dev/null)
  
  if [ $? -eq 0 ] && [ -n "$FEED_CONTENT" ]; then
    # Extract items from the feed (between <item> and </item> tags)
    # Remove any lines with namespace prefixes (like source:, nyaa:, etc.)
    echo "$FEED_CONTENT" | sed -n '/<item>/,/<\/item>/p' | grep -v '<[a-zA-Z0-9_-]*:' >> "$TEMP_FILE"
  else
    log "Failed to fetch: $url"
  fi
  
  # Small delay to avoid hammering servers
  sleep 2
  
done < "$FEEDS_FILE"

# Close RSS feed
cat >> "$TEMP_FILE" << 'EOF'
  </channel>
</rss>
EOF

# Move temp file to final location atomically
mv -f "$TEMP_FILE" "$OUTPUT_FILE"

log "RSS aggregation complete: $OUTPUT_FILE"
