# -*- 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           haskell_stack 1.0
PortGroup           legacysupport 1.1

github.setup        commercialhaskell stack 3.3.1 v
# Change github.tarball_from to 'releases' or 'archive' next update
github.tarball_from tarball
revision            0

name                stack
categories          lang haskell
maintainers         {ieee.org:s.t.smith @essandess} openmaintainer
license             BSD

description         The Haskell Tool Stack
long_description    Stack is a cross-platform command line interface \
                    for developing Haskell projects. It features: \
                    installing GHC automatically, installing packages \
                    needed for your project, building your project, \
                    testing your project, benchmarking your project. \
                    It is intended for Haskellers both new and experienced.

homepage            https://haskellstack.org

variant prebuilt \
    description {Do not bootstrap stack; install the pre-built binary.} {}

if {${build_arch} eq "x86_64"} {
    distname        ${name}-${github.version}-osx-x86_64
} elseif {${build_arch} eq "arm64"} {
    distname        ${name}-${github.version}-osx-aarch64
}

worksrcdir          ${name}-${github.version}

master_sites        ${github.homepage}/releases/download/${github.tag_prefix}${github.version}/:release \
                    ${github.homepage}/archive/refs/tags/:archive

distfiles           ${distname}-bin:release \
                    ${github.tag_prefix}${github.version}${extract.suffix}:archive

extract.only        ${github.tag_prefix}${github.version}${extract.suffix}

# set build_arch by hand on arm64/x86_64 systems to get x86_64/arm64 checksums
# sudo port -d checksum stack +prebuilt os.arch=arm build_arch=arm64
# sudo port -d checksum stack +prebuilt os.arch=i386 build_arch=x86_64
# run `port clean --all stack` afterwards

checksums           ${github.tag_prefix}${github.version}${extract.suffix} \
                    rmd160  94f5ac344ef57bf46bb9184219ccb67625f941ec \
                    sha256  9a8dc9dd403fa8fd56339353091d438fd5d97ee6f2001a0cc11ba94b19271d98 \
                    size    975891

switch ${build_arch} {
    arm64 {
        checksums-append \
                    ${distname}-bin \
                    rmd160  5cd5a83351d676cbb15f1baac929b1772fddb258 \
                    sha256  0406ec239067d99ed0d47fc2c03fc1bd1fe63d052b91480f6d29141843a6fbd8 \
                    size    105068392
    }
    x86_64 {
        checksums-append \
                    ${distname}-bin \
                    rmd160  2342c6bafdf30eed2de7346b7e046f1cefa01315 \
                    sha256  1992bf54c16a82fc438012f6889bd5fb3d6970a51a19374477597f1ce7847a35 \
                    size    35511328
    }
    default {
        known_fail  yes
        pre-fetch {
            ui_error "${subport} @ ${version} only supported for architectures ${supported_archs}"  
            return -code error "Unsupported architecture: ${build_arch}"
        }
    }
}

checksums-append    ${distname}-bin.asc \
                    size    488

gpg_verify.use_gpg_verification \
                    yes

if {[option gpg_verify.use_gpg_verification]} {
    distfiles-append \
                    ${distname}-bin.asc:release

    post-checksum {
        # check GPG signature: https://docs.haskellstack.org/en/stable/SIGNING_KEY/
        set gpg_keyid 575159689befb442

        gpg_verify.verify_gpg_signature \
            ${filespath}/keyid-${gpg_keyid}.txt \
            ${distpath}/${distname}-bin.asc \
            ${distpath}/${distname}-bin
    }
}

post-extract {
    xinstall -W ${workpath} -d ./bin
    xinstall -m 0755 -W ${distpath} ./${distname}-bin ${workpath}/bin
}

# bootstrap binaries seg fault on 10.7 and older
set max_darwin 12
if { ${os.platform} eq "darwin" && ${os.major} < ${max_darwin} } {
    known_fail yes
    pre-fetch {
        ui_error "${name} @${version} requires OS X 10.[expr ${max_darwin} - 4] or later"
        return -code error "unsupported Mac OS X version"
    }
}

# Darwin 19 and older fails to build from source, e.g.
# Undefined symbols for architecture x86_64:
#  "_utimensat", referenced from:
#      _Lc6xB_info in libHSdirectory-1.3.6.0.a(Posix.o)
# and error: ld: unknown option: -no_fixup_chains
if { ${os.platform} eq "darwin" && ${os.major} <= 19 } {
    default_variants-append +prebuilt
}

if { [variant_isset "prebuilt"] } {
    use_configure   no

    build {}

    if { ${os.major} <= [option legacysupport.newest_darwin_requires_legacy] } {
        depends_build-append port:cctools
    }

    destroot {
        copy        ${workpath}/bin/${distname}-bin \
                    ${destroot}${prefix}/bin/${name}
        legacysupport::relink_libSystem ${destroot}${prefix}/bin/${name}
    }
} else {
    post-extract {
        xinstall -m 0755 -d "[option haskell_stack.stack_root]"

        # standard stack install with 'curl | sh'; don't use
        # system -W ${worksrcpath} "/bin/mkdir ./bin && /usr/bin/curl -sSL https://get.haskellstack.org/ | /bin/sh -s - -d ./bin"
        # bootstrap using the pre-built ./stack binary
        ln -s       ${workpath}/bin/${distname}-bin \
                    ${workpath}/bin/${name}
    }

    depends_build-append port:pkgconfig

    # use a modified zlib.pc that forces the system paths to be used
    # this prevents -L${prefix}/lib from being added to the library link path
    # which causes libiconv linkage errors
    build.env-append PKG_CONFIG_PATH=${filespath}/zlibsystempkgconfig

    set haskell_stack.bin ${workpath}/bin/stack
}