# -*- 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

github.setup        jgm pandoc 3.8
revision            0
categories          textproc haskell
license             GPL-3
maintainers         {judaew @judaew} openmaintainer

description         Conversion between markup formats

long_description    Pandoc is a Haskell library for converting from \
                    one markup format to another, and a command-line \
                    tool that uses this library. It can read markdown \
                    and (subsets of) HTML, reStructuredText, LaTeX, \
                    DocBook, MediaWiki markup, and Textile, and it can \
                    write markdown, reStructuredText, HTML, LaTeX, \
                    ConTeXt, Docbook, OpenDocument, ODT, Word docx, \
                    RTF, MediaWiki, Textile, groff man pages, plain \
                    text, Emacs Org-Mode, AsciiDoc, EPUB (v2 and v3), \
                    FictionBook2, and S5, Slidy and Slideous HTML \
                    slide shows.

homepage            https://pandoc.org

installs_libs       no

github.tarball_from archive

# Note: When updating, please also update the checksums on legacy versions below
checksums           rmd160  12bace6f7b69fb57e160ca2b761e9f8fd7a3add6 \
                    sha256  2d375526e7d26d46ebcdaf107564c8bbfd68e40a7af425906e9612889699bcf6 \
                    size    7985699

depends_lib-append  port:libffi \
                    port:zlib

depends_run-append  port:groff

# https://github.com/jgm/pandoc/blob/main/tools/latex-package-dependencies.lua
# comm -23 <(pandoc lua ${worksrcpath}/tools/latex-package-dependencies.lua | xargs -I{} find ${prefix}/share/texmf-texlive -name {} | sort -u | xargs -I{} find {} -depth 1 -type f | xargs port provides | awk '{print $NF}' | sort -u) <(port echo rdepof:texlive-xetex rdepends:texlive-fonts-recommended | grep texlive | sort -u)
variant texlive \
    description {Use TeXLive; see "port notes" for more information.} {
    depends_run-append \
                    port:latexmk \
                    port:texlive-fonts-recommended \
                    port:texlive-xetex
}

variant full_latex_dependencies \
    requires texlive \
    description {Install all LaTeX dependencies.} {
    depends_run-append \
                    port:texlive-bin-extra \
                    port:texlive-context \
                    port:texlive-latex-extra \
                    port:texlive-luatex
}

notes-append       "${name} uses LaTeX to create PDFs.

The (minimized) variant +texlive uses MacPorts TeXLive.

For full LaTeX PDF support, please use the variant +full_latex_dependencies."

set minimum_major_version_for_source 20

if {(${os.platform} eq {darwin}
        && ${os.major} >= ${minimum_major_version_for_source})
        || ${os.platform} ne {darwin}} {
    compiler.blacklist-append \
                   {clang < 900}

    # https://trac.macports.org/ticket/48971
    variant stack \
        description {Use stack to build.} {}
    if { [variant_isset "stack"] } {
        PortGroup   haskell_stack 1.0

    } else {
        PortGroup   haskell_cabal 1.0

        # https://github.com/jgm/pandoc/blob/main/macos/make_macos_release.sh
        set cabalopts   {-fembed_data_files -fserver -flua}
        build.target    all
        build.post_args-prepend \
                    {*}${cabalopts}
        build.post_args-append \
                 --allow-newer

        destroot.target exe:${subport}
        destroot.post_args-prepend \
                    {*}${cabalopts}
        destroot.post_args-append \
                 --allow-newer

        post-destroot {
            # https://github.com/jgm/pandoc/blob/main/macos/Makefile
            ln -s   ${prefix}/bin/${subport} \
                    ${destroot}${prefix}/bin/${subport}-lua
            ln -s   ${prefix}/bin/${subport} \
                    ${destroot}${prefix}/bin/${subport}-server
            xinstall -d ${destroot}${prefix}/share/doc
            copy    ${worksrcpath}/doc \
                    ${destroot}${prefix}/share/doc/${subport}
            system -W ${worksrcpath} \
                     "${destroot}${prefix}/bin/${subport} -s COPYING.md -Vpagetitle=License -o \"${destroot}${prefix}/share/doc/${subport}/license.html\""
            xinstall -m 0644 -W ${worksrcpath} \
                    AUTHORS.md \
                    BUGS \
                    CONTRIBUTING.md \
                    MANUAL.txt \
                    README.md \
                    ${destroot}${prefix}/share/doc/${subport}
            xinstall -m 0644 \
                    ${worksrcpath}/pandoc-cli/man/pandoc.1 \
                    ${worksrcpath}/pandoc-cli/man/pandoc-lua.1 \
                    ${worksrcpath}/pandoc-cli/man/pandoc-server.1 \
                    ${destroot}${prefix}/share/man/man1
        }
    }

    test.run        yes
    test.pre_args   run
    test.args       test-${subport}
    test.post_args-prepend \
                    {*}${cabalopts}
    test.post_args-append \
                    {-- '-p markdown'}
} else {
    # binary support for older macOS versions
    github.tarball_from releases
    use_zip         yes
    distfiles       pandoc-${version}-${build_arch}-macOS${extract.suffix}
    set worksrcpath ${workpath}/pandoc-${version}-${build_arch}

    # set build_arch/os.arch/os.major to get binary arm64/x86_64 checksums
    # sudo port -d checksum pandoc os.arch=arm os.major=18 build_arch=arm64
    # sudo port -d checksum pandoc os.arch=i386 os.major=18 build_arch=x86_64
    # run `sudo port clean --all pandoc` afterwards
    switch ${build_arch} {
        arm64 {
            checksums \
                    rmd160  581076c3571db637afa2dd38aef00613b6a307db \
                    sha256  7cfaa04a57816397dd6498fd50ada01bdeb04feefab737da37d493fb30131533 \
                    size    42413815
        }
        x86_64 {
            checksums \
                    rmd160  287be41d01af683749b68f169d483eca91517678 \
                    sha256  07a7fb7db6346710f3c9b06e6eb0ce49a66afa0d1969aa0158ecb14d9eaecb20 \
                    size    23581194
        }
        default {
            known_fail  yes
            pre-fetch {
                ui_error "${subport} @ ${version} only supported for architectures ${supported_archs}"
                return -code error "Unsupported architecture: ${build_arch}"
            }
        }
    }

    use_configure   no

    build {}

    destroot {
        xinstall -m 0755 ${worksrcpath}/bin/${subport} ${destroot}${prefix}/bin
        # https://github.com/jgm/pandoc/blob/main/macos/Makefile
        ln -s   ${prefix}/bin/${subport} \
                    ${destroot}${prefix}/bin/${subport}-lua
        ln -s   ${prefix}/bin/${subport} \
                    ${destroot}${prefix}/bin/${subport}-server
        xinstall -m 0755 {*}[glob ${worksrcpath}/share/man/man1/*] ${destroot}${prefix}/share/man/man1
    }
}
