#! /usr/bin/perl

-x 'utils/ghc-pkg/dist-inplace/build/ghc-pkg/ghc-pkg' or die "inplace ghc-pkg not executable or present";

open PKG, 'utils/ghc-pkg/dist-inplace/build/ghc-pkg/ghc-pkg \
list --global-conf inplace-datadir/package.conf --simple-output |'
    or die "ghc-pkg list failed: $!";
my @pkgs;
my @ignored = ('ghc', 'ghc-mtl', 'utf8-string', 'terminfo', 'haskeline');
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/ghc6.substvars';
print DEV 'provided-devs=';
my $buf = "";
foreach (@pkgs) {$buf .= "libghc6-$_-dev, ";}
$buf =~ s#(.*), #$1#;
print DEV $buf."\n";
close DEV;

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

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