#!/usr/bin/perl -w
# arch-tag: Debhelper script for Haskell
#
# Copyright (C) 2004-2006 John Goerzen <jgoerzen@complete.org>
#           (C) 2006-2007 Arjan Oosting <arjan@debian.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_build - builds Cabalized libraries

=cut

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

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

=head1 SYNOPSIS

B<dh_haskell_build> [S<I<debhelper options>>]
[B<option=>I<option>]
[B<options>I<option>]
[B<html-location=>I<PATH>]
[B<css=>I<PATH>]
[B<hscolour-css=>I<PATH>]
[B<executable>]
[B<hyperlink-source>]

=head1 DESCRIPTION

dh_haskell_build is a debhelper program that helps with building Haskell
libraries.

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

=cut

my @buildflags;
my @haddockflags;
my @savedargv = @ARGV;

init();

foreach (@ARGV) {
    if (cabal_version_ge("1.2")) {
	if (m/^\w*-option=.*$/) {
	    push @buildflags , "--$_";
	    next;
	} elsif (m/^\w*-options=.*$/) {
	    push @buildflags , "--$_";
	    next;
	} elsif (m/^html-location=.*$/){
	    push @haddockflags , "--$_";
	    next;
	} elsif (m/^executables$/) {
	    push @haddockflags , "--$_";
	    next;
	} elsif (m/^css=.*$/) {
	    push @haddockflags , "--$_";
	    next;
	} elsif (m/^hyperlink-source$/) {
	    push @haddockflags , "--$_";
	    next;
	} elsif (m/^hscolour-css=.*$/) {
	    push @haddockflags , "--$_";
	    next;
	}
    }
    if (m/^hoogle$/) {
	push @haddockflags , "--$_";
    } else {
	error("Unrecognized argument: $_\n");
    }
}

foreach my $package (@{$dh{DOPACKAGES}}) {
    my $tmp = tmpdir($package);
    if (is_handled_package($package)) {
	my $pkgtype = type_of_package($package);
	my $builddir = builddir($package);

	print "\n ****************************************** \n";
	print " BUILDING $package FOR $pkgtype";
	print "\n ****************************************** \n\n";

	if (! -e "$builddir/.setup-config" &&
	    ! -e "$builddir/dist/setup-config" ) {
	    print "Running missing dh_haskell_configure call...\n";
	    doit ("dh_haskell_configure", @savedargv);
	    print "\n ****************************************** \n";
	    print " CONTINUE BUILDING $package FOR $pkgtype";
	    print "\n ****************************************** \n\n";
	}


	my $olddir = getcwd();
	chdir($builddir);
	if ( $pkgtype eq "haddock" ) {
	    doit("./setup", "haddock", @haddockflags);
	} else {
	    doit("./setup", "build", @buildflags);
	}
	chdir($olddir);
    }
}

=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_prep(1)>
L<dh_haskell_configure(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
