#!/afs/athena/contrib/perl/p

print("<title>Globe Event Listings</title>\n");
print("<h1>Boston Globe Events</h1>\n");
print("<dl>\n");

while(<>){
    chop;
#    print("Checking ----- -$_-\n");
    if (/^[\n \t]*$/) {
	if($in eq "header"){
	    $in = "main";
	}
	next;
    }
    $in = 0 if /\[cc[^\]]+\]/;
    if (/\[uf([^\]]+)\]/i) {
	$in = 0;
	$category = $1; $continuecategory =1;
	$category = substr($category, 1) if substr($category, 0, 1) eq "l";
    }
    s/\[[^\]]\]//g unless /\[mdbo\](([^\[])+)\[mdnm\](.+)/;

    if(/\[mdbo\](.+)/){
	$rest = $1;
	if(/\[mdbo\](([^\[])+)\[mdnm\](.+)/){
	    if($event){
		&do_event;
	    }
	    $event_name = $1;
	    $text = $3;
	    
	    $event = 1;
	    $in ="events";
#	    print("</dl><h2>$category</h2><dl>\n") if $continuecategory;
	    $continuecategory=0;
	}
	else{
	    $in = "etitle";
	}
    }				# 
    elsif($in eq "etitle"){
	if(/(.*)\[mdnm\]/){
	    $rest .= $1;
	    $event_name = $rest;
	    $in = "main";
	}
	else{
	    $rest .= $_;
	}
    }			# 
    elsif($in ne "etitle" && /(.+)\[mdnm\]/){
	if($event){
		&do_event;
	    }
	    $event_name = $1;
	    $text = $3;
	    
	    $event = 1;
	    $in ="events";
#	    print("</dl><h2>$category</h2><dl>\n") if $continuecategory;
	    $continuecategory=0;
    }
    elsif(/From hd.+psi\.com/){
	$in = "header";
    }
    elsif($in eq "main"){
	if(!/[a-z]/){
	    if($continuecategory){
		$category .=$_;
	    }
	    else{
		$category = $_;
	    }			# 
	    $continuecategory = 1; # 
	}
    }
    elsif($in eq "events"){
	$text .= $_;
#	print("</dl><h2>$category</h2><dl>\n") if $continuecategory;
	$continuecategory=0;
    }
    
}
print("</dl>\n");



sub do_event {
#    print("\n\nCategory: $category\n");
#    print("Event Name: $event_name\n");
    print("(");
    print("(\"name\" \"$event_name\") \n");
#    print("Text: $text\n");
    print("(\"body\" \"$text\") \n");

    $fulltext = $text;
    
    # Phone number ?
    $phone = '';
    while($text =~ /([\d]*\-*\d\d\d\-\d\d\d\d)/g){
	$phone .="$1, ";
    }
    $phone = substr($phone, 0, length($phone)-2);
    print("(\"phone\" \"$phone\") \n") if $phone;

    # Time/date ?
    $when = '';
    while($text =~/([A-Z]..\. [\d,\-]+ [\d:\-]+ ?[ap]?\.?m?\.?)/g){
	$when .= "$1, ";
    }
    while($when =~/([A-Z]..)\. ([\d\-]+)/g){
	print("(\"date\" \"$1 $2\") \n");
    }
    while($when =~ /([\d:\-]+) ?([ap])\.?(m)\.?/g){
	print("(\"time\" \"$1 $2$3\") \n");
    }
    print(")\n\n");


#    print("Phone: $phone\n") if $phone;
#    print("When: $when\n") if $when;

}
