#! /usr/bin/perl

-x 'inplace/bin/ghc-pkg' or die "inplace ghc-pkg not executable or present";

open PKG, 'inplace/bin/ghc-pkg list --simple-output |'
    or die "ghc-pkg list failed: $!";

my @ignored = ('ghc', 'mtl', 'transformers', 'terminfo', 'haskeline', 'utf8-string', 'xhtml', 'rts');
my %ignored;
$ignored{$_}++ for @ignored;

my @no_conflict = ('cabal', 'binary');
my %no_conflict;
$no_conflict{$_}++ for @no_conflict;

my @pkgs;
while (<PKG>) {
    y/A-Z/a-z/;
    my $pkgstring = $_;
    LOOP: while ($pkgstring =~ m,([^ ]*?)-\d.*? ?,g) {
	my $pkg = $1;
	next if exists $ignored{$pkg};
	push @pkgs, $1;
    }
}
close PKG;

my $buf;
open DEV, '>debian/ghc.substvars';
$buf = "provided-devs=";
foreach (sort @pkgs) {$buf .= "libghc-$_-dev, ";}
$buf =~ s#(.*), #$1#;
print DEV $buf."\n";

$buf = "conflicting-devs=";
foreach (sort @pkgs) {
	next if $no_conflict{$_};
	$buf .= "libghc-$_-dev, ";
}
$buf =~ s#(.*), #$1#;
print DEV $buf."\n";
close DEV;

open PROF, '>debian/ghc-prof.substvars';
print PROF 'provided-profs=';
$buf = "";
foreach (@pkgs) {$buf .= "libghc-$_-prof, ";}
$buf =~ s#(.*), #$1#;
print PROF $buf."\n";
close PROF;

open DOC, '>debian/ghc-doc.substvars';
print DOC 'provided-docs=';
$buf = "";
foreach (@pkgs) {$buf .= "libghc-$_-doc, ";}
$buf =~ s#(.*), #$1#;
print DOC $buf."\n";
close DOC;
