#! /usr/bin/perl -w

# by Kristian Vilmann, Ole Tange & Hans Schou

# 2000.01.14: Modified by Jacob Sparre Andersen to allow multi-digit prices.

# 2000.05.22: Modified sub makehtml to produce correct HTML (Jacob Sparre Andersen).

# 1. Open all files in $datadir
# 2. Merge all files sorted to 'bigfile'
# 3. Open 'bigfile' and make html

use strict;
use CGI;
use CGI::Carp;

if (@ARGV != 1) {
        print "Wrong number of arguments\n";
        print "Usage: makecal.pl datadir\n";
        exit(1);
}
my $datadir = $ARGV[0];

# make a today-string in the format: YYYYMMDD 19991231
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) 
    = localtime(time);
my $today=sprintf("%04d%02d%02d",$year+1900,$mon+1,$mday);

my (@bad,@ok);

# Open 'sources' and save all events from each organisation
# in a separate file. If 'localevents' can not be read then
# do not overwrite the old event-file.
# While reading the event, change the organisation name
# in 'localevents' to name specified in 'sources' (first argument).

# Read 'sources' into an array
open(SRC,"sources") || die "'sources' not found";
my $srcline;
while ($srcline = <SRC>) {
  chomp $srcline;
  my ($organizer,$localevents) = split(/;/, $srcline);
  my @events = ();
  open(EVENT, "/usr/bin/wget -nv -t 3 -O - $localevents 2>/dev/null |") || die "Fail $0";
  while (<EVENT>) {
    chomp;
    if (!/^DATE/) {
      # Change field 14 to $org
      my @line = split(/;/, $_);
      if (defined($line[0]) && $line[0] && $line[14]) {
        $line[14] = $organizer;
        push @events, join(";", @line)."\n";
      } else {
        push (@bad,"1: $organizer: $_\n");  # save 'bad' lines into @bad
      }
    } else {
      push (@bad,"2: $organizer: $_\n");  # save 'bad' lines into @bad
    }
  }
  close(EVENT);
  # if any events in buffer, store
  if (@events) {
    open(OUT, ">$datadir/$organizer") || die "Can not write to disk";
    print OUT @events;
    close(OUT);
  } else {
    # if old organizer file is more than 14 days, then erase it
    if ( -e "$datadir/$organizer" and -M "$datadir/$organizer" > 14 ) {
      warn "'$datadir/$organizer' more than 14 days, erased";
      unlink("$datadir/$organizer");
    }
  }
}
close(SRC);

# Get all file names from the data directory
my @files=<$datadir/*>;

# Push file names on command line
@ARGV=@files;

# 'cat' all files into @arr exept lines starting with "DATE;"
my @arr=grep(! /^DATE;/, <>);

# Field validation
#       /^\d{8}; # Date 8 digits
for (@arr) {  
  if(
     /^(19|20)[90-3][0-9](0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01]); # Date 8 digits
       \d+;  # EventSeq at least one digit
       \d+;  # Number of days, at least one digit
       ([01][0-9]|2[0-3])[0-5][0-9];  # Start time, four digits
       ([01][0-9]|2[0-3])[0-5][0-9];  # End time, four digits
       (19|20)[90-3][0-9](0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01]);  # Last edited date, eight digits
       [^;]*;  # Author email or blank
       [^;]*;  # Author name or blank
       (|[A-Z]{3});  # Three letter capital Currency or blank
       (|\d+(?:[,\.']\d+)?); # Price or blank
       (|[a-z]{2});  # Two letter lowercase language or blank
       [a-z]{2};  # Two letter lowercase continent
       [a-z]{2};  # Two letter lowercase country
       [^;]*;  # City, zero or more characters but not ';'
       [^;]+;  # Organizer, at least one character
       (|[hf]tt?p:\/\/[^;]+);  # Organizer URL or blank
       (|[hf]tt?p:\/\/[^;]+);  # Event URL or blank
       [^;]+;  # Headline, at least one character
       [^;]*  # Body or blank
       $/x) {
    push (@ok,$_);  # save 'ok' lines into @ok
  } else {
    push (@bad,$_);  # save 'bad' lines into @bad
  }
}

# Sort all future events into @future
my @future=sort grep ($_ ge $today,@ok);

# Reverse sort all old events into @past
my @past=reverse sort grep ($_ le $today, @ok);

open(BIGFILE, ">future") or die "Can't start sort out $!";
print BIGFILE @future;
close BIGFILE;

open(BIGFILE, ">past") or die "Can't start sort out $!";
print BIGFILE @past;
close BIGFILE;

open(BIGFILE, ">bad") or die "Can't start sort out $!";
print BIGFILE @bad;
close BIGFILE;

my $i = 0;
my $s;
my $myorg = "SSLUG";
open(SHORTCAL, ">shortcal.html") or die "Can't write $!";
open(SHORTCALS, ">shortcal.shtml") or die "Can't write $!";
open(FRONTPAGECAL, ">frontpagecal.shtml") or die "Can't write $!";
foreach $s (@future) {
  my ($date, $eventseq, $days, $starttime, $endtime, $lastedited,
      $authoremail, $authorname, $currency, $price, $language,
      $continent, $country, $city, $organizer, $organizerurl,
      $eventurl, $headline, $description) = split(/;/,$s);
  if ($myorg eq $organizer) {
    my $short = substr($date,0,4)."-".substr($date,4,2)."-".substr($date,6,2);
    my $shorts = "$short: <a href=\\\"/adict/mgroup.php#$date$eventseq$myorg\\\">$headline</a>";
    $short .= ": <a href=\"/adict/mgroup.php#$date$eventseq$myorg\">$headline</a>";
    print SHORTCALS "<!--#set var=\"Cal$i\" value=\"$shorts\"-->\n";
    if ($i < 5) {
      print SHORTCAL "$short<br>\n";
    }
    if ($i < 4) {
      print FRONTPAGECAL "<li>$short\n";
    }
    $i++;
  }
}
close SHORTCAL;
close SHORTCALS;

sub makehtml{ 
    my ($htmlfile,@array)=@_;
    my $i;
    # Make html file

# Better date representation:
my %months=( '01' => 'Januar', 
             '02' => 'Februar', 
             '03' => 'Marts', 
             '04' => 'April', 
             '05' => 'Maj', 
             '06' => 'Juni', 
             '07' => 'Juli', 
             '08' => 'August', 
             '09' => 'September', 
             '10' => 'Oktober', 
             '11' => 'November', 
             '12' => 'December'
             ); 

    open(HTMLFILE, ">$htmlfile") or die ;
    print HTMLFILE "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
    print HTMLFILE "<html><head><title>Dansk/Svensk Linux Eventkalender</title>";
    print HTMLFILE "<Link Rel='STYLESHEET' Href='/style/sslug.css' Type='text/css'>";
    print HTMLFILE "</head>";
    print HTMLFILE "<body bgcolor='white'>";
    print HTMLFILE "<!--#include virtual=\"/includes/top.html\" -->\n";
    print HTMLFILE "<H1 align=\"center\">Linux Kalender</H1>\n";
    print HTMLFILE "<blockquote>\n";
    
    foreach $i (@array) {
  my ($date, $eventseq, $days, $starttime, $endtime, $lastedited, 
      $authoremail, $authorname, $currency, $price, $language, 
      $continent, $country, $city, $organizer, $organizerurl, 
      $eventurl, $headline, $description) = split(/;/,$i);
  
  my $year=substr($date,0,4);
  my $month=$months{substr($date,4,2)};
  my $day=substr($date,6,2);
  
        $organizerurl =~ s/&/&amp;/g;
        $organizer    =~ s/&/&amp;/g;
        $eventurl     =~ s/&/&amp;/g;
        $headline     =~ s/&/&amp;/g;
        $description  =~ s/&/&amp;/g;

  print HTMLFILE "<p>\n<font size=\"+1\"><em>$day. $month $year: ";
  print HTMLFILE "<a href=\"".$organizerurl."\">".$organizer."</a>\n";
  print HTMLFILE " - <a href=\"".$eventurl."\">".$headline."</a>";
  print HTMLFILE "</em></font><br>\n";
  print HTMLFILE "<em>Fra kl. $starttime til $endtime</em><br>\n";
  print HTMLFILE "$description\n</p>\n";
  
    } # end-foreach-read-line
    
    
    print HTMLFILE "</blockquote>\n";
    
    print HTMLFILE "<!--#include virtual=\"/includes/bottom.html\" -->\n";
    print HTMLFILE "</body></html>";
    
    close(HTMLFILE);
    
}

makehtml ("future.html", @future);
makehtml ("past.html", @past);

