# -*- 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
PortGroup           gpg_verify 1.0
PortGroup           compilers 1.0

compilers.choose    fc f77 f90
compilers.setup     require_fortran -g95
compiler.blacklist-append {clang < 900}

github.setup        JuliaLang julia 1.12.6 v
revision            0
categories-append   lang math science
maintainers         {ieee.org:s.t.smith @essandess} \
                    {b-data.ch:olivier.benz @benz0li} \
                    openmaintainer
platforms           {darwin >= 14}
license             MIT
homepage            http://julialang.org

description         The Julia Language: A fresh approach to technical \
                    computing.

long_description    Julia is a high-level, high-performance dynamic \
                    programming language for technical computing, with \
                    syntax that is familiar to users of other technical \
                    computing environments.

github.tarball_from releases

distfiles           ${name}-${version}-full${extract.suffix}

checksums           ${name}-${version}-full${extract.suffix} \
                    rmd160  9edb80677cfa540ccc328a2eadd2923e4d0f958b \
                    sha256  711f3aa8d6ec5c9004593eb8f3d53e3564cd759acba8ad4adae967afc20332cc \
                    size    570081040

extract.only        ${distfiles}

gpg_verify.use_gpg_verification \
                    yes

if {[option gpg_verify.use_gpg_verification]} {
    distfiles-append \
                    ${name}-${version}-full${extract.suffix}.asc
    checksums-append \
                    ${name}-${version}-full${extract.suffix}.asc \
                    size    902

    post-checksum {
        gpg_verify.verify_gpg_signature \
            ${filespath}/juliareleases.asc \
            ${distpath}/${name}-${version}-full${extract.suffix}.asc \
            ${distpath}/${name}-${version}-full${extract.suffix}
    }
}

# julia build is `make && make install`
# see https://github.com/JuliaLang/julia/blob/master/Makefile

use_configure       no

build.target        release

pre-build {
    # create a `gfortran` symbolic link
    xinstall -m 0755 -d ${workpath}/bin
    ln -sf ${configure.fc} ${workpath}/bin/gfortran

    build.env-append \
        PATH=$env(PATH):${workpath}/bin \
        PREFIX=${prefix} \
        FC=${configure.fc}
}

set app_name "[string totitle ${name}].app"

variant app \
    description {Install macOS app.} {
        post-build {
            # create the app by hand following ${worksrcpath}/contrib/mac/app/Makefile
            set app_copyright "© [exec date +%Y] The Julia Project"
            system -W ${worksrcpath}/contrib/mac/app \
                "/bin/sh -c \"
                    osacompile -o ${app_name} startup.applescript
                    rm ${app_name}/Contents/Resources/applet.icns
                    cp julia.icns ${app_name}/Contents/Resources/
                    plutil -replace CFBundleDevelopmentRegion -string \"en\" ${app_name}/Contents/Info.plist
                    plutil -insert CFBundleDisplayName -string \"Julia\" ${app_name}/Contents/Info.plist
                    plutil -replace CFBundleIconFile -string \"julia.icns\" ${app_name}/Contents/Info.plist
                    plutil -replace CFBundleIconName -string \"julia\" ${app_name}/Contents/Info.plist
                    plutil -insert CFBundleIdentifier -string \"org.julialang.launcherapp\" ${app_name}/Contents/Info.plist
                    plutil -replace CFBundleName -string \"Julia\" ${app_name}/Contents/Info.plist
                    plutil -insert CFBundleShortVersionString -string \'${version}\' ${app_name}/Contents/Info.plist
                    plutil -insert CFBundleVersion -string \'${version}\' ${app_name}/Contents/Info.plist
                    plutil -insert NSHumanReadableCopyright -string \'${app_copyright}\' ${app_name}/Contents/Info.plist
                    mkdir -p ${app_name}/Contents/Resources/julia/bin
                    ln -s ${prefix}/bin/julia ${app_name}/Contents/Resources/julia/bin/julia\"
                "

            if {${configure.build_arch} eq {arm64}} {
                system -W ${worksrcpath}/contrib/mac/app \
                    "codesign -f -s - '${app_name}/Contents/MacOS/applet'"
            }
        }

        post-destroot {
            copy ${worksrcpath}/contrib/mac/app/${app_name} \
                ${destroot}${applications_dir}
        }
}

default_variants-append \
                    +app

pre-destroot {
    destroot.env-append \
        PATH=$env(PATH):${workpath}/bin \
        PREFIX=${prefix} \
        FC=${configure.fc}
}

post-destroot {
    # julia doesn't do destroot quite right
    set short_version [join [lrange [split ${version} .] 0 1] .]
    set major_version [lindex [split ${version} .] 0]
    set dpw ${destroot}${worksrcpath}
    delete ${destroot}${prefix}/bin
    move ${dpw}/julia-${version}/bin ${destroot}${prefix}/bin
    move ${dpw}/julia-${version}/etc/julia ${destroot}${prefix}/etc
    move ${dpw}/julia-${version}/include/julia ${destroot}${prefix}/include
    move ${dpw}/julia-${version}/lib/julia ${destroot}${prefix}/lib
    move ${dpw}/julia-${version}/lib/libjulia.${short_version}.dylib ${destroot}${prefix}/lib
    move ${dpw}/julia-${version}/lib/libjulia.dylib ${destroot}${prefix}/lib
    move ${dpw}/julia-${version}/lib/libjulia.${version}.dylib ${destroot}${prefix}/lib
    # see https://github.com/JuliaLang/julia/pull/48931
    move ${dpw}/julia-${version}/libexec/julia ${destroot}${prefix}/libexec/julia
    move ${dpw}/julia-${version}/share/julia ${destroot}${prefix}/share
    delete ${destroot}${prefix}/share/man
    move ${dpw}/julia-${version}/share/man ${destroot}${prefix}/share

    delete ${destroot}${prefix}/var

    system -W ${destroot}${prefix}/bin "install_name_tool -add_rpath ${prefix}/lib julia"
    system -W ${destroot}${prefix}/lib/julia "install_name_tool -change ${worksrcpath}/usr/lib/libccalllazyfoo.dylib @rpath/libccalllazyfoo.dylib libccalllazybar.dylib"
    if {${os.arch} eq "arm"} {
        # manually fix code signature on arm
        system -W ${destroot}${prefix} "/usr/bin/codesign --sign - --force lib/libjulia.${version}.dylib"
        system -W ${destroot}${prefix} "/usr/bin/codesign --sign - --force libexec/julia/dsymutil"
        system -W ${destroot}${prefix} "/usr/bin/codesign --sign - --force libexec/julia/lld"
    }
}

pre-test {
    test.env-append \
        PATH=$env(PATH):${workpath}/bin \
        PREFIX=${prefix} \
        FC=${configure.fc}
}

test.run            yes

if {${os.arch} eq "arm"} {
    notes-append \
"
On Apple Silicon machines, access the macOS Accelerate\
and Metal frameworks using, respectively:\

    using AppleAccelerate
    using Metal
"
}

if {${os.platform} eq "darwin" && ${os.major} < 14} {
    known_fail      yes
    pre-fetch {
        ui_error "${name} @${version} requires OS X 10.10 or later"
        return -code error "incompatible OS X version"
    }
}

github.livecheck.regex  {([^"-]+)}
