#!/usr/bin/perl
#
# Skript for å konvertere ical-filer til HTML-kode som kan inkluderes
# under "Hva skjer" på forsiden av www.nuug.no.

use strict;
use warnings;

use iCal::Parser;

use Data::Dumper;

# Input is UTF-8, output is ISO-8859-1
#use utf-8, STDOUT => "Latin1";

my $showentries = 7;

my %output;

my $today = DateTime->now;

for my $filename (@ARGV) {
    my $parser = iCal::Parser->new();
    my $hash = $parser->parse($filename);

    for my $year (sort keys %{$hash->{'events'}}) {
        for my $month (sort keys %{$hash->{'events'}->{$year}}) {
            for my $day (sort keys %{$hash->{'events'}->{$year}->{$month}}) {
                for my $evid (keys %{$hash->{'events'}->{$year}->{$month}->{$day}}) {
                    my $event =
                        $hash->{'events'}->{$year}->{$month}->{$day}->{$evid};
                    my %newevent;
                    $newevent{description} = $event->{'DESCRIPTION'};
                    $newevent{created} = $event->{'CREATED'};
                    $newevent{dtend} = $event->{'DTEND'};
                    $newevent{dtstamp} = $event->{'DTSTAMP'};
                    $newevent{dtstart} = $event->{'DTSTART'};
                    $newevent{'last-modified'} = $event->{'LAST-MODIFIED'};
                    $newevent{sequence} = $event->{'SEQUENCE'};
                    $newevent{summary} = $event->{'SUMMARY'};
                    $newevent{uid} = $event->{'UID'};
                    $newevent{url} = $event->{'URL'};
                    $newevent{location} = $event->{'LOCATION'};

                    # some descriptions / URLs are HTML (looking at you,
                    # Google calendar) and at the same time these aren't
                    # the actual URLs with a bit of <a href=...> ... </a>
                    # around them but instead links to Google Search, e.g.,
                    #
                    # DESCRIPTION:<a href="https://www.google.com/url?q=
                    # https://www.usenix.org/conferences&amp\;sa=D&amp\;
                    # source=calendar&amp\;ust=1690220367651630&amp\;usg
                    # =AOvVaw1HHR0mhO4t00wEJ_O0SjKd" target="_blank">htt
                    # ps://www.usenix.org/conferences</a>
                    #
                    # This breaks the HTML we generate.  Thus: do some basic
                    # HTML stripping.

                    my $url = $event->{URL} || $event->{DESCRIPTION};
                    if ($url) {
                        if ($url =~ m/<a href=[^>]*>([^<]*)<\/a>/) {
                            $url = $1;
                        }
                    }

                    my $start = $newevent{dtstart};
                    my $end = $newevent{dtend};
                    my $location = $newevent{location};
                    my $title = $newevent{summary};
                    my $startstr = $start->day . "." . $start->month;
                    my $endstr = $end->day . "." . $end->month;
                    my $datestr = $startstr;
                    if ($startstr ne $endstr) {
                        $datestr = "$startstr-$endstr";
                    }

                    next unless $start ge $today;
                    next unless (defined $location);

                    $location =~ s/\\,/,/g;
                    $title =~ s/\\,/,/g;

                    if (defined $url && $url) {
                        $output{$start} = <<EOF;
  <div class="eventwrap">
    <div class="time"><p>$datestr</p></div>
    <div class="event"><a href="$url">$title</a>, $location</div>
  </div> 

EOF
                    } else {
                        $output{$start} = <<EOF;
  <div class="eventwrap">
    <div class="time"><p>$datestr</p></div>
    <div class="event">$title, $location</div>
  </div>

EOF
                    }
                }
            }
        }
    }
}

my $count = 0;
for my $str (sort keys %output) {
    print $output{$str};
    last if $count++ > $showentries;
}

exit 0;
