#!/bin/perl
#
#
#  xcal.el xcal-19.el  顼    Ver 1.001
#
#    Copyleft 1994 Shigeki.Morimoto(mori@jnet.co.jp)
#
#
#(00/09/22;taiji)-X ץ xcalendar ߴ⡼(Ver 1.001)
#(00/09/22;taiji)-x ץ xmessage ǤΤ餻
#(99/01/20;taiji)2000 ǯ
#(94/9/20;morimoto)Ver 1.0 
#
#
# BUG: դѤնǤ 顼ưޤ (_ _)
#
#

# 塼Υǥ쥯ȥ
$CAL_DIR = $ENV{"HOME"} . "/Calendar";
# ʬ åɽ뤫
$countdown = "0,5,10";
# ä˹뤫
$update = 10;

@month = (undef,
	  "Jan", "Feb", "Mar", "Apr", 
	  "May", "Jun", "Jul", "Aug",
	  "Sep", "Oct", "Nov", "Dec");

require('getopts.pl');		# ץϥ롼ɤ߹

# ץβ
&usage if(! &Getopts('d:c:u:Xxqh') || defined $opt_q || defined $opt_h) || -1 != $#ARGV;

$CAL_DIR = $opt_d if(defined $opt_d);
$countdown = $opt_c if(defined $opt_c);
$update = $opt_u if(defined $opt_u);
$external_command = "xmessage" if ($opt_x);

for(;;){
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
	localtime(time);
    $mon ++;
    if($oldYear != $year  ||
       $oldMon  != $mon ||
       $oldMday != $mday){

	# դѤä & ư
	&readScheduleFile(&xcalFileName($year, $mon, $mday));

	# եι
	($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
	 $atime,$oldMtime,$ctime,$blksize,$blocks)
	    = stat(&xcalFileName($year, $mon, $mday));
    }else{
	($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
	 $atime,$mtime,$ctime,$blksize,$blocks)
	    = stat(&xcalFileName($year, $mon, $mday));
	if($oldMtime != $mtime){
	    # ե뤬ѹ줿
	    &readScheduleFile(&xcalFileName($year, $mon, $mday));
	}
	$oldMtime= $mtime;
    }
    $oldYear = $year;
    $oldMon  = $mon;
    $oldMday = $mday;

    $now = $hour*60+$min;
    undef $out;
    for $step (reverse (sort (split(",", $countdown)))){
	if(! defined $doneAlarm{"$now:$step"} &&
	   $alarmTime{$now+$step}){
	    if($step){
		$out .= "$stepʬǤ\n";
	    }else{
		$out .= "֤֤Ǥ\n";
	    }
	    $out .= $alarmTime{$now+$step};
	}

	# ɽѤߥޡ򥻥å
	$doneAlarm{"$now:$step"} ++;
    }
    if($out ne ""){
      if (!$external_command) {
	$| = 1;
	print "SOT\n${out}EOT\n";
	$| = 0;
      } else {
	$out =~ s/\n//g;
	system("$external_command $out &");
      }
    }

    sleep $update;
}

sub readScheduleFile
{
    local($filename) = @_;
    local($hour, $min, $ampm, $msg);

    undef %alarmTime;
    undef %doneAlarm;

    open(SCH, $filename);
    while(<SCH>){
	s/\n//;

	undef $hour;
	undef $min;
	undef $ampm;
	
	if(/^(\d{1,2})(|a|p|am|pm)(.*)$/i){
	    # h or hh
	    $hour = $1;
	    $min  = 0;
	    $ampm = $2;
	    $msg  = $3; $msg =~ s/^\s*//;
	}elsif(/^(\d{1,2})(|:|\.)(\d\d)(|a|p|am|pm)(.*)$/i){
	    # hmm hhmm h:mm hh:mm h.mm hh.mm
	    $hour = $1;
	    $min  = $3;
	    $ampm = $4;
	    $msg  = $5; $msg =~ s/^\s*//;
	}
	if(defined $hour){
	    # 00:00am  00:00pm ˤʤͤϤդ
	    # ѹޤ
	    if($ampm =~ /(p|pm)/i){
		$hour += 12;
	    }
	    $alarmTime{$hour*60+$min} .= "  $_\n";
	}
    }
}

sub xcalFileName
{
    local($year, $month, $day) = @_;

    $year += 1900;
    if ($opt_X) {
      return "$CAL_DIR/xc$day$month[$month]$year";
    }
    else {
      return "$CAL_DIR/xy$year/xc$day$month[$month]$year";
    }    
}

sub usage 
{
    ($command = $0) =~ s#.*/##;

    print STDERR <<"EOF";
Usage: $command [options]
Options:
  -d directory                Υǥ쥯ȥ
                              default  [$CAL_DIR]
  -c countdown                ʬ˥顼Ф
                              `,' Ƕڤäƻꤹ
                              default  [$countdown]
  -u update                   åץǡȤδֳ
                              default  [$update]
  -X                          xcalendar ߴ⡼
  -x                          åνϤ xmessage Ȥ
EOF

    exit(10);
}
