#!/opt/local/bin/perl

# USAGE: RefreshMusicAuditStatusPage.pl > MusicAuditStatus.htm
# Location: ~/bin
# Last Updated: 04-27-24 - implemented the '_S' feature

# format of the audit check textfile (case sensitive): Checked_8 - digit date[_S].txt
# the '_S' string indicates that a playlist for that band exists. 

# can't find module, can't find module, can't find module.
# FIX: the cpan utility. Eventually, it will fix it. But you may have to watch scrolling data for a long, long time.

# this enables the given and when statements without warning errors
use experimental 'switch';

use File::Iterator;
use File::Basename;

# enabes given-when statement (perl switch() equivalent)
use feature qw(switch);

use DateTime;
my $dt = DateTime -> now(time_zone => 'America/Phoenix');
my $date = $dt->ymd;
my $time = $dt->hms;

print "<!DOCTYPE html>\n\n";
print "<html>\n\n<head>\n";
print "\t<title>Music Collection Audit Status</title>\n";
print "</head>\n\n";
print "<body>\n\n<h2>My Music Collection Audit Status</h2>\n";
print "<address>" . "\t" . $date . "\t" . $time . "</address><br>\n\n";
print "<table cellpadding=5 cellspacing=3>\n\n";
print "<tr>";
print "<td width=280 align=left><b><u>Artist</u></b></td>";
print "<td width=146 align=left><b><u>DateOfLastAudit</u></b></td>";
print "<td width=113 align=left><b><u>Playlist Created</u></b></td>";
print "<tr><td></td><td></td></tr>\n\n";
print "</tr>";

$Total = 0;
$TotalChecked = 0;

$it = new File::Iterator
(
  DIR     => '/Users/Pudnik/Music',
  RECURSE => 0,
  RETURNDIRS => 1
);

while ($file = $it->next()) 
{
  $Total++;
  if (-d $file) 
  {
    chdir $file;
    @files = glob("Checked_*.txt");
    foreach $textfile (@files)
    {
      $TotalChecked++;
      ($name,$path,$ext) = fileparse($file, qr/\.[^.]*/);
      print "<tr><td>" . $name . "</td><td>" . substr($textfile,8,8) . "</td><td>";
      given (length($textfile)) 
      {
        when (length($textfile) < 20) {print length($textfile);}
        when (20) {print "";}
        when (21) {print length($textfile);}
        when (22) 
        {
          if (substr($textfile,16,2) == "_S"){print "yes";}
          else {print length($textfile);}
        }
        when (length($textfile) > 22) {print length($textfile);}
      }
      print "</td></tr>\n";      
    }
  }
  chdir "..";
}

print "</table>\n\n";
print "<p>" . $TotalChecked . " / " . $Total . " = " . $TotalChecked / $Total * 100 . "% </p>\n\n";

print "</body>\n</html>";