# -*- 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             1.20.1.1
revision            1
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  bccf9d0c8ef3028e25ff196f18349f1bac0bfd5e \
                    sha256  8b4e7dc5a6c5fd666f8f7163232931ab28746d0d17da8fa1cbd68be9e878881b \
                    size    183409

# 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 ./src/Paths_${name}.hs

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