# -*- 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                cabal
version             3.16.1.0
revision            0
categories          lang haskell devel
license             BSD
maintainers         {ieee.org:s.t.smith @essandess} openmaintainer

description         Haskell Cabal: Common Architecture for \
                    Building Applications and Libraries

long_description    Cabal is a system for building and packaging \
                    Haskell libraries and programs. It defines a \
                    common interface for package authors and \
                    distributors to easily build their applications in \
                    a portable way. Cabal is part of a larger \
                    infrastructure for distributing, organizing, and \
                    cataloging Haskell libraries and programs.

homepage            https://www.haskell.org/cabal

master_sites        https://downloads.haskell.org/~${name}/${name}-install-${version}/:haskell

installs_libs       no

platform arm {
    set cabal_build_arch    aarch64
}
platform i386 {
    set cabal_build_arch    x86_64
}
platform powerpc {
    set cabal_build_arch    ppc_not_supported_by_cabal
}

if {${os.arch} ni [list arm i386]} {
    known_fail      yes
    pre-fetch {
        ui_error "Cabal is only available on macOS \
            with [join ${supported_archs} " or "] architectures."
        error {unsupported platform}
    }
}

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

set cabal_distname  ${name}-install-${version}-${cabal_build_arch}-darwin
set distfile_haskell    ${cabal_distname}.tar.xz
distfiles           ${distfile_haskell}:haskell

# set build_arch by hand on arm64/x86_64 systems to get x86_64/arm64 checksums
# sudo port -d checksum cabal-prebuilt os.arch=i386 build_arch=x86_64
# sudo port -d checksum cabal-prebuilt os.arch=arm build_arch=arm64
# run `port clean --all cabal-prebuilt` afterwards
if {${build_arch} eq {arm64}} {
    checksums       ${distfile_haskell} \
                    rmd160  61281c223f06baadec48ef12db26fcd7c27ddcaa \
                    sha256  e02f4561fbce72b198a3c6c81b9f211f9c7cbf40c073f8f2ee59f835dd1dd502 \
                    size    12902504
} elseif {${build_arch} eq {x86_64}} {
    checksums       ${distfile_haskell} \
                    rmd160  ce1d47508513f7bf423082ca6b1b5ba7e60e89f3 \
                    sha256  e09fec9aa6379d79a749d337446fa72f03f880a577d149c7b039592860bea095 \
                    size    6095212
}

# cabal may use these MacPorts build tools:
# alex, ar, strip, cpphs, happy, hscolour, ld, pkg-config
# https://github.com/haskell/cabal/blob/master/Cabal/src/Distribution/Simple/Program/Builtin.hs
depends_lib-append  port:cctools \
                    port:ld64 \
                    port:pkgconfig

# cabal uses curl or wget
depends_lib-append  port:curl \
                    port:wget

if {${name} eq ${subport}} {
    PortGroup       haskell_cabal 1.0

    haskell_cabal.use_prebuilt  yes

    set distfile_github \
                    [string totitle ${name}]-v${version}.tar.gz
    master_sites    https://github.com/haskell/${name}/archive/refs/tags:github
    distfiles       ${distfile_github}:github
    checksums       ${distfile_github} \
                    rmd160  99602c774cc2b902f4bc671ec01d3fc141ca8fa1 \
                    sha256  33cfe3367aa1a58bf13a3b5fd18a39efe5e4841a47e8f4e8b91ceb37be8a9d78 \
                    size    4098461

    extract.only-append \
                    ${distfile_github}
    if {[exists extract.rename]} {
        extract.rename  yes
    }
    variant prebuilt \
        description {This variant is deprecated.} {
        ui_error "Please install the port ${name}-prebuilt."
        return -code error "deprecated variant."
    }

    # use these to specify python versions, python3 required
    # use ${prefix}/var/macports/sources/rsync.macports.org/macports/release/tarballs/ports/_resources/port1.0/group/python-1.0.tcl
    set python3_version 314
    set python3_branch  [string index ${python3_version} 0].[string range ${python3_version} 1 end]
    set python3_prefix  ${frameworks_dir}/Python.framework/Versions/${python3_branch}
    set python3_bin     ${python3_prefix}/bin/python${python3_branch}

    depends_build-append \
                    port:python${python3_version} \
                    port:py${python3_version}-importlib-metadata \
                    port:py${python3_version}-parsing \
                    port:py${python3_version}-setuptools \
                    port:py${python3_version}-sphinx \
                    port:py${python3_version}-sphinx-jsonschema \
                    port:py${python3_version}-sphinx_rtd_theme \
                    port:py${python3_version}-sphinxnotes-strike \
                    port:py${python3_version}-tz \
                    port:py${python3_version}-yaml \
                    port:py${python3_version}-zipp

    depends_lib-append  \
                    port:gmp \
                    port:libiconv \
                    port:zlib

    # alex, happy, and hscolour are bootstrapped using cabal-prebuilt
    depends_lib-append  \
                    path:bin/alex:alex \
                    path:bin/happy:happy \
                    path:bin/HsColour:hscolour

    # use bootstrapped ghc -- reconfigure settings from PG haskell_cabal
    depends_build-delete \
                    port:ghc-prebuilt
    depends_lib-append \
                    path:bin/ghc:ghc

    post-extract {
        delete ${haskell_cabal.cabal_root}/bin/ghc
        delete ${haskell_cabal.cabal_root}/bin/ghc-pkg
    }
    
    haskell_cabal.env \
                    "CABAL_CONFIG=${haskell_cabal.cabal_root}/config" \
                    "GHC=${prefix}/bin/ghc"

    set project_file cabal.release.project

    haskell_cabal.update_flags-append \
                    --project-file=${project_file}

    build.target    ${name}-install \
                    --project-file=${project_file}
    # apparently required for MacPorts github CI and buildbots
    build.post_args-prepend \
                    --ghc \
                    --with-compiler=${prefix}/bin/ghc
    build.post_args-append \
                    --bindir=${prefix}/bin \
                    --datadir=${prefix}/share/${subport} \
                    --allow-newer
    post-build {
        # https://github.com/haskell/cabal/blob/c1f490a50782cc89030889fae8edd9f95db7c1e4/Makefile#L207
        system -W ${worksrcpath} \
            "sphinx-build-${python3_branch} -n --keep-going -E\
                    doc ${haskell_cabal.build_dir}/doc/users-guide"
    }

    # apparently required for MacPorts github CI and buildbots
    destroot.post_args-prepend \
                    --ghc \
                    --with-compiler=${prefix}/bin/ghc
    destroot.post_args-append \
                    --allow-newer

    post-destroot {
        xinstall -d ${destroot}${prefix}/share/doc/${distname}

        xinstall -d ${destroot}${prefix}/share/man/man1
        system -W ${destroot}${prefix}/share/man/man1 \
                    "${destroot}${prefix}/bin/${name} man --raw > ${name}.1"

        copy        ${haskell_cabal.build_dir}/doc/users-guide \
                    ${destroot}${prefix}/share/doc/${distname}
    }

    notes \
        "The Cabal User Guide is available at:

    file://${prefix}/share/doc/${distname}/users-guide/index.html
"
}

subport cabal-prebuilt {
    revision        0
    supported_archs arm64 x86_64

    use_configure   no
    build           {}

    destroot {
        move        ${workpath}/${name} \
                    ${destroot}${prefix}/bin/${subport}
    }

    notes \
        "The Cabal prebuilt binary is installed at `${prefix}/bin/${subport}`.\
It is intended to be used to bootstrap ghc and other Haskell-based tools.
"
}

livecheck.url       https://github.com/haskell/${name}/tags
livecheck.regex     "archive/refs/tags/${name}\-install\-v(\[^\"\]+)\.tar\.gz"
