#!/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.
# it ignores .html files with modified-by times that are 11:11 since that means it's a makephotoblogpost.pl post.

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 55 31336/;

for (@daysback) {
	recents($_);
}

# generate the RSS feeds too
# get it at http://www.superkuh.com/blog/blogrssgen.pl
`/home/superkuh/www/blog/blogrssgen.pl`;
`/home/superkuh/www/blog/blogrssgen-allposts-mojo.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
#GOOD	#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";
	## 
	##no, don't use -photo- tags, instead use the specifically set modified-by time of 11:11 to filter them out. and a regex for the years till 2030
	## find . -mtime -10000 -name "20*.html" -not -name comments.html -not -name template.html -not -name recent7.html -not -name recent31337.html -not -name blog.html -not -regex ".*blog-20(1[8-9]|2[0-9]).*\.html" -printf "%p %TH:%TM\n" | grep -v " 11:11\$" | cut -d' ' -f1 | sort | tac


	my $findargs;
	if ($displaydays == 31337) {
		$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 -regex \".*blog-20(18|19|2[0-4]).*\\.html\" -not -name \"*-photo.html\" | sort | tac";
	} else {
		$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 -regex \".*blog-20(18|19|2[0-4]).*\\.html\" -not -name \"*-photo.html\" -printf \"%p %TH:%TM\\n\" | grep -v \" 11:11\$\" | cut -d' ' -f1 | sort | tac";
	}



	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);
#	}

	# or the h-entry microformat markup with regex to handle the old style too.
	# 	<h3 class="p-name">CSS in HTTP Headers</a></h3>
	#	m{<h3 class="p-name">(<a href=\".+?\">)?(.*?)(</a>)?</h3>}sg;
	#	m{<div><h3>(<a href=\".+?\">)?(.*?)(</a>)?</h3>}sg;
	#	m{(<div>)?<h3( class=\"p-name\")?>(<a href=\".+?\">)?(.*?)(</a>)?</h3>}sg;
	#	     1	              2                   3            4    5
	while(<THEFILE>) {
		m{(<div>)?<h3( class=\"p-name\")?>(<a href=\".+?\">)?(.*?)(</a>)?</h3>}sg;
		$title = $4;
		#print "1: $1, 2: $2, 3: $3, 4: $4, 5: $5, 6: $6,\n";
		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; 
}
