#!/usr/bin/perl -w

=head1 NAME

dh_installzope - install zope product and extension files into package build
directories

=cut

use strict;
use Debian::Debhelper::Dh_Lib;

=head1 SYNOPSIS

B<dh_installzope> [S<I<debhelper options>>] [B<-n>] [B<-X>I<item>] [S<I<dir ...>>]

=head1 DESCRIPTION

dh_installzope is a debhelper program that is responsible for installing
zope products and extensions into package build directories.

Directory names are interpreted as zope product names and will be
installed into the first package dh_installzope is told to act on. By
default, this is the first binary package in debian/control, but if
you use B<-p>, B<-i>, or B<-a> flags, it will be the first package
specified by those flags.

dh_installzope automatically installs debian/dzproduct and debian/dzextension
if it exists. If dh_installzope is acting on multiple packages, debian/dzproduct
and debian/dzextension files will be installed into the first package.
Use debian/package.dzproduct and debian/package.dzextension for different
binary packages.

If your package needs to register more than one product or extension,
you need multiple files. To accomplish this, you can use files named
debian/package.dzproduct.* and  debian/package.dzextension.*

Missing dzproduct and dzextension files for the first package dh_installzope
is told to act on are generated and installed automatically.

The substitution variable zope:Depends is replaced by a list of the available
zope versions.

=head1 OPTIONS

=over 4

=item B<-n>, B<--noscripts>

Do not modify postinst/prerm scripts.

=item B<-Xitem>, B<--exclude=item>

Exclude files that contain "item" anywhere in their filename from
being installed.

=item I<directory ...>

Install these directories as products into the first package acted on.

=back

=head1 EXAMPLES

This is an example of a debian/package.dzproduct file:

  Package: zope-cmfdefault1.4
  Name: CMFDefault
  Directory: CMFDefault:1.4
  Depends: CMFTopic:1.4
  Version: 1.4.7-1
  ZopeVersions: 2.9 2.8

The Version field is added or updated by dh_installzope. If Name
and Directory match, the latter can be omitted.  A deviating 
Directory is used to install different product versions. A Directory
has the form <product name>[:<suffix>].

It is possible to specify the list of zope versions for the 2.x serie
using a >= relation, like in this example:

  Package: zope-psycopgda
  ZopeVersions: >= 2.7

This allows bin-NMUs of zope products on new Zope 2.x major versions,
and for this reason it should be the preferred syntax.

=head1 NOTES

Note that this command is not idempotent. "dh_clean B<-k>" should be called
between invocations of this command. Otherwise, it may cause multiple
instances of the same text to be added to maintainer scripts.

=cut

init();

my @zope_versions = ('2.6', '2.7', '2.8', '2.9', '2.10', '3');
my %python_versions = ('3', '2.4');

sub read_dzfile {
    my $fn = shift;
    my %fields=();
    open (DZ, "debian/$fn") || error("cannot read debian/$fn: $!\n");
    while (<DZ>) {
        chomp;
        s/\s+$//;
        if (/^([^:]+):\s*(.*)/) {
            $fields{$1} = $2;
        }
    }
    close DZ;
    return %fields;
}

sub write_dzfile {
    my ($fn, %fields) = @_;
    my ($out) = "";
    open (DZ, ">$fn") || error("cannot write $fn: $!\n");
    foreach (keys %fields) {
        $out = "$_: $fields{$_}\n" . $out;
    }
    print DZ $out;
    close DZ;
}

sub addzopesubstvars {
    my ($package, %fields) = @_;
    my ($var) = "zope-common (>= 0.5.38), ";
    my (@versions) = ();
    my ($minor) = "0";

    if ($fields{'ZopeVersions'}) {
        my (@vers) = split(/ /, $fields{'ZopeVersions'});
        if ($vers[0] eq '>=') {
            @versions = ();
            if ($vers[1] =~ m/^[0-9]\.([0-9]+)$/) {
              $minor = $1;
            } 
            foreach (@zope_versions) {
                if ($_ =~ m/[0-9]\.([0-9]+)/ && int($1) >= int($minor)) {
                    push(@versions, $_);
                }
            }
        } else {
            @versions = @vers;
        }
    } else {
      @versions = @zope_versions;
    }

    foreach (reverse(@versions)) {
        $var .= "zope" . $_ . " | ";
    }
    $var = substr($var, 0, length($var)-3);
    addsubstvar($package, "zope:Depends", $var);
    return join(" ", reverse(@versions));
}

my @autoscripts = ('preinst', 'postinst', 'prerm', 'postrm');

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

    my $dzproductfile=pkgfile($package,"dzproduct");
    my $dzextensionfile=pkgfile($package,"dzextension");

    my $archdir=package_arch($package) eq 'all' ? 'share' : 'lib';

    # needed to get $dh{VERSION}
    my $isnative = isnative($package);

    my $addon_version;

    my @dirs;

    if ($package eq $dh{FIRSTPACKAGE} && @ARGV) {
        push @dirs, @ARGV;
    }

    if ($dh{V_FLAG}) {
        $addon_version = $dh{V_FLAG};
    }

    # Handle dzproduct and dzextension files. There are two filename formats,
    # the usual plus an extended format (debian/package.*).
    my @dzpr_files;
    my @dzex_files;
    
    opendir(DEB,"debian/") || error("can't read debian directory: $!");
    # If this is the main package, we need to handle unprefixed filenames.
    # For all packages, we must support both the usual filename format plus
    # that format with a period an something appended.
    my $regexp="\Q$package\E\.";
    if ($package eq $dh{MAINPACKAGE}) {
        $regexp="(|$regexp)";
    }
    foreach my $fn (grep {/^${regexp}dz(product|extension)(\..*)?$/} readdir(DEB)) {
        # .EX are example files, generated by eg, dh-make
        next if $fn=~/\.EX$/;
        if ($fn=~/dzproduct/) {
            push @dzpr_files, $fn;
        }
        else {
            push @dzex_files, $fn;
        }
    }
    closedir(DEB);

    if (@dirs > 0 && @dzpr_files > 1) {
        error("unable to handle directories and multiple dzproduct/dzextension files");
    }
    if (@dirs > 1 && @dzpr_files > 0) {
        error("unable to handle multiple directories and dzproduct/dzextension files");
    }

    if (@dirs) {
        my %tdzproduct;
        if (@dzpr_files) {
            %tdzproduct = read_dzfile(shift @dzpr_files);
            if (not exists $tdzproduct{'Package'}) {
                $tdzproduct{'Package'} = $package;
            }
        }
        else {
            $tdzproduct{'Package'} = $package;
        }
        $tdzproduct{'Version'} = $dh{VERSION};
        $tdzproduct{'ZopeVersions'} = addzopesubstvars($package, %tdzproduct);

        my $exclude = '';
        if ($dh{EXCLUDE_FIND}) {
            $exclude = ' -and -not \( '.$dh{EXCLUDE_FIND}.' \)';
        }
        doit("install","-d","$tmp/usr/$archdir/zope/Products");
        foreach my $dir (@dirs) {
            my %dzproduct = %tdzproduct;
            my ($dir_basename) = basename($dir);
            if (!$dzproduct{'Name'}) {
                if ($dir ne '.') {
                    $dzproduct{'Name'} = $dir_basename;
                } else {
                    error("you have to specify the product name in the dzproduct file");
                }
            }

            my $target;
            if ($dzproduct{'Directory'}) {
                $target = $dzproduct{'Directory'};
            }
            else {
                $target = $dzproduct{'Name'};
                if ($addon_version) {
                    $dzproduct{'Directory'} = $package;
                }
            }

            my ($exclude_debian) = '';
            $exclude_debian = ' -and -not \( -path \'*/debian/*\' -or -path \'*-stamp\' \)' if ($dir eq ".");
 
            doit("install","-d","$tmp/usr/$archdir/zope/Products/$target");
 
            my $pwd=`pwd`; chop($pwd);
            if (exists $dzproduct{'Global'} && $dzproduct{'Global'} eq 'yes') {
                if ($dzproduct{'ZopeVersions'} != '3') {
                    error("global products are supported only for zope3");
                }
                my ($pyver) = $python_versions{$dzproduct{'ZopeVersions'}};
                addsubstvar($package, "zope:PythonVersion", $pyver);
                complex_doit("cd $dir && find -type f$exclude$exclude_debian " .
                    "-exec install -D --mode=644 {} $pwd/$tmp/usr/lib/python$pyver/" .
                    "site-packages/$target/{} \\;");
                complex_doit("mkdir -p $pwd/$tmp/usr/$archdir/zope/Products/$target");
                complex_doit("find $pwd/$tmp/usr/lib/python$pyver/site-packages/$target".
                " \\( -name \\*-configure.zcml -o -name \\*-ftesting.meta -o -name " .
                "\\*-meta.zcml \\) -exec mv {} $pwd/$tmp/usr/$archdir/zope/Products" .
                "/$target/ \\;");
            } else {
                complex_doit("cd $dir && find -type f$exclude$exclude_debian -exec install -D ".
                    "--mode=644 {} $pwd/$tmp/usr/$archdir/zope/Products/$target/{} \\;");
            }
 
            write_dzfile("$tmp/usr/$archdir/zope/Products/$target/.dzproduct", %dzproduct);
            if (! $dh{NOSCRIPTS}) {
                foreach my $script (@autoscripts) {
                    autoscript($package,$script,"$script-dzproduct",
                           "s,#DZ-DIR#,/usr/$archdir/zope/Products/$target,;"
                           . "s,#DZ-ADDON#,$dzproduct{'Name'},;"
                           . "s,#PACKAGE#,$package,",
                          );
                }
            }
        }
        doit("chown","-R","0:0","$tmp/usr/$archdir/zope/Products");
        doit("chmod","-R","go=rX","$tmp/usr/$archdir/zope/Products");
        doit("chmod","-R","u+rw","$tmp/usr/$archdir/zope/Products");
    }

    foreach my $fn (@dzpr_files) {
        my %dzproduct = read_dzfile($fn);
        my $target = $dzproduct{'Directory'} ? $dzproduct{'Directory'} : $dzproduct{'Name'};
        if (! $target) {
            error("no Name or Directory key in debian/$fn")
        }
        if (not exists $dzproduct{'Package'}) {
            $dzproduct{'Package'} = $package;
        }
        if (! -d "$tmp/usr/$archdir/zope/Products/$target") {
            doit("install","-g",0,"-o",0,"-d","$tmp/usr/$archdir/zope/Products/$target");
        }
        doit("install","-g",0,"-o",0,"-m644","-p","debian/$fn",
             "$tmp/usr/$archdir/zope/Products/$target/.dzproduct");
        $dzproduct{'Version'} = $dh{VERSION};
        $dzproduct{'ZopeVersions'} = addzopesubstvars($package, %dzproduct) if exists $dzproduct{'ZopeVersions'};
        write_dzfile("$tmp/usr/$archdir/zope/Products/$target/.dzproduct", %dzproduct);
        if (! $dh{NOSCRIPTS}) {
            foreach my $script (@autoscripts) {
                autoscript($package,$script,"$script-dzproduct",
                       "s,#DZ-DIR#,/usr/$archdir/zope/Products/$target,;"
                       . "s,#DZ-ADDON#,$dzproduct{'Name'},;"
                       . "s,#PACKAGE#,$package,",
                      );
            }
        }
    }

    foreach my $fn (@dzex_files) {
        my %dzext = read_dzfile($fn);
        my $target = $dzext{'Directory'} ? $dzext{'Directory'} : $dzext{'Name'};
        if (! $target) {
            error("no Extension or Directory key in debian/$fn")
        }
        if (not exists $dzext{'Package'}) {
            $dzext{'Package'} = $package;
        }
        if (! -d "$tmp/usr/$archdir/zope/Extensions/$target") {
            doit("install","-g",0,"-o",0,"-d","$tmp/usr/$archdir/zope/Extensions/$target");
        }
        doit("install","-g",0,"-o",0,"-m644","-p","debian/$fn",
             "$tmp/usr/$archdir/zope/Extensions/$target/.dzextension");
        $dzext{'Version'} = $dh{VERSION};
        $dzext{'ZopeVersions'} = addzopesubstvars($package, %dzext) if exists $dzext{'ZopeVersions'};
        write_dzfile("$tmp/usr/$archdir/zope/Extensions/$target/.dzextension", %dzext);
        if (! $dh{NOSCRIPTS}) {
            foreach my $script (@autoscripts) {
                autoscript($package,$script,"$script-dzextension",
                       "s,#DZ-DIR#,/usr/$archdir/zope/Extensions/$target,;"
                       . "s,#DZ-ADDON#,$dzext{'Name'},;"
                       . "s,#PACKAGE#,$package,",
                      );
            }
        }
    }
}

=head1 SEE ALSO

L<debhelper(7)>

This program is a part of zope-debhelper.

=head1 AUTHOR

Matthias Klose <doko@ubuntu.com>
Fabio Tranchitella <kobold@debian.org>

=cut
