# -*- 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

name                openjdk8-bootstrap
# use format based on macports:
# https://guide.macports.org/chunked/reference.portgroup.html#reference.portgroup.java
version             8
revision            0
categories          java devel
# TODO: specify which darwin versions the binary actually works on
platforms           {darwin any}
supported_archs     arm64 x86_64
license             Restrictive/Distributable
maintainers         nomaintainer
description         Java 8 Boot JDK
long_description    Java 8 Boot JDK to bootstrap Openjdk8 port
homepage            https://openjdk.org/

if {${configure.build_arch} eq "x86_64"} {
    set ver 1.8.0_201
    master_sites        ${homepage}webapps/download/${distfiles}?BundleId=236866_42970487e3af4f5aa5bca3f542482c60&dummy=
    distfiles           AutoDL
    
    checksums           rmd160  b237dd06774127454df314a28fa6b297b50a0dbf \
                        sha256  5b52df4b3efd51480c40ad5abfa18a075365afbb2040bbc5e9f2db568435a6e2 \
                        size    257863467

    depends_extract     port:xar

    extract {
        system -W ${workpath} "mkdir -p ${worksrcpath}/packages"
        file copy ${distpath}/${distfiles} ${worksrcpath}/packages
        system -W ${workpath} "mkdir -p ${worksrcpath}/dmg"
        system -W ${workpath} "mkdir -p ${worksrcpath}/pkg"
        system -W ${workpath} "mkdir -p ${worksrcpath}/files"
        system -W ${worksrcpath}/packages "hdiutil attach ${worksrcpath}/packages/${distfiles} -mountroot ${worksrcpath}/dmg"
        system -W ${worksrcpath}/dmg "cp ${worksrcpath}/dmg/*/*.pkg ${worksrcpath}/pkg"
        system -W ${worksrcpath}/dmg "hdiutil detach ${worksrcpath}/dmg/*"
        system -W ${worksrcpath}/pkg "mv ${worksrcpath}/pkg/*.pkg ${worksrcpath}/pkg/jdk${ver}.pkg"
        system -W ${worksrcpath}/pkg "${prefix}/bin/xar -xf ${worksrcpath}/pkg/jdk${ver}.pkg -C ${worksrcpath}/files"
        system -W ${worksrcpath} "cpio -i < ${worksrcpath}/files/jdk180201.pkg/Payload"
    }
    worksrcdir          jdk${ver}
} elseif {${configure.build_arch} eq "arm64"} {
    set ver 8.54.0.21
    set openjdk_ver 8.0.292
    master_sites        https://cdn.azul.com/zulu/bin/
    distname            zulu${ver}-ca-jdk${openjdk_ver}-macosx_aarch64
    checksums           rmd160  cafab852e7263d3b1077b7b37f8d9019f4d3797a \
                        sha256  2cf0af3118dd4b6244724308b190587804a76ad7919b6da5c377392a1d4f8817 \
                        size    105926746

    configure.cxx_stdlib libstdc++

    worksrcdir          ${distname}/zulu-8.jdk
} else {
    master_sites
    distfiles
}

use_configure    no

build {}

if {${configure.build_arch} eq "x86_64"} {
    pre-destroot {
        # MacPorts report `Contents/Home/jre/lib/libdeploy.dylib` as broken file during `port rev-upgrade`
        # see https://trac.macports.org/ticket/65049
        delete ${worksrcpath}/Contents/Home/jre/lib/libdeploy.dylib
    }
}

set path /Library/Java/JavaVirtualMachines/${name}
destroot {
    xinstall -m 755 -d ${destroot}${path}
    copy ${worksrcpath}/Contents ${destroot}${path}
}
destroot.violate_mtree      yes