#!/usr/bin/perl -w

use strict;
use warnings;

use DBI;
use Mojo::Template;
use SReview::Config::Common;
use SReview::Video::ProfileFactory;

my $config = SReview::Config::Common::setup;

my $dbh = DBI->connect($config->get('dbistring'), '', '') or die "Cannot connect to database!";

my $talkid = $ARGV[0];

$dbh->prepare("UPDATE talks SET progress='running' WHERE id=?")->execute($talkid);

my $mt = Mojo::Template->new;
$mt->vars(1);

sub run_command($$) {
	my $file = shift;
	my $relative = shift;

	foreach my $command(@{$config->get('upload_actions')}) {
		my @run;
		foreach my $component(@$command) {
			my $rendered = $mt->render($component, {file => $file, relative_file => $relative });
			chomp($rendered);
			push @run, $rendered;
		}
		system(@run);
	}
}

my $actions = $config->get('upload_actions');

exit 0 if(scalar(@$actions) < 1);

my $raw_file = $dbh->prepare("SELECT filename FROM raw_files JOIN talks ON raw_files.room = talks.room JOIN events ON talks.event = events.id WHERE events.name = ? LIMIT 1");
$raw_file->execute($config->get("event"));
$raw_file = $raw_file->fetchrow_hashref();
$raw_file = SReview::Video->new(url => $raw_file->{filename});
my $talk = $dbh->prepare("SELECT event, room, room_output, starttime::date AS date, to_char(starttime, 'yyyy') AS year, name AS title, subtitle, slug FROM talk_list WHERE id = ?");
$talk->execute($talkid);
my $talkdata = $talk->fetchrow_hashref;
my $outputdir = $config->get('outputdir');
my $subdirs = $config->get('output_subdirs');
my $reldir = "";

foreach my $subdir(@$subdirs) {
	$reldir = join('/', $reldir, $talkdata->{$subdir});
}

foreach my $profile(@{$config->get('output_profiles')}) {
	my $basename = join('.', $talkdata->{slug}, SReview::Video::ProfileFactory->create($profile, $raw_file)->exten());
	my $relfile = join('/', $reldir, $basename);
	my $fullfile = join('/', $outputdir, $relfile);
	run_command($fullfile, $relfile);
}

$dbh->prepare("UPDATE talks SET progress='done' WHERE id = ?")->execute($talkid);
