# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           github 1.0

github.setup        pxb1988 dex2jar 2.1 v
github.tarball_from releases
maintainers         nomaintainer

categories          java devel
description         Tools to work with android .dex and java .class files
long_description    dex2jar contains 4 components:\n\
    - dex-reader/writer: Read/write the Dalvik Executable (.dex) file.\n\
    - d2j-dex2jar: Convert .dex file to .class files (zipped as jar)\n\
    - smali/baksmali: disassemble dex to smali files and assemble dex from smali files.\n\
    - other tools: d2j-decrypt-string

platforms           any
supported_archs     noarch

license             Apache-2 BSD

use_zip             yes

checksums           rmd160  9e45f6d0860df2255090d25b0322092cecb85f5c \
                    sha256  7a9bdf843d43de4d1e94ec2e7b6f55825017b0c4a7ee39ff82660e2493a46f08 \
                    size    5641797

patchfiles          patch-adjust-classpath.diff
post-patch {
    # dex2jar does not expect to be installed, but to be run from its source
    # directory. Since we put the binaries in $prefix/bin, they fail to find
    # their *.jar archives (which would then be expected in $prefix/bin/lib).
    # Reinplace $prefix to fix this. For the handling of the other tools, see
    # below.
    reinplace \
        "s#\$APP_HOME/lib/#${prefix}/lib/dex2jar/#g" \
        ${worksrcpath}/bin/dex-tools

    # All other shell scripts in dex2jar contain the same code that
    # (incorrectly) discovers the classpath. The only difference is the class
    # that is invoked as the entry point at the end. There is also one generic
    # wrapper d2j_invoke.sh, that does not pass a class name. We patch
    # d2j_invoke.sh, reinplce prefix, and overwrite all other scripts with
    # a script that will just invoke d2j_invoke.sh with the correct class name.
    reinplace -W ${worksrcpath} "s#@@PREFIX@@#${prefix}#g" d2j_invoke.sh
    array set tools []
    foreach filename [glob -directory ${worksrcpath} *.sh] {
        if {[file tail $filename] eq "d2j_invoke.sh"} {
            # Don't modify d2j_invoke.sh
            continue
        }
        # But re-write all other tools as wrappers for d2j_invoke.sh
        set fp [open ${filename} r]
        set lines [split [read $fp] "\n"]
        set last_line [lindex $lines end-1]
        close $fp
        # Example line: java -Xms512m -Xmx2048m -classpath "${_classpath}" "com.googlecode.d2j.jasmin.Jar2JasminCmd" "$@"
        regexp "java .* -classpath \"\[^\"]+\" \"(com\\.googlecode\[^\"]+)\"" $last_line -> classname
        set tools($filename) $classname
    }
    foreach filename [array names tools] {
        set fp [open ${filename} w]
        puts $fp "#!/bin/sh"
        puts $fp "${prefix}/bin/d2j_invoke.sh \"$tools($filename)\" \"\$@\""
        close $fp
    }
}

use_configure       no
build               {}
destroot {
    xinstall -m 0755 -W ${worksrcpath} \
        d2j-apk-sign.sh \
        d2j-asm-verify.sh \
        d2j-baksmali.sh \
        d2j-class-version-switch.sh \
        d2j-decrypt-string.sh \
        d2j-dex-recompute-checksum.sh \
        d2j-dex-weaver.sh \
        d2j-dex2jar.sh \
        d2j-dex2smali.sh \
        d2j-jar-access.sh \
        d2j-jar-weaver.sh \
        d2j-jar2dex.sh \
        d2j-jar2jasmin.sh \
        d2j-jasmin2jar.sh \
        d2j-smali.sh \
        d2j-std-apk.sh \
        d2j_invoke.sh \
        bin/dex-tools \
        ${destroot}${prefix}/bin
    xinstall -m 0755 -d ${destroot}${prefix}/lib/dex2jar
    xinstall -m 0644 -W ${worksrcpath}/lib \
        {*}[glob -directory ${worksrcpath}/lib *] \
        ${destroot}${prefix}/lib/dex2jar
    xinstall -m 0755 -d ${destroot}${prefix}/share/doc/dex2jar
    xinstall -m 0644 -W ${worksrcpath} \
        LICENSE.txt \
        NOTICE.txt \
        ${destroot}${prefix}/share/doc/dex2jar
}
