#!/usr/bin/perl

use strict;
use warnings;

use YAML;
use Getopt::Std;

our $opt_e;
getopts('e') or die;

sub pdeps {
    while(@_) {
        for(my $i = 0; $i < 5; $i++) {
            last unless @_;
            print " ", shift;
        }
        print " \\\n   " if @_;
    }
    print "\n";
}

my @targets;

foreach (@ARGV) {
    my $y = YAML::LoadFile($_);
    next unless exists $y->{Target};
    my @deps;
    @deps = values %{$y->{ModuleSource}} if !$opt_e;
    push @deps, values %{$y->{LibraryDeps}} if exists $y->{LibraryDeps};
    if (exists $y->{LibraryDesc}) {
        my @ld = @{$y->{LibraryDesc}};
        map { s/^tmp\/build\/[^\/]*/lib\/ext/ } @ld;
        @deps = (@ld, @deps);
    }
    map { s/^\.\/// } @deps;
    foreach (@deps) {
        my $patch = $_;
        push @deps, $patch if $patch =~ s/\.(cabal|yaml)(.m4)?$/.patch/ and -e $patch;
    }
    push @targets, @{$y->{Target}};
    for (@{$y->{Target}}) {
        print "$_:";
        pdeps(@deps);
        print "\tperl utils/build_extlibs.prl \$<\n" if $opt_e;
        print "\t./jhc \$(LIB_OPTIONS) --build-hl \$< -o \$@\n" if !$opt_e;
    }
}
print ($opt_e ? "JHC_EXT_LIBS =" : "JHC_LIBS =") if @targets;
pdeps(@targets) if @targets;
