#!/usr/bin/perl

# (c) G. Finch (salsaman)

# released under the GNU GPL 3 or later
# see file COPYING or www.gnu.org for details

#######################################################################
# LiVES pdf plugin v1.4

# version 1.0 original - salsaman
# version 1.1 - add support for png, correct requires error text
# version 1.2 - add support for Win32
# version 1.3 - more verbose
# version 1.4 api update and add skip param
#######################################################################

my $USE_STRICT = 1;
if ($USE_STRICT) {
    use strict;
}

my $USE_WARNINGS = 1;
if ($USE_WARNINGS) {
    use warnings;
}

use POSIX;     # Needed for setlocale()
setlocale(LC_NUMERIC, "C");

my $nulfile;
my $exe;

if ($^O eq "MSWin32") {
    $nulfile = "NUL";
    $exe = ".exe";
} else {
    $nulfile = "/dev/null";
    $exe = "";
}

if (!caller) {
    our $command;
    #our $otype;
    exit 0 if (!defined($ARGV[0]));
}

if (!defined($command)) {
    $command  = $ARGV[0];
}


#######################################################################

if ($command eq "version") {
    print "pdf encoder plugin v1.4\n";
    exit 0;
}


if ($command eq "init") {
    # perform any initialisation needed
    # On error, print error message and exit 1
    # otherwise exit 0

    if (&location("convert") eq "") {
	print "Convert was not found. Please install imagemagick and try again.";
	exit 1;
    }
    if (($^O ne "MSWin32" && &location("gs") eq "") || ($^O eq "MSWin32" && &location("gswin32.exe") eq "")) {
	print "Ghostscript was not found. Please install ghostscript and try again.";
	exit 1;
    }
    
    # end init code
    print "initialised\n";
    exit 0;
}


if ($command eq "get_capabilities") {
    # return capabilities - this is a bitmap field
    # bit 0 - takes extra parameters (using RFX request)
    # bit 1 - unused
    # bit 2 - can encode png
    # bit 3 - not pure perl
    print "5\n";
    exit 0;
}


if ($command eq "get_formats") {
   # for each format: -
   # return format_name|display_name|audio_types|restrictions|extension|

   # audio types are: 0 - cannot encode audio, 1 - can encode using
    #  mp3, 2 - can encode using pcm, 3 - can encode using pcm and mp3
    
    print "pdf|pdf|0|none|pdf|converted|\n";
    exit 0;
}


if ($command eq "get_format_request") {
    # return the code for how we would like audio and video delivered
    # this is a bitmap field composed of:
    # bit 0 - unset=raw pcm audio; set=pcm audio with wav header
    # bit 1 - unset=all audio; set=clipped audio
    # bit 2 - unset=all frames; set=frames for selection only

    print 0; # clipped wav, clipped frames
    exit 0;
}


if ($command eq "get_rfx") {
    # mandatory section
    print "<define>\n";
    print "|1.7\n";
    print "</define>\n";

    # mandatory section
    print "<language_code>\n";
    print "0xF0\n";
    print "</language_code>\n";

    # optional section
    print "<params>\n";
    print "skip|I will take a snapshot every  |num0|50|1|1000|\n";
    print "</params>\n";

    print "<param_window>\n";
    print "layout|\"I can make a pdf from your video clip !\"|\n";
    print "layout|p0|\" frames.\"|\n";
    print "</param_window>\n";

    exit 0;
}


if ($command eq "encode") {
    # encode

    my $skip = $ARGV[13];

    for ($i = $start; $i <= $end; $i+= $skip) {
	$name=&mkname($i);
	`convert $name$img_ext $name.pdf`;
	print STDERR "converted $i\n";
    }

    if ($^O eq "MSWin32") {
	$gscom="gswin32.exe";
    }
    else {
	$gscom="gs";
    }
    
    $syscom="$gscom -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=\"$nfile\" *.pdf";

    if (defined($DEBUG_ENCODERS)) {
	print STDERR "Debug: pdf_encoder command is $syscom\n";
    }

    system($syscom);
}


if ($command eq "clear") {
    # this is called after "encode"
    # note that encoding could have been stopped at any time

    for ($i=$start; $i <= $end; $i++) {
	$name=&mkname($i);
	if (-f "$name.pdf") {
	    unlink "$name.pdf";
	}
    }
    exit 0;
}


if ($command eq "finalise") {
    # do any finalising code

    # ...

    # end finalising code
    print "finalised\n";
    exit 0;
}


###### subroutines #######

sub location {
    # return the location of an executable
    my ($command)=shift;

    if ($^O eq "MSWin32") {
	return "$command.exe";
    }

    my ($location) = `which \"$command\" 2>$nulfile`;
    chomp($location);

    $location;
}


sub mkname {
    sprintf("%08d", shift);
}


##### required
1;
