# -*- 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                alex
version             3.5.1.0
revision            0
categories          devel haskell
maintainers         nomaintainer
license             GPL-3
platforms           macosx
homepage            https://www.haskell.org/alex/

description         A tool for generating lexical analysers in Haskell
long_description    Alex is a tool for generating lexical analysers in \
                    Haskell.  It takes a description of tokens based \
                    on regular expressions and generates a Haskell \
                    module containing code for scanning text \
                    efficiently. It is similar to the tool lex or flex \
                    for C/C++.

checksums           rmd160  01e33e533c4c2ac9c4aec30df6f77a88b468033f \
                    sha256  c92efe86f8eb959ee03be6c04ee57ebc7e4abc75a6c4b26551215d7443e92a07 \
                    size    85982

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

set build_store_root    {}
set build_store_dirname {}
set build_alex_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 alex 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}/${alex_datadir}
    } else {
        xinstall -d ${destroot}${prefix}/${alex_datadir}
    }

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