#!/usr/bin/perl
my ($PROGNAME,$BASENAME) = ($0,$0);
$PROGNAME =~ s|^.*/||;
$BASENAME="./$BASENAME" if ($BASENAME !~ /\//);
$BASENAME =~ s|/[^\/]+$||;
$BASENAME=$BASENAME || "/";

open(IN,"<$BASENAME/todo") || die("Can't read $BASENAME/todo list\n");
my %todo;
while(<IN>) {
  # priority	sched	complete	requestor description
  s/#.*//g;
  next unless /\S/;
  die("Can't parse line [$. of todo]\n")
    unless /^([lmhcd][^\t]*)\t+([^\t]+)\t+(\d+)%?\t+(\S+)\t+([^\t].*)$/i;
  my ($prio,$sched,$comp,$req,$desc) = (lc($1),$2,$3,$4,$5);
  push(@{$todo{$prio}}, [$sched,$comp,$req,$desc]);
}
close IN;

open(STDOUT,">$BASENAME/index.html") || die("Can't write $BASENAME/index.html\n");

print <<HEADER;
<html>
<head>
  <title>Development schedule for MyVite.com</title>
</head>
<body bgcolor=white>

<table width=90%>
  <tr valign='bottom'>
    <td>
      <a href=/><img src='/Pics/MyVite_Inc.gif' border=0 alt='MyVite Inc.'></a>
    </td>
    <td align='right'>
      <h1>Development Schedule</h1>
    </td>
  </tr>
</table>

<table width='100%'>
HEADER

foreach my $priority ( qw(DONE High Medium Low Consideration) ) {
  my $prio = lc(substr($priority,0,1));
  my $prio_full = ($prio eq "c") ? "Under $priority" :
                  ($prio eq "d") ? $priority : "Priority: $priority";
  print <<START_PRIO;
  <tr>
    <td colspan='4'>
      <br><br>
      <b>$prio_full</b><br>
    </td>
  </tr>
  <tr>
    <td><b>Schedule&nbsp;</b></td>
    <td><b>Complete&nbsp;</b></td>
    <td><b>Requestor&nbsp;</b></td>
    <td><b>Description&nbsp;</b></td>
  </tr>
  <tr>
    <td colspan='4'><hr></td>
  </tr>
START_PRIO
  foreach my $l ( @{$todo{$prio}} ) {
    my ($sched,$comp,$req,$desc) = @$l;
    $sched="<center><img src='Check.gif' alt='X'></center>" if $prio eq "d";
    $comp=100 if $prio eq "d";
    if ($comp>=0 && $comp<=100) {
      my $red=2.55*(100-$comp);
      my $green=2.55*$comp;
      $comp=sprintf("<font color='#%2.2x%2.2x00'>$comp</font>",$red,$green);
    }
    $comp .= "\%";
    $comp="" if $prio eq "c";
    print <<TODO;
  <tr valign='top'>
    <td>$sched</td>
    <td>$comp</td>
    <td>$req</td>
    <td>$desc</td>
  </tr>
TODO
  }
}

print <<FOOTER;
</table>
<br><br>
<i>Automagically generated by <a href=make>todo_make</a> from <a href=todo>source</a></i>
</body>
</html>
FOOTER

close STDOUT;
