# -*- 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.7.1 v
github.tarball_from archive
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  dba37a3031230505699e8c4f755801ea4e1ebc73 \
                    sha256  e2ce0d053566634a426ba1916592dfcefe48bdebbfe6a0da07e23a79c0ed7759 \
                    size    1046005

switch ${build_arch} {
    arm64 {
        checksums-append \
                    ${distname}-bin \
                    rmd160  40737655247a75a8a77b3a7ae17f43f0f5d67636 \
                    sha256  ac6efa5be8e031af3926c9b8781ab9eb1b7423099b9ba866ae329c12c6a65045 \
                    size    107619736
    }
    x86_64 {
        checksums-append \
                    ${distname}-bin \
                    rmd160  e6c9f11cfeabe822869fb66e7cca801e728d8d6c \
                    sha256  5b2d9d3478f3aefa7ee4fe5defd1b8201a76345cf41037cf377175ef89e3670e \
                    size    35620704
    }
    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
}
