#!/usr/bin/perl -w
# Created by Ben Okopnik on Sat Jun 27 07:25:26 CDT 2009
use strict;

my $DEBUG = 1;

# {EVENT} text for deadline 
my $d_text = "this month's deadline has just passed, and it's time for the proofing/editing team to revise the articles";
# {EVENT} text for end of month 
my $e_text = "the publication date is approaching, and the opportunity for final revisions is coming to a close";

# Calculate last day of the month for leap years
my $eom = {
    January   => sub {31},
    February  => sub {28 + ($_ [0] % 400 ? 0 :  1) +
                           ($_ [0] % 100 ? 0 : -1) +
                           ($_ [0] %   4 ? 0 :  1)},
    March     => sub {31},
    April     => sub {30},
    May       => sub {31},
    June      => sub {30},
    July      => sub {31},
    August    => sub {31},
    September => sub {30},
    October   => sub {31},
    November  => sub {30},
    December  => sub {31},
};

my %mon;
@mon{1..12} = qw/January February March April May June July August September October November December/;

my ($day, $month, $year) = (localtime)[3, 4, 5];
$month = $mon{$month+1};
$year += 1900;

my %deadline = (
	January    => 21,
	February   => 18,
	March      => 21,
	April      => 20,
	May        => 20,
	June       => 20,
	July       => 21,
	August     => 21,
	September  => 21,
	October    => 21,
	November   => 19,
	December   => 19,
);

# Adjust deadline for leap years
$deadline{February}++ if $month eq 'February' && $eom->{$month}->($year) == 29;

# Terminate unless it's one of the two important days - or DEBUG is on
unless ($day == $deadline{$month} || $day == $eom->{$month}->($year) || $DEBUG){
	print "Today is $day $month $year; that's neither the deadline nor the EOM.\n";
	exit;
}

### Get the data
my %vlist;
open V, '<', "$ENV{HOME}/volunteer_list" or die "~/volunteer_list: $!\n";
while (<V>){
	chomp;
	# Data stored as kinda-CSV
	my ($name, $email, $job) = split /\s*,\s*/;
	if ($job =~ /proof|editor/i){
		$vlist{$email} = [ $name, $job ];
	}
}
close V;

open R, '<', "$ENV{HOME}/reminder.txt" or die "~/reminder.txt: $!\n";
my $text = do { local $/; <R> };
close R;

if ($day == $deadline{$month} || $DEBUG == 1){
	$text =~ s/{EVENT}/$d_text/;
	for my $email (keys %vlist){
		(my $t = $text) =~ s/{JOB}/$vlist{email}->[1]/gsm;
		if ($DEBUG){
			print qq{echo $t|/usr/bin/mutt -x -s "Linux Gazette: Monthly deadline" $email\n};
		}
		else {
			qx#echo $t|/usr/bin/mutt -x -s "Linux Gazette: Monthly deadline" $email#;
			# Don't slam the server with all these emails
			sleep 1;
		}
	}
}
# I know it's the only option remaining, but I'm being extra careful here
elsif ($day == $eom->{$month}->($year) || $DEBUG == 2){
	$text =~ s/{EVENT}/$e_text/;
	for my $email (keys %vlist){
		(my $t = $text) =~ s/{JOB}/$vlist{email}->[1]/gsm;
		if ($DEBUG){
			print qq{echo $t|/usr/bin/mutt -x -s "Linux Gazette: Publishing tomorrow" $email\n};
		}
		else {
			qx#echo $t|/usr/bin/mutt -x -s "Linux Gazette: Publishing tomorrow" $email#;
			# Don't slam the server with all these emails
			sleep 1;
		}
	}
}