#! /usr/bin/perl
# gen_contents_index, written for Debian by Kari Pahula
# Copyright 2009 Kari Pahula
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

use Cwd qw(abs_path);

my @ifaces;
my %pkgs;

# Only do something if we actually have /usr/share/doc
exit 0 unless (-d "/usr/share/doc/ghc-doc/html/libraries/" and -r "/usr/share/doc/ghc-doc/html/libraries/prologue.txt");

# Add everything from the global Cabal registry to index.
if (-e '/usr/bin/ghc-pkg') {
    open CABAL, "/usr/bin/ghc-pkg --global --simple-output field '*' name,version,haddock-interfaces,haddock-html |" or warn "ghc-pkg dump failed: $!";
    addInfo (\*CABAL, \%pkgs, \@ifaces);
    close CABAL;
}

exec ('haddock', '--gen-index', '--gen-contents',
      '--mathjax', 'file:///usr/share/javascript/mathjax/MathJax.js',
      '-o', '/usr/share/doc/ghc-doc/html/libraries/',
      '-t'. 'Haskell Hierarchical Libraries',
      '-p', '/usr/share/doc/ghc-doc/html/libraries/prologue.txt',
      @ifaces);

sub addInfo {
    my $fh = shift;
    while (<$fh>) {
        my %dat;
        chomp ($dat{pkg} = $_);
        chomp ($dat{ver} = <$fh>);
        chomp ($dat{ifaces} = <$fh>);
        chomp ($dat{html} = <$fh>);
        $dat{html} = abs_path($dat{html});
        process(\%dat, @_);
    }
}

sub process {
    my $dat = shift;
    my $pkgs = shift;
    my $ifaces = shift;
    my $path;
    return undef if $$dat{pkg} eq 'ghc';
    my $p = $$dat{pkg}.'-'.$$dat{ver};
    return undef if (exists $$pkgs{$p});
    if ($$dat{html} =~ m,^/usr/share/doc/ghc-doc/html/libraries/(.*),) {
        $path = $1;
    } elsif ($$dat{html} =~ m,^/usr/share/doc/([^/]*-doc/html.*),) {
        $path = "../../../$1";
    }

    if (defined $path && -r $$dat{ifaces}) {
        $$pkgs{$p} = 1;
        push @ifaces, "--read-interface=$path,$$dat{ifaces}";
    }
}
