# -*- 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               gnu_info 1.0
PortGroup               legacysupport 1.1

name                    gawk
version                 5.4.1
revision                0
categories              lang
license                 GPL-3+
installs_libs           no
maintainers             {mps @Schamschula} openmaintainer
master_sites            gnu
homepage                https://www.gnu.org/software/${name}
description             The GNU awk utility
long_description \
    The awk utility interprets a special-purpose programming language \
    that makes it possible to handle simple data-reformatting jobs \
    with just a few lines of code. It is a free, extended version of awk.
use_xz                  yes
checksums               rmd160  929ac99cc1bcb8015b7cc0701f29fdf812833c75 \
                        sha256  07f6f7342b7febe4313fc2c2542ad93d64fe20ad8717200109f105a826f5fd37 \
                        size    3838416

# https://trac.macports.org/ticket/72587
patchfiles              patch-_NSGetExecutablePath.diff

if [string match *gcc-4.* ${configure.compiler}] {
    patchfiles-append   patch-gcc4.diff
}

depends_build           port:gettext

depends_lib-append      port:gettext-runtime

configure.args          --with-libiconv-prefix=${prefix} \
                        --without-mpfr \
                        --without-readline \
                        ac_cv_libsigsegv=no \
                        ac_cv_prog_AWK=awk

platform darwin {
    # https://trac.macports.org/ticket/65944
    if {${os.major} < 10} {
        post-patch {
            reinplace   "s:-Xlinker -no_pie::" ${worksrcpath}/configure
        }
    }
    if {${os.major} < 9} {
        # Hopefully upstream fixes this silly bug in the next release...
        patchfiles-replace \
                        patch-_NSGetExecutablePath.diff \
                        patch-tiger.diff
        configure.cppflags-append \
                        -D__DARWIN_UNIX03
    }
    # gawkmisc.c:363:60: error: passing argument 2 of '_NSGetExecutablePath'
    # from incompatible pointer type [-Wincompatible-pointer-types]
    if {[string match macports-gcc* ${configure.compiler}]} {
        configure.cflags-append \
                        -Wno-error=incompatible-pointer-types
    }
}

configure.checks.implicit_function_declaration.whitelist-append strchr

test.run                yes
test.target             check

post-destroot {
    delete ${destroot}${prefix}/bin/awk

    xinstall -m 755 -d ${destroot}${prefix}/libexec/gnubin
    foreach binary [glob -tails -directory ${destroot}${prefix}/bin g*] {
        ln -s ${prefix}/bin/${binary} ${destroot}${prefix}/libexec/gnubin/[string range $binary 1 end]
    }
    xinstall -m 755 -d ${destroot}${prefix}/libexec/gnubin/man/man1
    foreach manpage [glob -tails -directory ${destroot}${prefix}/share/man/man1 g*] {
        ln -s ${prefix}/share/man/man1/${manpage}.gz ${destroot}${prefix}/libexec/gnubin/man/man1/[string range $manpage 1 end].gz
    }
}

variant mpfr description "Add mpfr support" {
    depends_lib-append  port:gmp \
                        port:mpfr

    configure.args-delete   --without-mpfr
}

notes "
readline support has been removed from gawk. If you need to run gawk interactively,\
install rlwrap:

sudo port install rlwrap

and run gawk using rlwrap:

rlwrap gawk ...
"
