#!/usr/bin/perl
use strict;
use warnings;

use XML::RSS;      # no more XML::RSS::SimpleGen;
use File::Slurp;
use Mojo::DOM58;
use Time::Piece;

my $url = q<http://superkuh.com/blog/recent31337.html>;
my $baseurl = q<http://www.superkuh.com>;

print "starting...\n";

# initialize the RSS object using XML::RSS
my $rss = XML::RSS->new(version => '2.0');

# set the main channel
my $now_rfc822 = localtime->strftime("%a, %d %b %Y %H:%M:%S %z");
$rss->channel(
    title          => 'superkuh',
    link           => $url,
    description    => 'all blog posts ever',
    language       => 'en',
    lastBuildDate  => $now_rfc822,
    webMaster      => 'rss@superkuh.com',
);

# list of all posts as html <ul> <li></li> ... list
my $html = read_file('/home/superkuh/www/blog/recent31337.html');
die "Failed to read the local HTML file" unless $html;

my $dom = Mojo::DOM58->new($html);
my $item_count = 0;

# loop through each item, extract data, and add to the RSS object
$dom->find('li > a:nth-of-type(2)')->each(sub {
    my $fileurl   = $_->{href};
    my $titletext = $_->preceding('a')->first->text;
    my $file      = "/home/superkuh/www$fileurl";

    unless (-e $file) {
        warn "File not found, skipping: $file\n";
        return;
    }

    my $fulltext = read_file($file);
    # extract content from the first <div>
    if ($fulltext =~ m#<div.*?>(.*?)</div>#s) {
        $fulltext = $1;
    } else {
        warn "Could not find content div in $file\n";
        $fulltext = 'Content not available.';
    }

    # extract date from filename and format it for RSS
    # blog post .html filename conventions have date at the end, ie 2025-07-24-1.html
    my $pubDate = '';
    if ($fileurl =~ m/(\d{4}-\d{2}-\d{2})/) {
        my $date_str = $1;
        my $tp = Time::Piece->strptime($date_str . " 12:00:00", "%Y-%m-%d %H:%M:%S");
        $pubDate = $tp->strftime("%a, %d %b %Y %H:%M:%S GMT");
    }

    # add the item to the RSS feed with all its data
    $rss->add_item(
        title       => $titletext,
        link        => "$baseurl$fileurl",
        description => $fulltext,
        pubDate     => $pubDate, # gotta be a very specific date format
    );
    $item_count++;
});

# final validation and save
die "No items were found to add to the RSS feed. Aborting.\n" unless $item_count > 0;

$rss->save('/home/superkuh/www/rss-all.xml');

print "finished!\n";
exit;
