#!/usr/bin/perl -w

use lib '../../pdb';

require 'util.pl';

*write_file = \&Pika::CodeGen::util::write_file;
*FILE_EXT   = \$Pika::CodeGen::util::FILE_EXT;

$destdir = ".";
$builddir = ".";

$ignorefile = ".gitignore";
$rcfile     = "pikarc.common";

$outmk = "$builddir/Makefile.am$FILE_EXT";
$outignore = "$builddir/$ignorefile$FILE_EXT";
$outrc = "$builddir/$rcfile$FILE_EXT";

open MK, "> $outmk";
open IGNORE, "> $outignore";
open RC, "> $outrc";

require './plugin-defs.pl';

$bins = ""; $opts = ""; $dirs = "";

foreach (sort keys %plugins) {
    my $makename = $_;
    $makename =~ s/-/_/g;

    if (exists $plugins{$_}->{optional}) {
	$bins .= "${makename}_libexec_PROGRAMS = \$(\U$makename\E)\n";
	$opts .= "\t$_ \\\n";
    }
    else {
	$bins .= "${makename}_libexec_PROGRAMS = $_\n";
    }

    $dirs .= "${makename}_libexecdir = \$(pikaplugindir)/plug-ins/$_\n";
}

$extra = "";
foreach (@extra) { $extra .= "\t$_\t\\\n" }
if ($extra) {
    $extra =~ s/\t\\\n$//s;
    $extra = "\t\\\n$extra";
}

foreach ($bins, $opts) { s/ \\\n$//s }

print MK <<EOT;


## ---------------------------------------------------------
## This file is autogenerated by mkgen.pl and plugin-defs.pl
## ---------------------------------------------------------

## Modify those two files instead of this one; for most
## plug-ins you should only need to modify plugin-defs.pl.

if OS_WIN32
mwindows = -mwindows
else
libm = -lm
endif

if PLATFORM_OSX
xobjective_c = "-xobjective-c"
framework_cocoa = -framework Cocoa
endif

if HAVE_WINDRES
include \$(top_srcdir)/build/windows/pikarc-plug-ins.rule
include $rcfile
endif

libpika = \$(top_builddir)/libpika/libpika-\$(PIKA_API_VERSION).la
libpikabase = \$(top_builddir)/libpikabase/libpikabase-\$(PIKA_API_VERSION).la
libpikacolor = \$(top_builddir)/libpikacolor/libpikacolor-\$(PIKA_API_VERSION).la
libpikaconfig = \$(top_builddir)/libpikaconfig/libpikaconfig-\$(PIKA_API_VERSION).la
libpikamath = \$(top_builddir)/libpikamath/libpikamath-\$(PIKA_API_VERSION).la \$(libm)
libpikamodule = \$(top_builddir)/libpikamodule/libpikamodule-\$(PIKA_API_VERSION).la
libpikaui = \$(top_builddir)/libpika/libpikaui-\$(PIKA_API_VERSION).la
libpikawidgets = \$(top_builddir)/libpikawidgets/libpikawidgets-\$(PIKA_API_VERSION).la


AM_LDFLAGS = \$(mwindows)

EXTRA_DIST = \\
	mkgen.pl	\\
	plugin-defs.pl$extra	\\
	$rcfile

AM_CPPFLAGS = \\
	-I\$(top_srcdir)	\\
	\$(GTK_CFLAGS)	\\
	\$(GEGL_CFLAGS)	\\
	-I\$(includedir)

$dirs

$bins

EXTRA_PROGRAMS = \\
$opts

install-\%: \%
	\@\$(NORMAL_INSTALL)
	\$(mkinstalldirs) \$(DESTDIR)\$(pikaplugindir)/plug-ins/\$<
	\@p=\$<; p1=`echo \$\$p|sed 's/\$(EXEEXT)\$\$//'`; \\
	if test -f \$\$p \\
	   || test -f \$\$p1 \\
	; then \\
	  f=`echo "\$\$p1" | sed 's,^.*/,,;\$(transform);s/\$\$/\$(EXEEXT)/'`; \\
	  echo " \$(INSTALL_PROGRAM_ENV) \$(LIBTOOL) --mode=install \$(INSTALL_PROGRAM) \$\$p \$(DESTDIR)\$(pikaplugindir)/plug-ins/\$\$p/\$\$f"; \\
	  \$(INSTALL_PROGRAM_ENV) \$(LIBTOOL) --mode=install \$(INSTALL_PROGRAM) \$\$p \$(DESTDIR)\$(pikaplugindir)/plug-ins/\$\$p/\$\$f || exit 1; \\
	else :; fi
EOT

print IGNORE <<EOT;
/.deps
/.libs
/Makefile
/Makefile.in
EOT

foreach (sort keys %plugins) {
    my $makename = $_;
    $makename =~ s/-/_/g;

    my $libpika = "";

    if (exists $plugins{$_}->{ui}) {
        $libpika .= "\$(libpikaui)";
        $libpika .= "\t\t\\\n\t\$(libpikawidgets)";
	$libpika .= "\t\\\n\t\$(libpikamodule)";
	$libpika .= "\t\\\n\t";
    }

    $libpika .= "\$(libpika)";
    $libpika .= "\t\t\\\n\t\$(libpikamath)";
    $libpika .= "\t\t\\\n\t\$(libpikaconfig)";
    $libpika .= "\t\\\n\t\$(libpikacolor)";
    $libpika .= "\t\t\\\n\t\$(libpikabase)";

    my $glib;
    if (exists $plugins{$_}->{ui}) {
	$glib = "\$(GTK_LIBS)\t\t\\";
    } else {
	$glib = "\$(CAIRO_LIBS)\t\t\\\n\t\$(GDK_PIXBUF_LIBS)\t\\";

	if (exists $plugins{$_}->{gio} &&
	    ! exists $plugins{$_}->{gegl}) {
	    $glib .= "\n\t\$(GIO_LIBS)\t\t\\";
	}
    }

    if (exists $plugins{$_}->{gegl}) {
	$glib .= "\n\t\$(GEGL_LIBS)\t\t\\";
    }

    my $optlib = "";

    if (exists $plugins{$_}->{libs}) {
		$optlib = "\n\t\$(" . $plugins{$_}->{libs} . ")\t\t\\";
    }

    if (exists $plugins{$_}->{ldflags}) {
	my $ldflags = $plugins{$_}->{ldflags};

	print MK <<EOT;

${makename}_LDFLAGS = $ldflags
EOT
    }

    if (exists $plugins{$_}->{cflags}) {
	my $cflags = $plugins{$_}->{cflags};
	my $cflagsvalue = $cflags =~ /FLAGS/ ? "\$($cflags)" : $cflags;

	print MK <<EOT;

${makename}_CFLAGS = $cflagsvalue
EOT
    }

    if (exists $plugins{$_}->{cppflags}) {
	my $cppflags = $plugins{$_}->{cppflags};

	print MK <<EOT;

${makename}_CPPFLAGS = $cppflags
EOT
    }

    my $deplib = "\$(RT_LIBS)\t\t\\\n\t\$(INTLLIBS)";
    if (exists $plugins{$_}->{libdep}) {
	my @lib = split(/:/, $plugins{$_}->{libdep});
	foreach $lib (@lib) {
	    $deplib = "\$(\U$lib\E_LIBS)\t\t\\\n\t$deplib";
	}
    }

    my $rclib = "\$(${makename}_RC)";

    print MK <<EOT;

${makename}_SOURCES = \\
	$_.c

${makename}_LDADD = \\
	$libpika		\\
	$glib$optlib
	$deplib		\\
	$rclib
EOT

     print RC <<EOT;
${makename}_RC = $_.rc.o
EOT

    print IGNORE "/$_\n";
    print IGNORE "/$_.exe\n";
}

close RC;
close MK;
close IGNORE;

&write_file($outmk, $destdir);
&write_file($outignore, $destdir);
&write_file($outrc, $destdir);

