#!/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
#

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

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

my $haddockpkg;
my $cabalname = getcabalname();
my $cabalnameversion = getcabalnameversion();

sub remove_emptydir {
    my $dir = $_;

    if (-d $dir) {
	opendir DH, $dir or die "Cannot open '$dir' : $!";
	foreach my $file (readdir DH) {
	    next if $file eq "." or $file eq "..";
	    return;
	}
	print "Removing empty directory $File::Find::name\n";
	rmdir $File::Find::name or die "Cannot remove 'dir': $!";
    }
}

sub package_description {
    if (cabal_version_ge("1.2")) {
	doit("./setup", "register" , "--gen-pkg-config");
	return $cabalnameversion . ".conf";
    } else {
	return ".installed-pkg-config" ;
    }
}

init();

build_setup();

foreach my $pkg (&getpackages("indep")) {
    if (&is_handled_package($pkg) &&
	&type_of_package($pkg) eq "haddock") {
	$haddockpkg = $pkg
    }
}

foreach my $package (@{$dh{DOPACKAGES}}) {
    my $tmp = getcwd() ."/" . tmpdir($package);
    if (is_handled_package($package)) {
	my $pkgtype = type_of_package($package);

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

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

	if ($pkgtype eq "hugs") {
	    mkpath("$tmp/usr/lib/hugs/packages/$cabalname");
	    doit("./setup", "copy", "--destdir=$tmp");
	    # remove LICENSE file and empty directories
	    doit("rm", "-f", "$tmp/usr/share/doc/$cabalnameversion/LICENSE");
	    finddepth({wanted => \&remove_emptydir}, "$tmp/usr/share/doc");
	} elsif ($pkgtype eq "ghc6") {
	    mkpath("$tmp" . getcabalbasepath($pkgtype));
	    doit("./setup", "copy", "--destdir=$tmp");
	    # Remove installed docs as the go into a separete package
	    rmtree("$tmp/usr/lib/haskell-packages/ghc6/share/$cabalnameversion/doc/");
	    rmtree("$tmp/usr/lib/haskell-packages/ghc6/share/doc/$cabalnameversion/");
	    # Remove empty directories from package
	    finddepth({wanted => \&remove_emptydir}, "$tmp/usr/lib");

	    my $inconfig = package_description();

	    open INCONFIG , "<", $inconfig or
		die "Cannot read $inconfig: $!";
	    open OUTCONFIG, ">", "$tmp" .
		getcabalpkglibpath($pkgtype) . "/installed-pkg-config"
		or die "Cannot write installed-pkg-config: $!";
	    while (<INCONFIG>) {
		chomp;
		if (m%(\w+-dirs):\s+(.*)%) {
		    $_ = "$1:";
		    foreach my $dir (split(/\s+/, $2)) {
			if ( -d "$tmp$dir" ) {
			    $_ .= " $dir" ;
			} else {
			    print "Removing non-existing directory " .
				$dir . " from $1 field";
			}
		    }
		}
		if (defined($haddockpkg)) {
		    if(cabal_version_ge(1.2)) {
			s%(haddock-interfaces:).*/doc/$cabalnameversion/html/(.*\.haddock)%$1 /usr/share/doc/$haddockpkg/html/$cabalname/$2%;
			s%(haddock-html:).*/doc/$cabalnameversion/html%$1 /usr/share/doc/$haddockpkg/html/$cabalname%;
		    } else {
			s%(haddock-interfaces:).*/doc(/html/.*\.haddock)%$1 /usr/share/doc/$haddockpkg$2%;
			s%(haddock-html:).*/doc(/html)%$1 /usr/share/doc/$haddockpkg$2%;
		    }
		} else {
		    s%(haddock-interfaces:).*%$1%;
		    s%(haddock-html:).*%$1%;
		}
		print OUTCONFIG "$_\n";
	    }
	    close INCONFIG;
	    close OUTCONFIG;
	} elsif ($pkgtype eq "ghc6-prof") {
	    mkpath("$tmp" . getcabalbasepath($pkgtype));
	    doit("./setup", "copy", "--destdir=$tmp");
	    # Remove LICENSE file and empty directories from package
	    doit("rm", "-f", "$tmp/usr/lib/haskell-packages/ghc6/share/doc/$cabalnameversion/LICENSE");
	    finddepth({wanted => \&remove_emptydir}, "$tmp/usr/lib");
	} elsif ($pkgtype eq "haddock" ) {
	    mkpath("$tmp" . "/usr/share/doc/$package");
	    doit("cp", "-ar", "dist/doc/html", "$tmp/usr/share/doc/$package");
	}
	chdir($olddir);
    }
}
