#!/usr/bin/perl -w

use strict;
use warnings;

use File::Basename;
use File::Temp qw/tempdir/;
use SReview::Talk;
use SReview::Config::Common;
use SReview::Normalizer;
use SReview::Video;
use SReview::Video::ProfileFactory;
use SReview::Videopipe;
use SReview::Files::Factory;
use Getopt::Long;
use Pod::Usage;
use Mojo::JSON qw/true/;

my $inputfile = undef;
my $talkid = undef;
my $talknonce = undef;
my $talkslug = undef;
my $help = undef;
my $audionormal = undef;

GetOptions("t|talkid=i" => \$talkid,
           "n|nonce=s" => \$talknonce,
           "s|slug=s" => \$talkslug,
           "i|input=s" => \$inputfile,
	   "a|audionormal" => \$audionormal,
           "h|help" => \$help) or pod2usage("command line invalid");

if($help) {
    pod2usage(0);
}

die "require an input file name\n" unless defined($inputfile);
die "Require exactly one of a nonce, a talk ID, or a talk slug\n" unless scalar(grep({defined}($talkid, $talknonce, $talkslug))==1);

my $config = SReview::Config::Common::setup();
my $talk;
if(defined($talknonce)) {
    $talk = SReview::Talk->by_nonce($talknonce);
} elsif(defined($talkid)) {
    $talk = SReview::Talk->new(talkid => $talkid);
} elsif(defined($talkslug)) {
    $talk = SReview::Talk->by_slug($talkslug);
}
$talk->set_state("injecting", "running");
$talk = SReview::Talk->new(talkid => $talkid);
my $input = SReview::Video->new(url => $inputfile);
if($audionormal) {
	my $dirname = tempdir("injectXXXXXX", DIR => $config->get("workdir"), CLEANUP => 1);
	my $normalized = SReview::Video->new(url => join("/", $dirname, basename($inputfile)));
	SReview::Normalizer->new(input => $input, output => $normalized)->run();
	$input = $normalized;
}
my $profile = SReview::Video::ProfileFactory->create($config->get("input_profile"), $input);
my $output_coll = SReview::Files::Factory->create("intermediate", $config->get("pubdir"));
my $outputfile = $output_coll->add_file(relname => $talk->relative_name . ".mkv");
SReview::Videopipe->new(inputs => [$input], output => SReview::Video->new(url => $outputfile->filename, reference => $profile), vcopy => 0, acopy => 0)->run();
$outputfile->store_file;
$talk->set_flag('is_injected' => true);
$talk->done_correcting;
$talk->state_done("injecting");
