#!/usr/bin/perl -w
#
#
#  rhimport
#
#  Copyright (C) 2009 Christian Pointner <equinox@helsinki.at>
#
#  This file is part of rhimport.
#
#  rhimport 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
#  any later version.
#
#  rhimport 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 rhimport. If not, see <http://www.gnu.org/licenses/>.
#

use strict;
use DBI;
use Date::Calc;
use DateTime::Format::Strptime;
use XML::Feed;
use XML::Feed::Entry;
use XML::Feed::Content;
use XML::Feed::Enclosure;
use URI::URL;
use IO::Handle;
use IPC::Open3;
use HTML::Entities;
use LWP::Simple;

use lib '/usr/local/share/rhimport/';
use rhimport;

my $DBHOST = "airplay";
my $DBUSER = "rivendell";
my $DBPW = "lldriven";
my $DB = "rivendell";

my $STAT_FILE = $ENV{'HOME'} . "/rhimport-wr.stat";
my $RSS_URL = "https://cba.fro.at/series/werkstatt-radio/feed?c=Kfs2IoV2Wmd";
$XML::Feed::MULTIPLE_ENCLOSURES=1;
my $PV_ID = '567';
my $LAST_RUN = 0;

binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");


my $curweek = rhimport::get_rd_week();

if($curweek == 1 || $curweek == 3) {
  if($#ARGV >= 0 && $ARGV[0] eq 'last') {
    print "!!!This is the last attempt, there won't be a retry on error!!!\n";
    $LAST_RUN = 1;
  }
}

my $user = `/usr/bin/id -un`;
$user =~ s/\n//;
my $group = "werkstatt";

my $dbh = DBI->connect("DBI:mysql:$DB:$DBHOST","$DBUSER","$DBPW") or die "Database Error: $DBI::errstr";
my @allowed_dbs = rhimport::get_dropboxes($dbh, $user, $group);

my $idx = 0;
if(scalar(@allowed_dbs) != 1) {
  print "found more or less than 1 Dropbox for this group?!\n";
  $dbh->disconnect();
  exit 1;
}
my $dropbox = $allowed_dbs[$idx]->{'PATH'};
my $to_cart = $allowed_dbs[$idx]->{'TO_CART'};

my @today = Date::Calc::Today();
print "today: " . Date::Calc::Date_to_Text(@today) . " (Week: " . $curweek . ")\n";

my @import_date = Date::Calc::Standard_to_Business(@today);
$import_date[2] = 3;
@import_date = Date::Calc::Business_to_Standard(@import_date);

my $dow = Date::Calc::Day_of_Week(@today);
if($curweek == 1 || $curweek == 3) {
  if($dow > 3) {
    @import_date = Date::Calc::Add_Delta_Days(@import_date, 14);
  }
} else {
  @import_date = Date::Calc::Add_Delta_Days(@import_date, 7);
}
print "day of next Radio Helsinki broadcast: " . Date::Calc::Date_to_Text(@import_date) . "\n";

my @broadcast_date = Date::Calc::Nth_Weekday_of_Month_Year($import_date[0], $import_date[1], 1, 3);
if(Date::Calc::Delta_Days(@broadcast_date, @import_date) <= 0) {
  my @tmp_date = Date::Calc::Add_Delta_YM(@broadcast_date, 0, -1);
  @broadcast_date = Date::Calc::Nth_Weekday_of_Month_Year($tmp_date[0], $tmp_date[1], 1, 3);
}
print "day of latest original broadcast before next Radio Helsinki broadcast: " . Date::Calc::Date_to_Text(@broadcast_date) . "\n";

if(Date::Calc::Delta_Days(@broadcast_date, @today) <= 0) {
  print "File won't be available by now!\n";
  $dbh->disconnect();
  exit 0;
}

my $id = sprintf("%04d-%02d-%02d", @import_date);
my $bd = sprintf("%04d-%02d-%02d", @broadcast_date);

print "looking for files for broadcast date $bd in RSS Feed\n";
print " -> $RSS_URL\n";

my $feed = XML::Feed->parse(URI->new($RSS_URL))
  or die "Error fetching feed: " . XML::Feed->errstr;

my $file = "";
my $out_file = "";
my $i = 0;
for my $entry ($feed->entries) {
  $i++;
  next unless $entry->enclosure;

  my $entry_bdt = $entry->{'entry'}{'https://cba.fro.at'}{'broadcastDate'};
  next unless (defined $entry_bdt);
  my $bdt = DateTime::Format::Strptime::strptime("%a, %d %b %Y %H:%M:%S", $entry_bdt);

  next unless($broadcast_date[0] == $bdt->year && $broadcast_date[1] == $bdt->month && $broadcast_date[2] == $bdt->day);

  my $j = 0;
  for my $enclosure($entry->enclosure) {
    $j++;
    if($enclosure->type eq "audio/mpeg" || $enclosure->type eq "audio/ogg") {
      print "$i/$j: (" . $enclosure->type . ", " . $enclosure->length . ") "  . $enclosure->url . "\n";
      my $fetchurl = $enclosure->url;
      if($fetchurl =~ /^(.*)_cut(\.[^\.]+)$/) {
        $fetchurl = $1 . $2;
      }

      my $url = new URI::URL($fetchurl);
      my @path = $url->path_components;

      my $current_stat = `cat $STAT_FILE`;
      my ($current_id, $current_file) = $current_stat =~ m/^(.*)\n(.*)/;
      if($current_id eq $id  &&  $current_file eq $path[-1]) {
        print "Already downloaded file of today\n";
        $dbh->disconnect();
        exit 0;
      }
      $out_file = $path[-1];
      if(!rhimport::check_file_extension($out_file)) {
        print "\n\nThe extension of the matching file '". $out_file . "' seems to be wrong - manual import necessary!!!\n";
        $dbh->disconnect();
        exit 1;
      }

      print " --> " . $entry_bdt . ": downloading " . $url . " (" . $enclosure->length . " Bytes) .. ";
      $file = "/tmp/" . $out_file;
      system("wget", "--quiet", "--no-check-certificate", $url, "-O", $file);
      if( $! != 0) {
        die "wget returned with error: " . $!;
      }
      print "ok\n";

      last;
    }
  }
  last if $file ne "";
}

if($file eq "" || !(-e "$file")) {
  print "No Entry found from $bd or download error - ";
  if($LAST_RUN) {
    print "giving up, manual import necessary!!!\n";
  } else {
    print "will retry later\n";
  }
  exit 1;
}

print "will import $file to dropbox $dropbox (cart=$to_cart)\n";

my $error_cb = sub {
  my ($text) = @_;

  print "\n$text";
  return 0;
};

rhimport::check_key_file() or die "Import Key not found, use rhimport-create-id to create one\n";

my $ret;
my $log = rhimport::clear_carts($dbh, $group, $to_cart);
my $import_log;
($ret, $import_log) = rhimport::import_single($file, $dropbox, $user, 0, $error_cb);
$log .= $import_log;

$dbh->disconnect();

unlink $file;

if(!$ret) {
  print "\nImport Error:\n";
  print $log;
  exit 1;
}

unlink($STAT_FILE);
open(my $fhs, '>', $STAT_FILE);
print $fhs "$id\n$out_file";
close($fhs);

exit 0;