Index: /trunk/tools/czarcal
===================================================================
--- /trunk/tools/czarcal	(revision 32559)
+++ /trunk/tools/czarcal	(revision 32559)
@@ -0,0 +1,136 @@
+#!/usr/bin/env perl
+
+# create an iCalendar with IPP czar assignments for a given number of days
+
+use strict;
+use warnings;
+
+use Time::Local;
+use Time::localtime;
+use Time::gmtime;
+
+
+# number of days that an individual is assinged to be czar
+my $czar_days = 2;
+my $first_czar = shift;
+my $start_date = shift;
+my $num_assignments = shift;
+
+$num_assignments = 1 unless $num_assignments;
+
+
+# list of all czars
+my @staff = qw(
+mark
+gene
+bill
+roy
+serge
+heather
+chris
+);
+
+die "usage: $0 <first czar> <start_date> <num assignments>\n  czar list: @staff\n"
+    unless $start_date and $first_czar;
+
+my @list = get_czars($first_czar);
+die "failed to find $first_czar in @list\n" if !scalar @list;
+
+my $begin_vcalendar =
+"BEGIN:VCALENDAR
+CALSCALE:GREGORIAN
+VERSION:2.0
+X-WR-CALNAME:IPP Czar calendar for import
+METHOD:PUBLISH
+PRODID:-//IfA.hawaii.edu//czarcal 1.0//EN";
+
+my $end_vcalendar =
+"END:VCALENDAR
+";
+
+
+
+my $year = substr $start_date, 0, 4;
+my $month = substr $start_date, 4, 2;
+my $day = substr $start_date, 6, 2;
+
+my $tm = localtime(timelocal(0, 0, 0, $day, $month - 1, $year - 1900));
+my $time = timelocal(0, 0, 0, $day, $month - 1, $year - 1900);
+
+print "$begin_vcalendar\n";
+
+my $j = 0;
+my $this_czar = shift @list;
+for (my $i = 0; $i < $num_assignments; $i++) {
+    $tm = localtime($time);
+    if ($tm->wday != 6 and $tm->wday != 0) {
+
+        assign_czar($this_czar, $tm->year+1900, $tm->mon+1, $tm->mday);
+
+        if (++$j == $czar_days) {
+            if (!scalar @list) {
+                @list = get_czars();
+            }
+            $this_czar = shift @list;
+            $j = 0;
+        }
+    } else {
+        # bump counter to account for weekend day
+        $num_assignments++;
+    }
+    $time += 86400;
+}
+
+
+print "$end_vcalendar\n";
+
+sub get_czars {
+    my $czar = shift;
+
+    my @czarlist = @staff;
+    if ($czar) {
+        for (my $i = 0; $i < scalar @staff; $i++) {
+            my $c = $staff[$i];
+            last if $c eq $czar;
+            shift @czarlist;
+        }
+    }
+    return @czarlist;
+}
+
+my $num_events = 0;
+sub assign_czar {
+#    print STDERR "assign: @_\n";
+    my $czar = shift;
+    my $year = shift;
+    my $month = shift;
+    my $day = shift;
+
+    $num_events++;
+
+    my $start = sprintf "%4d%02d%02d", $year, $month, $day;
+    my $start_ticks = timelocal(0, 0, 0, $day, $month - 1, $year - 1900);
+    my $end_ticks = $start_ticks + 86400;
+
+    my $tm = localtime($end_ticks);
+    my $end = sprintf "%4d%02d%02d", $tm->year+1900, $tm->mon + 1, $tm->mday;
+
+    $tm = gmtime(time());
+
+    my $now = sprintf "%4d%02d%02dT%02d%02d%02dZ", $tm->year+1900, $tm->mon + 1, $tm->mday, $tm->hour, $tm->min, $tm->sec;
+
+    my $uid = "$now-$num_events". '@czarcal.ipp.ifa.hawaii.edu';
+
+    print "BEGIN:VEVENT\n";
+    print "CREATED:$now\n";
+    print "UID:$uid\n";
+    print "DTEND;VALUE=DATE:$end\n";
+    print "TRANSP:TRANSPARENT\n";
+    print "SUMMARY:czar $czar\n";
+    print "DTSTART;VALUE=DATE:$start\n";
+    print "DTSTAMP:$now\n";
+    print "SEQUENCE:0\n";
+    print "END:VEVENT\n";
+}
+
+
