#!/bin/sh
# -*- perl -*-
# This code allows us to start perl from our path or an environment variable
# rather than hardcoding a path into the #! line.  It works from sh or csh.
(exit $?0) && eval 'exec ${QPERLQ-perl} -x $0 ${1+"$@"}'
if (! $?QPERLQ) setenv QPERLQ perl
exec $QPERLQ -x $0 $argv:q

#!/usr/local/bin/perl -w
#
# $Id: qalendar,v 1.4 1996/02/04 11:23:21 qjb Exp $
# $Source: /home/qjb/scripts/RCS/qalendar,v $
# $Author: qjb $
#
# Simplified version of "calendar".  Accepts lines of the form
#
# date message
#
# or 
# #include "file"
#
# date must be one of
#   month day
#   month/day
#   month-day
#
# Month can be at least the first three letters of a text month or a number.
# It can also be * which will match every month.
#
# For including files, the home directory is searched.
#
# This script then reads ~/lib/calendar or, if it does not existe,
# ~/calendar and prints matching lines.	 Based on BSD calendar
# program, but lacks some more esoteric functionality.
#

require 5.000;
use strict qw(refs subs);

$whoami = ($0 =~ m,([^/]*)$,) ? $1 : $0;

@months = (qw(january february march april may june),
	   qw(july august september october november december));
@mdays = (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
@cmdays = @mdays;		# for calculation

$home = $ENV{'HOME'} || ((getpwuid($<))[7]) ||
    die "$whoami: can't find home directory\n";

chdir $home || die "$whoami: can't chdir $home\n";

chdir "lib" if (-f "lib/calendar");

$cal = "calendar";
exit 0 if ! -f $cal;
$errors = 0;

($mday, $mon, $year) = (localtime)[3..5];
$cmdays[1]-- if ($year % 4);	# Will break in 2100.  I don\'t care.

%use = ();
&use($mon, $mday);
for ($i = 1; $i <= 4; $i++)
{
    $mday++;
    if ($mday > $cmdays[$mon])
    {
	$mday = 1;
	$mon++;
    }
    if ($mon >= @months)
    {
	$mon = 0;	# cmdays[1] may be wrong now, but it doesn't matter
    }
    &use($mon, $mday);
}

@output = ();
&parse_cal($cal);
exit 1 if $errors;

map { print } @output;

sub parse_cal
{
    local($file) = @_;
    local(@lines) = ();
    if (open(F, "<$file"))
    {
	@lines = <F>;
	close(F);
	local($l) = 0;
	for ($l = 1; $l <= @lines; $l++)
	{
	    $line = $lines[$l-1];
	    chomp($line);
	    $line =~ s/^\s+//;
	    next if $line eq "";
	    local($month, $day);
	    if ($line =~ s/^\#\s*include\s+[\"<](\S+)[\">]\s*$//)
	    {
		&parse_cal($1);
	    }
	    elsif ($line =~ m,^\s*([\w\d\*]+)\s*[/-]?\s*(\d+)\s*,)
	    {
		($month, $day) = ($1, $2);
		&store($month, $day, $line);
	    }
	    else
	    {
		warn "$whoami: $file: line $l: invalid line $line\n";
		$errors = 1;
	    }
	}
    }
    else
    {
	warn "$whoami: can't open $file: $!\n";
    }
}

sub store
{
    local($month, $day, $msg) = @_;
    local($omonth, $oday) = ($month, $day);
    local($invalidm, $invalidd) = (0, 0);
    $month =~ y/A-Z/a-z/;
    if ($month =~ m/^[a-z]{3,}$/)
    {
	$month = &month_name_to_number($month);
	if ($month == -1)
	{
	    $invalidm = 1;
	}
    }
    elsif ($month =~ m/^[0-9]+$/)
    {
	$month--;
	if (($month < 0) || ($month > 11))
	{
	    $invalidm = 1;
	}
    }
    elsif ($month eq "*")
    {
    }
    else
    {
	$invalidm = 1;
    }

    if ($invalidm)
    {
	$errors = 1;
	warn "$whoami: $file: line $l: invalid month \`$omonth\'\n";
	return;
    }	

    if ($month =~ m/^\d+/)
    {
	if (($day < 1) || ($day > $mdays[$month]))
	{
	    $invalidd = 1;
	}
    }    

    if ($invalidd)
    {
	$errors = 1;
	warn "$whoami: $file: line $l: invalid day \`$omonth $oday\'\n";
	return;
    }	

    if (&check($month, $day))
    {
	push(@output, "$msg\n");
    }
}

sub month_name_to_number
{
    local($month) = @_;
    local($m, $i);
    for ($i = 0; $i < @months; $i++)
    {
	$m = $months[$i];
	if ($m =~ m/^$month/)
	{
	    return $i;
	}
    }
    -1;
}

sub use
{
    local($m, $d) = @_;
    $use{"$m/$d"} = 1;
    $use{"*/$d"} = 1;
}

sub check
{
    local($m, $d) = @_;
    exists $use{"$m/$d"};
}
