# -*- 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                happy
version             2.2
revision            0
categories          devel haskell
maintainers         nomaintainer
license             GPL-3
platforms           macosx
homepage            https://github.com/simonmar/happy

description         A parser generator for Haskell
long_description    Happy is a parser generator for Haskell. Given a\
                    grammar specification in BNF, Happy generates\
                    Haskell code to parse the grammar. Happy works in\
                    a similar way to the yacc tool for C.

checksums           rmd160  19157e43be1e3327457bfc9107e9a7706fcc8e95 \
                    sha256  2e9345c99a61bc29b5a1b9d5c1ea791cbea219499a4c01ed71f33c3af34a5eb0 \
                    size    62273

# relative paths to ${prefix}
set happy_datadir   share/${name}

set build_store_root        {}
set build_store_dirname     {}
set build_happy_datadir     {}

# Fix for cabal data-files hardcoded path in binary
# See:
# https://github.com/commercialhaskell/stack/issues/848
# https://github.com/commercialhaskell/stack/issues/4857
# https://github.com/haskell/cabal/issues/462
# https://github.com/haskell/cabal/issues/3586
post-extract {
    xinstall -m 0644 -W ${worksrcpath} \
                    ${filespath}/Paths_NAME.hs ./app/Paths_${name}.hs

    reinplace "s|@PREFIX@|${prefix}|g" \
                    ${worksrcpath}/app/Paths_${name}.hs
    reinplace "s|@NAME@|${name}|g" \
                    ${worksrcpath}/app/Paths_${name}.hs
    reinplace -E "s|(Version\[\[:space:\]\]+)\\\[\[\[:digit:\]\]+(,\[\[:digit:\]\]+){1,4}\\\]|\\1\[[join [split ${version} .] ,]\]|" \
                    ${worksrcpath}/app/Paths_${name}.hs
}

variant stack \
    description {Use stack to build.} {}

if { [variant_isset "stack"] } {
    PortGroup       haskell_stack 1.0

    post-extract {
        if {[geteuid] == 0} {
            xinstall -o ${macportsuser} -d \
                    "[option haskell_stack.stack_root]"
        } else {
            xinstall -d \
                    "[option haskell_stack.stack_root]"
        }
    }

    set build_store_root    ${worksrcpath}/.stack-work
    set build_store_dirname "${name}-${version}"
} else {
    PortGroup       haskell_cabal 1.0

    variant haskell_cabal_use_prebuilt \
        description {Use prebuilt cabal and ghc. This is a necessary\
            default variant because happy is used to bootstrap ghc\
            and ghc is used to bootstrap cabal.} {
        haskell_cabal.use_prebuilt  yes
    }
    default_variants-append \
                    +haskell_cabal_use_prebuilt

    set build_store_root    ${haskell_cabal.cabal_root}/store
    set build_store_dirname share

    build.post_args-append \
                    --bindir=${prefix}/bin \
                    --datadir=${prefix}/share/${name}
}

post-destroot {
    if {[geteuid] == 0} {
        xinstall -o ${macportsuser} -d \
                    ${destroot}${prefix}/${happy_datadir}
    } else {
        xinstall -d ${destroot}${prefix}/${happy_datadir}
    }

    # find cabal data-files
    fs-traverse f ${build_store_root} {
        if { [file isdirectory ${f}]
            && [file tail ${f}] eq ${build_store_dirname}} {
            set build_happy_datadir ${f}
            break
        }
    }
    foreach f [glob ${build_happy_datadir}/*] {
        if {[file isfile ${f}]} {
            xinstall -m 0644 ${f} \
                    ${destroot}${prefix}/${happy_datadir}
        }
    }
}
