#!/usr/local/bin/perl 
######################################################################
# 
# plan2html: an utility script for "plan" program
#
# 1993, Copyright CRS4
# Centre for Advanced Studies, R&D in Sardinia
# Via Nazario Sauro, 10, 09123 Cagliari (Italy)
# tel +39 - 70 2796 282, fax +39 - 70 2796 283
#
######################################################################
#
# File desription: translate .dayplan in .dayplan.html
#
# Created by: Andrea.Leone@crs4.it
#
######################################################################

# RCS informations

# 
# $Id$
#
# $Header$
#
# $Revision$
#
# $Log$
#
#

require '/usr/local/lib/perl/date.pl' ;

$status = "init" ;
$me = `whoami` ;
chop $me ;
$input_file = "/u/".$me."/.dayplan" ;
$pdate = "" ;
$today_julian_day = &today() ;
($month, $day, $year, $weekday) = &jdate($today_julian_day) ;
$week_name=&weekday($weekday) ;
$month_name=&monthname($month) ;


open (DAYPLAN, $input_file) ;
open (HTML, ">/u/".$me."/.dayplan.html");

print HTML "<H1>Day Plan of $me</H1>\n" ;
print HTML "Click <A HREF=\"#today\">here</A> for agenda of \n" ;
print HTML "$week_name, $month_name $day, $year\n" ;
print HTML "<BODY>\n" ;

print HTML "<HR>\n" ;

while (<DAYPLAN>) {

  chop $_ ;

  if (/^[0-9]/) {
    ($date, $start_time, $end_time, $ring1, $ring2, $foo) = split(/  /, $_) ;
    ($month, $day, $year) = split(/\//,$date);
    ($hour, $min, $sec) = split(/:/, $start_time);
    if ($date ne $pdate) {
      if ($status eq "got a text") {
        print HTML "</I>\n" ;
        print HTML "</UL>\n" ;
        print HTML "</UL>\n" ;
      }
      if ($status eq "got an entry") {
        print HTML "</UL>\n" ;
      }
      if ( $status eq "got a date" ) {
        print HTML "</UL>\n" ;
      }
      $julian_date=&jday($month, $day, $year) ;
      $week=(&jdate($julian_date))[3] ;
      $week_name=&weekday($week) ;
      $month_name=&monthname($month) ;
      if ( $julian_date == $today_julian_day ) {
        print HTML "<A NAME=\"today\"><H3>$week_name, $month_name $day, $year</H3></A>\n"  ;
      } else {
        print HTML "<H3>$week_name, $month_name $day, $year</H3>\n"  ;
      }
      print HTML "<UL>\n"  ;
    } else {
      if ($status eq "got a text") {
        print HTML "</I>\n" ;
        print HTML "</UL>\n" ;
      }
    }
    print HTML "<LI>\n" ;
    if ($hour != 99) {
      print HTML "<B>" ;
      if ($min <= 9) {
        print HTML "$hour:0$min"  ;
      } else {
        print HTML "$hour:$min"  ;
      }
      if ( $end_time ne "0:0:0" ) {
        ($hour_l, $min_l, $sec_l) = split(/:/, $end_time);
	$hour += $hour_l ;
	$min += $min_l ;
	$sec += $sec_l ;
	if ( $sec >= 60 ) {
          $sec -= 60 ;
          $min += 1;
        }
	if ( $min >= 60 ) {
          $min -= 60 ;
          $hour += 1;
        }
        if ($min <= 9) {
          print HTML "-$hour:0$min"  ;
        } else {
          print HTML "-$hour:$min"  ;
        }
      }
      print HTML "</B>: "  ;
    }
    $status = "got a date" ;
    $pdate = $date;
  } else {

    if (/^N/) {
      if ( $status eq "got a text" ) {
        print HTML "</I>\n" ;
        print HTML "</UL>\n" ;
      }
      $status = "got an entry" ;
      s/^N[\s]*// ;
      print HTML "$_\n" ;
    } else {

      if (/^M/) {
        if ( $status eq "got an entry" ) {
          print HTML "<UL>\n<LI><I>\n" ;
        }
        if ( $status eq "got a text" ) {
          print HTML "<BR>\n" ;
        }
        $status = "got a text" ;
        s/^M[\s]*// ;
        print HTML "$_\n" ;
      }
    }
  }
}

print HTML "</UL>\n" ;
print HTML "</BODY>\n" ;


