#! /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 @pkgs;
my @ignored = ('ghc', 'mtl', 'terminfo', 'haskeline', 'utf8-string', 'xhtml');
while (<PKG>) {
    y/A-Z/a-z/;
    my $pkgstring = $_;
    LOOP: while ($pkgstring =~ m,([^ ]*?)-\d.*? ?,g) {
	my $pkg = $1;
	foreach (@ignored, @pkgs) {
	    next LOOP if $_ eq $pkg;
	}
	push @pkgs, $1;
    }
}
close PKG;

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

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