#!/usr/bin/perl -w

use strict;
use warnings;

# this script is repurposed to fit from an existing script for another pipeline.
# basically, just find in the dir then generate an ordered list html fragment.
# when that's done convert the ordered list to RSS with another perl script.

my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = gmtime();
my $year = 1900 + $yearOffset;

my $outputpath = '/home/superkuh/www/blog/recent';
my $dirpath = '/home/superkuh/www/blog/';
my $day_of_year = (localtime)[7] +1;
#my @daysback = qw/6 30 182 364 $day_of_year/;
my @daysback = qw/6 13 31336/;

for (@daysback) {
	recents($_);
}

# generate the RSS feeds too
`/home/superkuh/www/blog/blogrssgen.pl`;

sub recents {
	my $days = shift;
	my $displaydays = $days + 1; # why change it at this point?
	my $filetoopen = $outputpath . $displaydays . ".html";
	# find /home/superkuh/www/blog/ -name "*.html" -not -name comments.html -not -name blog.html -not -name "blog-2019*.html" | sort | tac
	my $findargs = "find $dirpath -mtime -$days -name \"20*.html\" -not -name comments.html -not -name template.html -not -name recent7.html -not -name recent31337.html -not -name blog.html -not -name \"blog-2019*.html\" -not -name \"blog-2020*.html\" -not -name \"blog-2018*.html\" | sort | tac";
	#my $findargs = "find $dirpath -mtime -$days -not -name comments.html -name \"*.html\"";
	my $filesize;
	my $date = getdate();

	open(RECENT, "$findargs|") or die "cannot open the pipe to find, $findargs\n$!";

	open(OUTPUT, ">$filetoopen") or die "cannot create $outputpath$displaydays.html\n$!";

	#print OUTPUT "<ul class=\"smalltext\">";
	print OUTPUT "<ul>";

	print "\$_: $_\nTest: $filetoopen\n";
	my $latestyear = 0;
	while (defined (my $recent = <RECENT>)) {
		chomp($recent);
		next if -d "$recent";
		my $posttitle = returnposttitle($recent);
		my $mod = (stat "$recent")[9]; 
		$mod = scalar localtime $mod;
		$filesize = niceSize(-s "$recent",0);
		$recent =~ m#/home/superkuh/www/blog/(.+)#;
		my $filename = $1;
		$recent = $filename;

		my $bareid = $recent;
		$bareid =~ s/\.html//;
		$bareid =~ m#(\d{4})-\d{2}-\d{2}-\d\d?#;
		my $tempyear = $1;
		if ($tempyear > $latestyear) {
			$latestyear = $tempyear;	
		}


		# html style spaces
		$recent =~ s#\s#\%20#g;
		$recent =~ s/(\%20)$//;

		#print OUTPUT "<li><a href=\"/blog/$recent\">$posttitle</a> ($filesize at $mod)</li>\n";
		#print OUTPUT "<li><a href=\"/blog/$recent\">$posttitle</a> [$recent]</li>\n";
		#print OUTPUT "<li><a href=\"/blog/$recent\">$posttitle</a> [<a href=\"/blog/blog.html\#$bareid\">$bareid</a>]</li>\n";

		# first find result should usually be on the front page
		if ($latestyear == 0) {
			print OUTPUT "<li><a href=\"/blog/blog.html\#$bareid\">$posttitle</a> [<a href=\"/blog/$recent\">$bareid</a>]</li>\n";
		} elsif ($tempyear != $latestyear) {
			print OUTPUT "<li><a href=\"/blog/blog-$tempyear.html\#$bareid\">$posttitle</a> [<a href=\"/blog/$recent\">$bareid</a>]</li>\n";
		} else {
			print OUTPUT "<li><a href=\"/blog/blog.html\#$bareid\">$posttitle</a> [<a href=\"/blog/$recent\">$bareid</a>]</li>\n";
		}

		#print OUTPUT "<li><a href=\"/blog/blog.html\#$bareid\">$posttitle</a> [<a href=\"/blog/$recent\">$bareid</a>]</li>\n";


	}
	print OUTPUT "</ul>\n";

	close OUTPUT or die "$!\n";
	close RECENT or die "$!\n";
}

sub returnposttitle {
	my $htmlfiletoparse = shift;
	#print "\nfilepath: $htmlfiletoparse";
	my $title;
	open( THEFILE, "<$htmlfiletoparse") or die "Can't open file $htmlfiletoparse $!";
	# <div><h3>There were dozens of these brown (?) snakes all along the road.</h3>
	# <div><h3><a href="http://sci-hub.se/10.1063/1.1609040">An easy modification to suppress noise from a household magnetron</a></h3>
	while(<THEFILE>) {
		m{<div><h3>(<a href=\".+?\">)?(.*?)(</a>)?</h3>}sg;
		#print "1: $1, 2: $2, 3: $3\n";
		$title = $2;
		#print "\nTitle: $title" if $title;
		last if ($title);
	}
#	my $file_content = do { local $/; <$fh> };
#	while($file_content =~ m{<div><h3>(<a href="">)?(.*)(</a>)?</h3>}sg) {
#		$title = $1;	
#	}
	return $title;
}

sub niceSize {
	# Will work up to considerable file sizes!
	my $fs = $_[0];	# First variable is the size in bytes
	my $dp = $_[1];	# Number of decimal places required
	my @units = ('bytes','kB','MB','GB','TB','PB','EB','ZB','YB');
	my $u = 0;
	$dp = ($dp > 0) ? 10**$dp : 1;
	while($fs > 1024){
		$fs /= 1024;
		$u++;
	}
	if($units[$u]){ return (int($fs*$dp)/$dp)." ".$units[$u]; } else{ return int($fs); }
}

sub getdate {
	my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
	my @weekDays = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
	my ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = gmtime();
	my $year = 1900 + $yearOffset;
	$second = sprintf("%02d",$second);
	$minute = sprintf("%02d",$minute);
	my $theGMTime = "$hour:$minute:$second, $weekDays[$dayOfWeek] $months[$month] $dayOfMonth, $year";
	return $theGMTime; 
}
