# -*- 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.14.1.1
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  65b2c1d87a22c59da3e58d6f9e03f91671f83a8b \
                    sha256  bd40920fb3d5bcf3d78ce93445039ba43bc5edf769c52234223f25b83e3cc682 \
                    size    12728880
} elseif {${build_arch} eq {x86_64}} {
    checksums       ${distfile_haskell} \
                    rmd160  d4b3a14b37d0b56964354550cfb48a1848be6e3c \
                    sha256  3690d8f7aa368141574f9eaf8e75bc26932ed7b422f5ade107d6972b3b72532f \
                    size    6065364
}

# 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  667727335b6372a7b927d3bdf016cd2db631a339 \
                    sha256  c5d3e5bdf78c2fce9f6b48d31d4561ef151587fc3c102e57a3c4b341578a836a \
                    size    3638559

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

    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"
