# -*- 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.4
categories          lang python java
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  000040e9ae585cf838da5f6052076569d8d59cb2 \
                    sha256  6001f0741ed5f4a474e5c5861bcccf38dc65819e25d46a258cbc0278394a070b \
                    size    95341227

    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
    # Change github.tarball_from to 'releases' or 'archive' next update
    github.tarball_from tarball

    checksums       rmd160  735ea1fe43053a4ae8c27621a360ea892ca69784 \
                    sha256  cf4ae482fe407974d7faed86675114e73a5b2955d25d1835c52114db6a8093c8 \
                    size    54577348
    # 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}
