#!/usr/bin/perl
#$Id: toXML.pl,v 1.1 2001/02/25 21:20:31 connolly Exp $

use strict;
use Palm::Datebook;

main();

sub main{
    my $pdb = new Palm::PDB;

    $pdb->Load($ARGV[0]);
    toXML($pdb, $ARGV[1], $ARGV[2]); #@@
}

sub toXML{
    my($pdb, $isomin, $isomax) = @_;
    my $record;

    printf "<datebook min='%s' max='%s'\n>", $isomin, $isomax;

    foreach $record (@{$pdb->{records}})
    {
	my($isodate);
	$isodate = sprintf("%04d-%02d-%02d",
			   $record->{year},
			   $record->{month},
			   $record->{day});

	if($isodate gt $isomax){
	    printf STDERR "record %s starts after window: %s > %s\n",
		    $record->{id}, $isodate, $isomax;
	    next;
	}


	elsif($record->{repeat}){
	    if($record->{repeat}{end_year}){
		my($isoend);
		$isoend = sprintf("%04d-%02d-%02d",
				  $record->{repeat}{end_year},
				  $record->{repeat}{end_month},
				  $record->{repeat}{end_day});
		if($isoend lt $isomin){
		    printf STDERR "repeating record %s ends before window: %s < %s\n",
		    $record->{id}, $isoend, $isomin;
		    
		    next;
		}
	    }
	    #else: no end to the repeat... it's in the window
	}

	# doesn't repeat
	elsif($isodate lt $isomin){
	    printf STDERR "record %s before window: %s < %s\n",
		    $record->{id}, $isodate, $isomin;
	    next;
	}
	
	printf "<record id='%s'\n",
	$record->{id};
	
	printf "  category='%s'\n",
	$record->{category}
	if $record->{category};
	
	printf "  private='%s'\n",
	$record->{attributes}{private}
	if $record->{attributes}{private};
	
	printf "  deleted='%s'\n",
	$record->{attributes}{deleted}
	if $record->{attributes}{deleted};
	
	printf "  expunged='%s'\n",
	$record->{attributes}{expunged}
	if $record->{attributes}{expunged};
	
	printf "  date='%04d-%02d-%02d'\n",
	$record->{year},
	$record->{month},
	$record->{day};
	
	printf "  start_time='%02d:%02d' end_time='%02d:%02d'\n",
	$record->{start_hour}, $record->{start_minute},
	$record->{end_hour}, $record->{end_minute}
	unless $record->{start_hour} == 255; #@@ test others too?
	
	print "when_changed='1'\n" if $record->{when_changed};
	
	print ">";
	
	if ($record->{alarm}){
	    printf "<alarm advance='%s' unit='%s'\n/>",
	    $record->{alarm}{advance},
	    $record->{alarm}{unit}; # 0 for minutes, 1 for hours, 2 for days
	}
	
	
	if($record->{repeat}){
	    my ($gi);
	    
	    if($record->{repeat}{type} == 1){
		$gi = 'repeatDaily';
		printf "<%s\n", $gi;
		
	    }elsif($record->{repeat}{type} == 2){
		$gi = 'repeatWeekly';
		printf "<%s\n", $gi;
		
		printf "  days='%s' start_of_week='%s'\n",
		"@{$record->{repeat}{repeat_days}}", #@@ not sure which days of week
		$record->{repeat}{start_of_week};
		
	    }elsif($record->{repeat}{type} == 3){
		$gi = 'repeatMonthlyByDay';
		printf "<%s\n", $gi;
		
		printf "  weeknum='%s' daynum='%s'\n",
		$record->{repeat}{weeknum},
		$record->{repeat}{daynum};
		
	    }elsif($record->{repeat}{type} == 4){
		$gi = 'repeatMonthlyByDate';
		printf "<%s\n", $gi;
		
	    }elsif($record->{repeat}{type} == 5){
		$gi = 'repeatYearly';
		printf "<%s\n", $gi;
		
	    }else{
		die "unknown repeat type", $record->{repeat}{type};
	    }
	    
	    printf "  frequency='%s'\n",
	    $record->{repeat}{frequency};
	    
	    printf "  end_date='%04d-%02d-%02d'\n",
	    $record->{repeat}{end_year},
	    $record->{repeat}{end_month},
	    $record->{repeat}{end_day}
	    if $record->{repeat}{end_year};
	    
	    print ">";
	    
	    if($record->{exceptions}){
		print "<exceptions\n>@@</exceptions\n>";
	    }
	    
	    printf "</%s>", $gi;
	}
	
	printf "<description>%s</description\n>", &asContent($record->{description});
	printf "<note>%s</note\n>", &asContent($record->{note})
	    if $record->{note};
	
	printf "</record\n>";
    }

    print "</datebook\n>";
}


sub asContent{
    my($c) = @_;

    $c =~ s,&,&amp;,g;
    $c =~ s,<,&lt;,g;
    $c =~ s,>,&gt;,g;
    $c =~ s/[\200-\377]/'&#'.ord($&).';'/ge;

    #@@hhm... protect newlines too?

    return $c
}
