#!/usr/bin/perl -w
# arch-tag: Debhelper script for Haskell
#
# Copyright (C) 2004-2006 John Goerzen <jgoerzen@complete.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

=head1 NAME

dh_haskell_prep - prepare Cabalized libraries for building

=cut

use strict;
use File::Find;
use Debian::Debhelper::Dh_Lib;

use lib '/usr/share/haskell-devscripts/';
use Dh_Haskell;

=head1 SYNOPSIS

B<dh_haskell_prep> [S<I<debhelper options>>]

=head1 DESCRIPTION

dh_haskell_prep is a debhelper program that helps with preparing Haskell
libraries.

It automates building libraries for the different supported Haskell systems in
Debian.

=cut

init();

print "Generating meta-information...\n";

foreach my $package (@{$dh{DOPACKAGES}}) {
	my $tmp = tmpdir($package);


	if (is_handled_package($package)) {
	    # substitute ${haskell:Depends}
	    my $pkgtype = type_of_package($package);
	    delsubstvar($package, "haskell:Depends");
            if ($pkgtype ne "haddock") {
                addsubstvar($package, "haskell:Depends",
                            $pkgtype, ">= " . upstream_version(version_of_type($pkgtype)));
            }
	    if (! ($pkgtype eq "hugs")) {
		addsubstvar($package, "haskell:Depends",
			    $pkgtype, "<< " . upstream_version(version_of_type($pkgtype)) . "+");
	    }

	    # add postinst/prerm scripts
	    if ($pkgtype eq "ghc6") {
		# Build scripts
		my $ghcver = "ghc-" . upstream_version(version_of_type($pkgtype));
		my $ghclibdir = `/usr/bin/ghc6 --print-libdir` or die "Could not determine  GHC library directory: $!";
		my $pkglibdir = getcabalpkglibpath($pkgtype);
		my $cabalname = getcabalname();
		my $cabalversion = getcabalversion();

		chomp $ghclibdir;

		print "$ghcver $pkglibdir $cabalname $cabalversion\n";
		autoscript($package,"postinst","postinst-ghc",
			   "s%#GHCLIBDIR#%$ghclibdir%;s%#PKGLIBDIR#%$pkglibdir%;s%#CABALNAME#%$cabalname%;s%#CABALVERSION#%$cabalversion%");
		autoscript($package,"prerm","prerm-ghc",
			   "s%#GHCLIBDIR#%$ghclibdir%;s%#PKGLIBDIR#%$pkglibdir%;s%#CABALNAME#%$cabalname%;s%#CABALVERSION#%$cabalversion%");
	    }
	}

	if (type_of_package($package) eq "ghc6-prof") {
	    # substitute ${haskell:Depends} for profiling package
	    my $pkgtype = type_of_package($package);
	    delsubstvar($package, "haskell:Depends");
	    addsubstvar($package, "haskell:Depends",
			$pkgtype, ">= " .
			upstream_version(version_of_type($pkgtype)));
	    addsubstvar($package, "haskell:Depends",
			$pkgtype, "<< " .
			upstream_version(version_of_type($pkgtype)) . "+");
	    # Call isnative becuase it sets $dh{VERSION}
	    # as a side effect.
	    isnative($package);
	    addsubstvar($package, "haskell:Depends",
			dev_name($package), "= " . $dh{VERSION});

	}
}

=head1 BUGS

hugs and ghc6 are the only supported targets at the moment.  Cabal does
not yet support nhc98.

=head1 SEE ALSO

L<dh_haskell(1)>
L<dh_haskell_configure(1)>
L<dh_haskell_build(1)>
L<dh_haskell_install(1)>
L<dh_haskell_buildinst(1)>
L<dh_haskell_depends(1)>
L<dh_haskell_clean(1)>
L<debhelper(7)>

=head1 AUTHOR

John Goerzen <jgoerzen@complete.org>

Based on ideas in dh_python by Josselin Mouette <joss@debian.org>
=cut
