#!/usr/bin/perl

=head1 NAME

jh_manifest - Adds or/and modifies manifests for jars

=cut

use strict;
use warnings;

use Cwd();
use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
use Debian::Debhelper::Dh_Lib;
use Debian::Javahelper::Java;
use Debian::Javahelper::Manifest( qw(MAIN_SECTION) );

=head1 SYNOPSIS

B<jh_manifest>  [S<I<debhelper options>>]  [S<I<options>>]  I<jar1> ... I<jarN>
B<jh_manifest>  [S<I<debhelper options>>]  [S<I<options>>]

=head1 DESCRIPTION

Javahelper tool to add or update manifests in a jar file. It can be
used in two modes. If passed jar files, it will only process these jar
files. Otherwise it will update all jar files in the packages it acts
on.

When processing a package, the L<debhelper(7)> exclude option will
make B<jh_manifest> ignore matching jar files.

=head1 FILES

=over 4

=item debian/I<package>.manifest (or debian/manifest)

This file consist of a list of jar files and values to add to their
manifests. Values in this file will take precedence over values in the
original manifest (and command line arguments over values in this
file).

It is allowed to list a link in this file instead of an actual jar
file, provided that the link can be resolved when B<jh_manifest>
processes it.

If a jar file is listed here cannot be found, B<jh_manifest> will
print a warning, unless the jar file has been excluded.

As of javahelper >= 0.32, you may add comments in this file. If the
line starts with a "#" it is completely ignored.

This file is ignored if B<jh_manifest> is passed jar files via command
line.

=back

=head1 OPTIONS

=over 4

=item B<-c> I<classpath>, B<--classpath=>I<classpath>

Sets the Class-Path attribute of all processed jar files to
I<classpath>.

If not passed, then the CLASSPATH environment variable will be used in
the given jar file do not have a Class-Path attribute.

=item B<-m> I<class>, B<--main=>I<class>

Sets the Main-Class attribute to I<class> in all processed jar files.

=item B<-o> I<options>, B<--javaopts=>I<options>

Sets the Debian-Java-Parameters to I<options> in all processed jar
files. This attribute is used by jarwrapper to start java with extra
options (e.g. to make more memory available).

=item B<-j> I</path/to/java/home>, B<--java-home=>I</path/to/java/home>

Sets the Debian-Java-Home attribute to I</path/to/java/home> in all
processed jars. This attribute is used by jarwrapper to determine
which JVM to use.

=back

=cut

my $cp = '';
my $mcl = '';
my $jvm = '';
my $jopt = '';
my $envcp = 0;

init(options => {
    # -o clashes debhelper's "only scripts"
    "javaopts|o=s" => \$jopt,
    "java-home|j=s" => \$jvm,
    "main|m=s" => \$mcl,
    "classpath|c=s" => \$cp,
    "version|V" => sub { print STDERR "Version has been removed - please stop using it\n"; exit(0) },
     });

if(!$cp && ($ENV{'CLASSPATH'}//'') ne ''){
    $cp = $ENV{'CLASSPATH'};
    $cp =~ s/:/ /go;
    $envcp = 1;
}

if(@ARGV){
    foreach my $jar (@ARGV){
        update_jar($jar, undef);
    }
    inhibit_log();
    # behave like the old jh_manifest.
    exit(0);
}

foreach my $package (@{$dh{DOPACKAGES}}) {
    my $man = pkgfile($package, "manifest");
    my $dir = tmpdir($package);
    my $manifests = {};
    my $files = {};
    my @links = ();
    my $find_cmd="find $dir ";
    my $err;
    my $fulldir = Cwd::abs_path($dir);
    # skip if it does not exist.
    if( ! -d $dir ){
        verbose_print("Skipping $package - $dir does not exist (or is not a dir).");
        next;
    }
    $manifests = parse_package_file($man) if($man);
    if (defined($dh{EXCLUDE_FIND}) && $dh{EXCLUDE_FIND} ne ''){
        $find_cmd.=" '!' \\( $dh{EXCLUDE_FIND} \\) -a ";
    }
    $find_cmd .= " -name '*.jar'";
    verbose_print("$find_cmd");
    open(my $jarfiles, "-|", "$find_cmd") or error("$find_cmd: $!");
    while( my $jar = <$jarfiles> ) {
         chomp($jar);
         if( -l $jar ) {
             $jar =~ s@^\Q$dir\E/*@@;
             verbose_print("Found symlink $jar");
             push(@links, $jar);
         } else {
             $jar = Cwd::abs_path($jar);
             $jar =~ s@^\Q$fulldir\E/*@@;
             verbose_print("Found $jar");
             $files->{$jar} = 1;
         }
    }
    close($jarfiles);
    # Shame on me - using internal subs from Dh_Lib - but it works.
    #   It will error out if invoked.
    Debian::Debhelper::Dh_Lib::_error_exitcode($find_cmd) if($?);
    # check the links first.
    foreach my $link (@links) {
        my $path = Cwd::abs_path("$dir/$link");
        my $manifest = $manifests->{$link};
        my $lp = $path;
        next unless(defined($manifest));
        error("Cannot modify $link - it is a broken symlink or not possible to resolve,") unless( defined($path) && -e $path );
        $lp =~ s@^\Q$fulldir\E/*@@;
        error("Conflicting manifests for $link (link) and $path,") if(exists($manifests->{$lp}));
        delete($files->{$lp});
        delete($manifests->{$link});
        verbose_print("Updating symlinked $path (using manifest for $link)");
        update_jar("$path", $manifest);
    }
    foreach my $jar (keys(%$files)){
        my $lp = $jar;
        my $manifest;
        $lp =~ s@^\Q$fulldir\E/*@@;
        $manifest = $manifests->{$lp};
        delete($manifests->{$lp});
        update_jar("$dir/$lp", $manifest);
    }
    foreach my $unused (keys(%$manifests)){
        # complain - unless it has been ignored of course.
        warning("No jar in $package matching $unused") unless(excludefile($unused));
    }
}

exit(0);

sub parse_package_file{
    my $pkgfile = shift;
    my $manifests = {};
    my @jars;
    my @man;
    my $inlen = 0;
    open(my $file, "<", $pkgfile) or error("$pkgfile: $!");
    while( my $line = <$file> ){
        chomp($line);
        next if($line =~ m/^#/o);
        if($line =~ m/^ \s(\s*) /ox){
            error("Manifest data not attached to a jar in $pkgfile") unless(@jars);
            $inlen = length($1) unless($inlen);
            $line = substr($line, 1 + $inlen);
            push(@man, $line);
            next;
        }
        if(@man){
            my $mlines = join("\n", @man);
            my $manifest;
            open(my $mfd, "<", \$mlines) or error("Reading scalar: $!");
            $manifest = parse_manifest_fd($mfd, $pkgfile);
            close($mfd);
            foreach my $j (@jars){
                error("Two manifests for $j in $pkgfile,") if(exists($manifests->{$j}));
                $manifests->{$j} = $manifest;
            }
            @man = ();
            @jars = ();
        }
        next if($line eq '');
        if($line =~ m/\.jar:$/o){
            $line =~ s@^/*@@o;
            $line =~ s@//+@/@og;
            $line =~ s/:$//o;
            push(@jars, $line);
            next;
        }
        print STDERR "Syntax error in $pkgfile ($line) - perhaps you are missing a \":\"?\n" if($line !~ m/:$/o);
        error("Unknown line in $pkgfile ($line),");
    }
    if(@man){
        my $mlines = join("\n", @man);
        my $manifest;
        open(my $mfd, "<", \$mlines) or error("Reading scalar: $!");
        $manifest = parse_manifest_fd($mfd, $pkgfile);
        close($mfd);
        foreach my $j (@jars){
            $manifests->{$j} = $manifest;
        }
    }
    close($file);
    return $manifests;
}

sub update_jar{
    my $jar = shift;
    my $merge = shift;
    my $zip = Archive::Zip->new();
    my $con;
    my $manifest;
    my $stat;
    my $dirty = 0;
    my $main;
    my $new_manifest = 0;
    # stringify or $zip will make a call back that fails.
    $zip->read( "$jar" ) == AZ_OK or error("Could not read $jar: $!");
    ($con, $stat) = $zip->contents( 'META-INF/MANIFEST.MF' );
    die("Could not read manifest from $jar ($stat): $!") unless(!defined($stat) or $stat == AZ_OK);
    if(defined($stat)) {
        verbose_print("Reading manifest from $jar");
        open(my $fd, "<", \$con) or error("open read string: $!");
        $manifest = parse_manifest_fd($fd, $jar);
        close($fd);
    } else {
        verbose_print("$jar does not have a manifest.");
        $manifest = Debian::Javahelper::Manifest->new();
        $new_manifest = 1;
    }
    if(defined($merge)){
        $manifest->merge($merge);
        $dirty = 1;
    }
    $main = $manifest->get_section(MAIN_SECTION, 1);
    if($cp && (!$envcp || ($main->get_value('Class-Path')//'') eq '')){
        $main->set_value('Class-Path', $cp);
        $dirty = 1;
    }
    if($mcl){
        $main->set_value('Main-Class', $mcl);
        $dirty = 1;
    }
    if($jvm){
        $main->set_value('Debian-Java-Home', $jvm);
        $dirty = 1;
    }
    if($jopt){
        $main->set_value('Debian-Java-Parameters', $jopt);
        $dirty = 1;
    }
    if($dirty){
        my $var;
        my $mem;
        open(my $fd, ">", \$var) or error("open write string: $!");
        write_manifest_fd($manifest, $fd, $jar);
        close($fd);
        verbose_print("Updating manifest in $jar");
        $zip->removeMember( 'META-INF/MANIFEST.MF' ) unless($new_manifest);
        $mem = $zip->addString($var, 'META-INF/MANIFEST.MF');
        $mem->desiredCompressionMethod(COMPRESSION_DEFLATED);
        # This on the other hand may fail.
        $zip->overwrite() == AZ_OK or error("Writing modified jar ($jar) failed: $!");
    } else {
        verbose_print("No update of $jar required.");
    }
    1;
}

=head1 EXAMPLES

An example debian/manifest file:

 # use the symlink so we do not have to update with the next upstream release.
 usr/share/java/my.jar:
  Class-Path: dep1.jar dep2.jar
  Main-Class: some.awesome.Class
 usr/share/java/dep2.jar:
  Class-Path: dep1.jar



=head1 SEE ALSO

L<debhelper(7)>

This program is a part of javahelper and uses debhelper as backend. There are
also tutorials in /usr/share/doc/javahelper.

=head1 AUTHOR

Niels Thykier <niels@thykier.net>

=head1 COPYRIGHT AND LICENSE

Copyright 2010 by Niels Thykier

This tool is free software; you may redistribute it and/or modify
it under the terms of GNU GPL 2.

=cut

