# -*- 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           java 1.0
PortGroup           python 1.0

name                jython
version             2.7.3
categories          lang python java
platforms           darwin
supported_archs     noarch
maintainers         {amake @amake} openmaintainer
license             PSF

description         Python implementation in pure Java
long_description    \
    Jython is an implementation of the high-level, dynamic, object-oriented \
    language Python written in 100% Pure Java, and seamlessly integrated with \
    the Java platform. It thus allows you to run Python on any Java platform.

homepage            https://www.jython.org/

# LTS Java to install if compatible Java not present
java.fallback       openjdk8

use_configure       no

# Launch scripts use Python
python.default_version 27

set jython_home     ${prefix}/share/java/${name}

variant installer description {Use installer, rather than building from source} {
    master_sites    https://search.maven.org/remotecontent?filepath=org/python/jython-installer/${version}

    distfiles       ${name}-installer-${version}.jar

    checksums       rmd160  bdb7823205e80d487e415dc5e75d59303070a483 \
                    sha256  3ffc25c5257d2028b176912a4091fe048c45c7d98218e52d7ce3160a62fdc9fc \
                    size    89408837

    extract.only

    java.version    1.7+

    build.cmd       java
    build.target    -jar ${distpath}/${distfiles}
    build.args      -s -d ${destroot}${jython_home}

    global jython_bin_name
    set jython_bin_name jython

    destroot {
        foreach binfile [glob ${destroot}${jython_home}/bin/*] {
            reinplace "s|#!/usr/bin/env .*|#!${prefix}/bin/${name}|" ${binfile}
        }
    }
}

default_variants +installer

# Install from source.
# This block must come after the installer variant definition.
if {![variant_isset installer]} {
    PortGroup       github 1.0
    github.setup    jython jython ${version} v

    checksums       rmd160  52929a497d0729c7f3fea1fc8c848de2abf46bf7 \
                    sha256  aca039f72328667602c06bf4785fcaa8083e502b8de52a74e6f46df39037171b \
                    size    51470331
    # Remove during next update
    dist_subdir   ${name}/${version}_2

    # Jython built with JDK 9 or later won't run on Java 8
    # https://github.com/macports/macports-ports/pull/8425#discussion_r488342820
    java.version    1.8

    # Use Ant 1.10.x for Java 8 compatibility
    depends_build-append \
                    port:apache-ant

    build.cmd       ant
    build.target    developer-build

    global jython_bin_name
    set jython_bin_name jython.py

    destroot {
        reinplace "s|#!/usr/bin/env python.*|#!${python.bin}|" \
            ${worksrcpath}/dist/bin/${jython_bin_name}
        xinstall -m 0755 -d ${destroot}${prefix}/share/java/
        copy ${worksrcpath}/dist ${destroot}${jython_home}
    }
}

post-destroot {
    # Workaround for https://github.com/jythontools/jython/issues/173
    system "chmod -R a+r ${destroot}${prefix}/share/java/${name}/Lib"

    ln -s ${jython_home}/bin/${jython_bin_name} ${destroot}${prefix}/bin/${name}

    delete {*}[glob -directory ${destroot}${jython_home}/bin *.bat *.exe]
}

pre-activate {
    # Old versions of this port left the Jython cache in the system-wide
    # jython_home. We clean up the old cache here.
    file delete -force -- ${prefix}/share/java/${name}/cachedir
}

# Homepage is rarely updated, so check tags on GitHub mirror
livecheck.type      regex
livecheck.url       https://github.com/jython/jython/tags
livecheck.regex     {archive/refs/tags/v([0-9.-]+)\.tar\.gz}
