#!/usr/bin/perl

# build-lives-rfx-plugin-multi - Copyright G. Finch (salsaman) 2005 - 2013
# Released under the LGPL 3 or later - see file COPYING.LGPL or www.gnu.org for details

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

my $smogcmd = "smogrify";

## TODO - should be passed as args
#my $home_dir_extra = "/.lives-dir/plugins/effects/";
my $home_dir_extra = "/.local/share/lives/plugins/effects/";
my $dir_extra = "/lives/plugins/effects/";


my $builder = "build-lives-rfx-plugin";

my $type="builtin"; # default

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

my $scripts_dir = "";
my $exec_dir = "";
my $build_bindir = "";
my $builder_location;

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

if (defined $ARGV[1] && $ARGV[1] ne "") {
    $scripts_dir = $ARGV[1];
}

if ($scripts_dir eq "") {
    print "Need scripts_dir. Exiting.\n";
    exit 1;    # check we have all
}

if (defined $ARGV[2] && $ARGV[2] ne "") {
    $exec_dir = $ARGV[2];
}

if ($exec_dir eq "") {
    print "Need exec_dir. Exiting.\n";
    exit 2;    # check we have all
}

if (defined $ARGV[3] && $ARGV[3] ne "") {
    $build_bindir = $ARGV[3];
}

if ($build_bindir eq "") {
    print "Need build_bindir. Exiting.\n";
    exit 3;    # check we have all
}

if ($type eq "builtin") {
    $scripts_dir .= $dir_extra;
    $indir = "$scripts_dir/RFXscripts/";
    unlink glob "$indir/*~";
    $exec_dir .= $dir_extra;
    $outdir = "$exec_dir/rendered/";
}

elsif ($type eq "custom") {
    $scripts_dir .= $home_dir_extra;
    $indir = "$scripts_dir/RFXscripts/custom/";
    unlink glob "$indir/*~";
    $exec_dir .= $home_dir_extra;
    $outdir = "$exec_dir/rendered/custom/";
}

elsif ($type eq "test") {
    $scripts_dir .= $home_dir_extra;
    $indir = "$scripts_dir/RFXscripts/test/";
    unlink glob "$indir/*~";
    $exec_dir .= $home_dir_extra;
    $outdir = "$exec_dir/rendered/test/";
}

elsif ($type eq "builtinx") {
    $indir = $scripts_dir;
    $outdir = $exec_dir;
    unlink glob "$indir/*~";
}

else {
    print "error: unknown RFX type\n";
    exit 1;
}

print "Deleting scripted plugins from $outdir\n";
&remove_scripted_plugins($outdir);

print "Building all in $indir\n";

if ($^O eq "MSWin32") {
    mkpath("$outdir");
} else {
    `/bin/mkdir -p $outdir`;
}

opendir DIR,$indir;
while ($plugin = readdir(DIR)) {
    unless ($plugin =~ /^\./) {
	print "building $indir$plugin \n";
	if (defined $build_bindir) {
	    if ($^O eq "MSWin32") {
		system("perl \"$build_bindir\\$builder\" \"$indir$plugin\" \"$outdir\"");
	    } else {
		system("\"$build_bindir/$builder\" \"$indir$plugin\" \"$outdir\"");
	    }
	}
	else {
	    if ($^O eq "MSWin32") {
		system("perl \"$builder\" \"$indir$plugin\" \"$outdir\"");
	    } else {
		system("$builder \"$indir$plugin\" \"$outdir\"");
	    }
	}
    }
}
close DIR;

if ($type eq "test" || $type eq "custom") {
    rmdir $outdir;
}

exit 0;

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

sub remove_scripted_plugins {
    # remove only plugins with properties&0x8000, and "broken" plugins
    my ($outdir)=@_;
    my $file;
    my $cmd;

    if ($^O eq "MSWin32") {
	# filter by file ext
	my $ext=&get_ext("$plugin");
	if ($ext eq ".py") {
	    $cmd="python";
	}
	else {
	    $cmd="perl";
	}
    }
    else {
	$cmd="";
    }

    opendir DIR,$outdir;
    while ($file=readdir(DIR)) {
	unless ($file =~ /^\./) {
	    if (-x "$outdir$file" || $^O eq "MSWin32") {
		my ($fx_caps)=`$cmd \"$outdir$file\" get_capabilities`;
		if ($fx_caps&0x8000||$fx_caps eq ""||$? != 0) {
		    unlink "$outdir$file";
		}
	    }
	}
    }
    closedir DIR;
}


sub location {
    # return the location of an executable
    my ($command)=@_;
    if ($^O eq "MSWin32") {
	return "$command.exe";
    }
    my ($location)=`which $command 2>/dev/null`;
    chomp($location);
    $location;
}


sub get_ext {
    my $fname=shift;

    my $ext=(split(/\./,$fname))[-1];

    if ($ext=~/(.*)\"$/) {
	$ext=$1;
    }

    return ".".$ext;
}

