# -*- 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 2.15.7 v
revision            0

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

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  30fd583d71a67268c9c339c8cd1fe86934c90e6f \
                    sha256  a508663e2bd92c1b6326ce313c623c2fc2d91d9dec962e88d953b2dc49a78b20 \
                    size    949917

switch ${build_arch} {
    arm64 {
        checksums-append \
                    ${distname}-bin \
                    rmd160  48a120674125753d8d04271b2184c168ecc45932 \
                    sha256  c111bd0e057fe0efd60b71b57c96871056564904267581a965d889dbe2422c6e \
                    size    100297696
    }
    x86_64 {
        checksums-append \
                    ${distname}-bin \
                    rmd160  31bbc224031d399ecf23eca8eb6bdc71acd8df86 \
                    sha256  4828cb2726e8ce721791eb75fa933ac3b3ec954ffc6734997e95cb7584136a11 \
                    size    35052472
    }
    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
}
