#!/usr/bin/perl -w

=head1 NAME

dfical-datebook - parse an iCal file and create a QSF XML pilot_datebook instance.

=head1 DATAFREEDOM

These scripts developed from the 'pilot-qof' package but now include
support for other packages and formats and will continue to be extended
along the lines of http://www.data-freedom.org/ - liberating user data 
from the application. Therefore, the datafreedom scripts use a 'df' prefix.

The scripts continue to be developed within the pilot-qof CVS until such
time as the scripts are sufficiently cohesive to form a new source package.

Please feel free to contribute any of your own scripts, under the provisions
of the GNU General Public Licence v3 or later, via the QOF-devel mailing list.
http://lists.sourceforge.net/lists/listinfo/qof-devel

=head1 VERSION

Version 0.0.1

=head1 SYNOPSIS

 dfical-datebook FILENAME
 dfical-datebook -h|--help|--version

=head1 DESCRIPTION

dfical-datebook parses an iCal vFile and prepares a 
QSF XML pilot_datebook file.

Specify '-' as the filename to parse STDIN.

=head1 OBJECTS

L<http://qof.sourceforge.net/>

L<http://pilot-qof.sourceforge.net/>

L<http://gpe-expenses.sourceforge.net/>

=head1 AUTHOR

Neil Williams, C<< <codehelp at debian.org> >>

=head1 BUGS

Please report bugs via the pilot-qof package, either
in the Debian BTS or via SourceForge trackers.

=head1 COPYRIGHT & LICENSE

  Copyright 2007 Neil Williams.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.

=cut

use strict;
use Date::Parse;
use Date::Format;
use XML::QOFQSF qw(QSFWrite);
use Text::vFile::asData;

use vars qw ( $template $rep_freq $except $advance $transient $forever $alarm $untimed $repeatType $objects $vevent $desc $guid_str $note $r_end $end_date $r_start $start_date);

my $our_version = "0.0.1";
sub usageversion {
    print(STDERR <<END)
dfical-datebook version $our_version

Usage:
 dfical-datebook FILENAME
 dfical-datebook -h|--help|--version

Options:
 -h|--help:           print this usage message and exit
 --version:           print this usage message and exit

dfical-datebook parses an iCal vFile and prepares a 
QSF XML pilot_datebook file.

Specify '-' as the filename to parse STDIN.

END
        || die "$0: failed to write usage: $!\n";
exit 0;
}

my $stdin = "false";
while( @ARGV ) {
    $_= shift( @ARGV );
    if (/^-$/) {
 		$stdin = "true";
    	next;
    }
    last if m/^--$/;
    if (!/^-/) {
        unshift(@ARGV,$_);
        last;
    }
    if (/^(-h|--help|--version)$/) {
        &usageversion();
        exit( 0 );
    }
}

$template = "%Y-%m-%dT%H:%M:%SZ";
my $file = "";
$file = "-" if ($stdin eq "true");
$file = $ARGV[0] if ($stdin eq "false");
&usageversion if (!$file);
die "Cannot find $file. Please specify an iCal file.\n" 
	if ((! -f $file) && ($file ne '-'));

my %obj;
my @datebook=();
# default values.
$rep_freq = 0;
$except = 0;
$advance = 0;
$transient = "false";
$forever = "false";
$alarm = "false";
$untimed = "false";
$repeatType = "repeatNone";

open my $fh, "$file"
    or die "couldn't open iCal file: $!";
my @data = Text::vFile::asData->new->parse( $fh );
$objects = $data[0]->{'objects'}->[0]->{'objects'};
foreach my $card (@$objects)
{
	next if $card->{'type'} ne 'VEVENT';
	$vevent = $card->{'properties'};
	$desc = $$vevent{'SUMMARY'}->[0]->{'value'};
	$guid_str = $$vevent{'UID'}->[0]->{'value'};
	my $check = Math::BigInt->new($guid_str);
	$guid_str = '' if $check->is_zero();
	$guid_str = '' if $check->is_nan();
	$note = $$vevent{'DESCRIPTION'}->[0]->{'value'};
	$r_end = str2time($$vevent{'DTEND'}->[0]->{'value'});
	$end_date = time2str($template, $r_end);
	$r_start = str2time($$vevent{'DTSTART'}->[0]->{'value'});
	$start_date = time2str($template, $r_start);
	$note =~ s/\n$//;
	$desc =~ s/\n$//;
	my $c = new Appointment;
	$c->description($desc);
	$c->guid($guid_str);
	$c->note($note);
	$c->repeat_type($repeatType);
	$c->repeat_forever($forever);
	$c->use_alarm($alarm);
	$c->untimed_event($untimed);
	$c->transient_repeat($transient);
	$c->start_time($start_date);
	$c->end_time($end_date);
	$c->repeat_end($end_date);
	$c->repeat_frequency($rep_freq);
	$c->exception_count($except);
	$c->alarm_advance($advance);
	push @datebook, $c;
}

$obj{'pilot_datebook'} = \@datebook;

QSFWrite(\%obj);
