#!/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_configure - prepare and configure Cabalized libraries for building

=cut

use strict;
use Cwd 'getcwd','abs_path';
use File::Find;
use File::Path;
use Debian::Debhelper::Dh_Lib;

use lib '/usr/share/haskell-devscripts/';
#use lib '/home/arjan/checkout/haskell-devscripts/';
use Dh_Haskell;

=head1 SYNOPSIS

B<dh_haskell_configure> [S<I<debhelper options>>]
[B<with-compiler=>I<PATH>]
[B<with-hc-pkg=>I<PATH>]
[B<with-happy=>I<PATH>]
[B<with-alex=>I<PATH>]
[B<with-hsc2hs=>I<PATH>]
[B<with-c2hs=>I<PATH>]
[B<with-cpphs=>I<PATH>]
[B<with-greencard=>I<PATH>]
[B<with-ar=>I<PATH>]
[B<with-haddock=>I<PATH>]
[B<with-pfesetup=>I<PATH>]
[B<with-ranlib=>I<PATH>]
[B<with-runghc=>I<PATH>]
[B<with-runhugs=>I<PATH>]
[B<haddock-args=>I<args>]
[B<pfesetup-args=I<args>]
[B<ranlib-args=I<args>]
[B<runghc-args=I<args>]
[B<runhugs-args=I<args>]

=head1 DESCRIPTION
=cut

my $compiler = "/usr/bin/ghc6";
my @flags;

sub link_builddir {
    my $package = shift;
    my $builddir = builddir($package);

    mkpath($builddir);
    foreach my $file (glob "*") {
	my $abs_path = abs_path($file);
	if ( -d $file ) {
	    unless ($file eq "debian" ) {
		mkdir("$builddir/$file");
		doit("lndir", $abs_path, "$builddir/$file");
	    }
	} else {
	    symlink("$abs_path", "$builddir/$file");
	}
    }
    return $builddir;
}

init();

foreach (@ARGV) {
    if (cabal_version_ge("1.2")) {
	if (m/^configure-option=.*$/) {
	    push @flags , "--$_";
	    next;
	} elsif (m/^\w*-option=.*$/) {
	    push @flags , "--$_";
	    next;
	} elsif (m/^\w*-options=.*$/) {
	    push @flags , "--$_";
	    next;
	}
    }
    if (m/^with-compiler=(.*)$/) {
	error("Compiler '$1' not found.\n") if (! -x $1 );
	$compiler = $1;
    } elsif (m/^with-.*=.*$/) {
	push @flags , "--$_";
    } elsif (m/^.*-args=.*$/) {
	push @flags , "--$_";
    } elsif (m/^(enable|disable)-.*$/){
	push @flags , "--$_";
    } else {
	error("Unrecognized argument: $_\n");
    }
}

build_setup();

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

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

	doit("./setup", "clean");
	foreach my $file (glob ".*config*") {
	    unlink $file or die "Could not remove '$file': $1\n";
	}

	my $builddir = link_builddir($package);
	my $olddir = getcwd;
	chdir($builddir);

	if ($pkgtype eq "hugs") {
	    doit("./setup", "configure", "--hugs", "--prefix=/usr", @flags );
	} elsif ($pkgtype eq "ghc6" || $pkgtype eq "haddock" ) {
	    doit("./setup", "configure",
		 "--prefix=" . getcabalbasepath($pkgtype),
		 "--with-compiler=$compiler", @flags)
		unless (-e ".setup-config");
	} elsif ($pkgtype eq "ghc6-prof") {
	    doit("./setup", "configure", 
		 "--prefix=" . getcabalbasepath($pkgtype),
		 "--with-compiler=$compiler",
		 "--disable-library-vanilla",
		 "--enable-library-profiling", @flags);
	}

	chdir($olddir);
    }
}
