# -*- 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.0.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  2359b874fac29654a892159d5ea86e33f77004a2 \
                    sha256  8e7aa8823f9c0d9c4c75b69051ebd405b29e2f1a8a1862fcc96df79578ffea1d \
                    size    12912528
} elseif {${build_arch} eq {x86_64}} {
    checksums       ${distfile_haskell} \
                    rmd160  fe1302dcb42b8d15566572900c639acc3b8ddac7 \
                    sha256  d3a74742caf998c1627a457de095c7d263680d96d38a7582cd3f9746bff76423 \
                    size    6076988
}

# 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  f2adc18aa2b8469f2dece0eee4670ad3541e14ea \
                    sha256  59c59ae925f2598f94c6d575c52dbf90f5d8371222f39664a61d431cf3938fd7 \
                    size    4072960

    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 313
    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"
