#!/usr/bin/env perl
# Created by Ben Okopnik on Wed Oct 11 21:45:39 EDT 2006
use warnings;
use strict;
$|++;
use CGI::Carp qw/fatalsToBrowser/;
use CGI qw/:standard/;
use HTML::Template;

my ( @projects, $update, $fh ); 

my $datadir = "jobs";
my $t = HTML::Template->new( filename => "jobs.tmpl", die_on_bad_params => 1 );

for my $file ( sort <$datadir/*> ){
	my %row;
	( my $name = $file ) =~ y/_/ /;
	$name =~ s#.*?/##;
	open Fh, "$file" or die "$file: $!\n";
	chomp( my $status = <Fh> );
	chomp( my $maint = <Fh> );
	chomp( my $email = <Fh> );
	close Fh;

	# A bit of very loose format checking
	die "Error in $file: no such status!\n" unless $status =~ /red|blue|yellow|green/;
	die "Error in $file: invalid email!\n" unless $email =~ /\w+\@\w+/;

	@row{ qw/NAME STATUS MAINT EMAIL URL/ } = ( $name, $status, $maint, $email, $file );
	push @projects, \%row;
}

# Format the date as "wkday mon day year"
$update = join " ", ( split ' ', localtime )[ 0..2, 4 ];

# Submit parameters
$t -> param( UPDATE => $update, PROJECTS => \@projects );

# Output everything including the header if invoked as CGI
if ( $0 =~ /cgi$/ ){
	print header;
	$fh = *STDOUT;
}
else {
	# Write "jobs.html" otherwise (link not named *cgi)
	open Jobs, ">jobs.html" or die "Can't open jobs.html: $!\n";
	$fh = *Jobs;
}

print $fh start_html( "Linux Gazette - Project Status" ), $t -> output, end_html;
close $fh;