#!/usr/bin/perl

my $infilen=$ARGV[0];



open (IN,$infilen);

top();

#              $_newline = $/;
 #              $/ = '';


while(<IN>){

$date;
$stime;
$etime;
$title;
$title2;
$authors;
$rdfauthors="";

#print "\n$_\n";
	if($authors ne "" && $title ne ""){
#debug($authors, $title);

	print "<component>";
	print "\n";
	print "<Vevent>";
	print "\n";
	print '<dtstart rdf:parseType="Resource">';
	print "\n";
	print "<dateTime>".$date."T".$stime.":00</dateTime>";
	print "\n";
	print "<tzid rdf:resource=\"#tz\"/>";
	print "\n";
	print '</dtstart>';
	print "\n";

	print '<dtend rdf:parseType="Resource">';
	print "\n";
	print "<dateTime>".$date."T".$etime.":00</dateTime>";
	print "\n";
	print '</dtend>';
	print "\n";
	print "<summary>".$title."\n";
	print $authors."\n</summary>";
	print "\n";
	print "</Vevent>";
	print "\n";
	print "</component>";
	print "\n";

	$title="";
	$authors="";

#	print "\n\n<foaf:Person>\n<foaf:name>".$rdfauthors . 
#"</foaf:name>\n</foaf:Person>\n";

	}



	if($_=~ m/<p class=\"title\" align=\"center\">(.*)<\/p>/){
#	print "\nmain title: ".$1;
	}


	if($_=~ m/p class=\"btext\">.*, (\d\d .* \d\d\d\d)<\/td>/){
#	print "\ndate: ".$1;
	$date=$1;
####fix the date

	$date=~ s/May/05/;
	$date=~ s/^([^ ]+) +([^ ]+) +([^ ]+)/$3-$2-$1/;

	}

	if($_=~ m/width=\"189\">(.*)\n/){
		if($_=~ m/<\/td>/){
		}else{
#		print "\ntitle: ".$1;
		}
	}

	if($_=~ m/\s\s\s\s\s\s\s\s\s\s([a-zA-Z].*)<br>\s*/){
#	print "\ntitle: ".$1;
	}

	if($_=~ m/(\d?\d.\d\d) - \d?\d.\d\d<\/td>/){
	$stime=$1;
		if($stime=~ m/\d\d.\d\d/){
		}else{
		$stime="0$stime";
		}
	$stime=~ s/\./:/;
	}

	if($_=~ m/\d?\d.\d\d - (\d?\d.\d\d)<\/td>/){
	$etime=$1;
		if($etime=~ m/\d\d.\d\d/){
		}else{
		$etime="0$etime";
		}
	$etime=~ s/\./:/;
	}

	if(($_=~ m/<li><strong>(.*)<\/strong>/)
		 || ($_=~ m/<li><b>(.*)<\/b>/)
		 || ($_=~ m/<li><b>(.*)\n/)
		 || ($_=~ m/<li><strong>(.*)\n/)){
#	print "\ntitle!: ".$1;
	$title=$1;
	}


	if($_=~ m/\s\s\s\s\s\s\s\s\s\s([a-zA-Z].*)<\/b>.*$/){
	$title=$title . $1;
	$title=~ s/\r/\n/g;
	}

	if($_=~ m/\s\s\s\s\s\s\s\s\s\s([a-zA-Z].*)<\/li>\s*$/){
#	print "\nauthor: ".$1;
	$authors=$1;	

####fix the authors
	$rdfauthors=$authors;

#	$rdfauthors=~ s/\s?,\s?/<\/foaf:name>\n<\/foaf:Person>\n</attendee>
#<foaf:Person>\n<foaf:name>/g;

	}

#debug($authors, $title);


}

#$/ = $_newline;


bottom();


sub top{

print <<RDF;
<rdf:RDF
  xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
  xmlns='http://www.w3.org/2002/12/cal/ical#'
  xmlns:i='http://www.w3.org/2002/12/cal/ical#'
  xmlns:x='http://www.softwarestudio.org/libical/UsingLibical/node49.html#'>
    <Vcalendar>
    <prodid>-//Libby//conversion of www.www2003.org//EN</prodid>
    <version>2.0</version>
RDF
}

sub bottom{

print <<RDFF;
  <component>
  <Vtimezone rdf:about='#tz'>
    <tzid>/softwarestudio.org/Olson_20011030_5/Europe/Budapest</tzid>
    <x:licLocation>Europe/Budapest</x:licLocation>
  <daylight rdf:parseType='Resource'>
    <tzoffsetfrom>+0100</tzoffsetfrom>
    <tzoffsetto>+0200</tzoffsetto>
    <tzname>CEST</tzname>
    <dtstart rdf:parseType='Resource'>
      <dateTime>1970-03-29T02:00:00</dateTime>
    </dtstart>
    <rrule rdf:parseType='Resource'>
      <bymonth>3</bymonth>
      <freq>YEARLY</freq>
      <byday>-1SU</byday>
      <interval>1</interval>
    </rrule>
  </daylight>
  <standard rdf:parseType='Resource'>
    <tzoffsetfrom>+0200</tzoffsetfrom>
    <tzoffsetto>+0100</tzoffsetto>
    <tzname>CET</tzname>
    <dtstart rdf:parseType='Resource'>
      <dateTime>1970-10-25T03:00:00</dateTime>
    </dtstart>
    <rrule rdf:parseType='Resource'>  
      <bymonth>10</bymonth>
      <freq>YEARLY</freq>
      <byday>-1SU</byday>
      <interval>1</interval>
    </rrule>
  </standard>
  </Vtimezone>
  </component>
  </Vcalendar>
</rdf:RDF>

RDFF
}


sub debug{

my $a=shift;
my $b=shift;

print "\na:...$a...";
print "\nb:...$b...";

}

print "\n\n";
