#!/usr/bin/perl

# By Mathias Moe (mathias@jpg.no) for Fr. James Smith, march 2019

use lib 
use Data::ICal;
use Data::ICal::Entry::Event;
use DateTime;
use DateTime::Format::ICal;
use JSON;

# # Hentet fra Data::ICal::Entry::Event -- som ikke er tilgjengelig på serveren der dette scriptet skal kjøre
# 
# sub ical_entry_type {'VEVENT'}
# 
# 
# sub mandatory_unique_properties {
#     my $self = shift;
#     return $self->rfc_strict ? ("uid") : ()
# }
# 
# sub optional_unique_properties {
#     my $self = shift;
#     my @ret = $self->rfc_strict ? () : ("uid");
#     if (not $self->vcal10) {
#         push @ret, qw(
#             class  created  description  dtstart  geo
#             last-modified  location  organizer  priority
#             dtstamp  sequence  status  summary  transp
#             url  recurrence-id
# 
#             dtend duration
#         );
#     } else {
#         push @ret, qw(
#             class dcreated completed description dtstart dtend
#             last-modified location rnum priority
#             sequence status summary transp
#             url
#         );
#     }
#     return @ret;
# }
# 
# sub optional_repeatable_properties {
#     my $self = shift;
#     if (not $self->vcal10) {
#         qw(
#             attach  attendee  categories  comment
#             contact  exdate  exrule  request-status  related-to
#             resources  rdate  rrule
#         );
#     } else {
#         qw(
#             aalarm  attach  attendee  categories
#             dalarm  exdate  exrule  malarm  palarm  related-to
#             resources  rdate  rrule
#         );
#     }
# }



# Set outfile here
my $outfile = "/home/apps1/htdocs/mathias/CalTest/jms.ics";

# Instanciate the calendar
my $calendar = Data::ICal->new("JMS");

# Data source
my $source = `curl -L "http://www.rotundasoftware.com/ministry/FSSPATL/schedules/?pointer=last"`;

# Remove anything befor this
$source =~ s/^.*window.Baseline.bootstrapData =//s;

# Remove anything after this:
$source =~ s/^(.*?)\;window\.Baseline.*$/$1/s;


my $calendardata = decode_json($source);

my @masses = @{$calendardata->{ms}};

foreach $mass (@masses) {
	 print "$mass->{dateTime}: $mass->{name}\n";

	# Date-time is ALMOST ISO-8601
	my $datetime = $mass->{dateTime};
	$datetime =~ m/(\d{4})-(\d{2})-(\d{2})\s(\d{2})\:(\d{2})\:(\d{2})/;
	$dt = DateTime->new(
		year       => $1,
		month      => $2,
		day        => $3,
		hour       => $4,
		minute     => $5,
		second     => $6,
		nanosecond => 0
#		time_zone  => 'America/New_York'
		);

	$end = DateTime->new(
		year       => $1,
		month      => $2,
		day        => $3,
		hour       => $4,
		minute     => $5,
		second     => $6,
		nanosecond => 0
#		time_zone  => 'America/New_York'
		);


	# If name is set, use this else use label	
	my $summary = (($mass->{name} =~ m/\w/) ? $mass->{name} : $mass->{label});

	# Add hours and minutes to get end
	$end->add( hours => $mass->{duration}->{hours}, minutes => $mass->{duration}->{minutes} );

	
	my $ts = DateTime->now;

	my $vevent = Data::ICal::Entry::Event->new();
#	my $vevent = Data::ICal::Entry->new();

    $vevent->add_properties(
        summary => $summary,
        description => "",
        # Dat*e*::ICal is not a typo here
        dtstart   => DateTime::Format::ICal->format_datetime($dt),
        dtend     => DateTime::Format::ICal->format_datetime($end),
        dtstamp => DateTime::Format::ICal->format_datetime($ts),
        uid        => $mass->{_id}
        );


	$calendar->add_entry($vevent);
	}


open (FH, '>', "$outfile") or die $!;
print FH $calendar->as_string;
close FH;
