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

name            grep
version         3.12
revision        0
categories      sysutils
license         GPL-3+
installs_libs   no
maintainers     {mps @Schamschula} openmaintainer
description     grep prints lines that contain a match for a pattern
long_description \
    Grep searches one or more input files for lines containing a match  \
    to a specified pattern. By default, grep prints the matching lines.
homepage        https://www.gnu.org/software/grep/grep.html
master_sites    gnu
use_xz          yes

checksums       rmd160  87fa03920037a53fc91ebfe29d9ca3b3779c8fc8 \
                sha256  2649b27c0e90e632eadcd757be06c6e9a4f48d941de51e7c0f83ff76408a07b9 \
                size    1918448

post-patch {
    # https://trac.macports.org/ticket/63381
    platform darwin 8 {
        reinplace "s|__ss.__r1|ss.r1|" lib/sigsegv.c
        reinplace "s|__ss.__esp|ss.esp|" lib/sigsegv.c
    }
}

# error: type name requires a specifier or qualifier
# error: expected member name or ';' after declaration specifiers
# error: expected ';' at end of declaration list
compiler.blacklist {clang < 500}

# Ensure system version of grep is used instead of a possibly broken MacPorts version.
configure.env   PATH=/usr/bin:$env(PATH)

configure.args  --disable-silent-rules \
                --program-prefix=g

# error: this system appears to support timestamps after mid-January 2038,
# but no mechanism for enabling wide 'time_t' was detected.
configure.args.i386-append  --disable-year2038
configure.args.ppc-append   --disable-year2038

platform darwin 8 {
    # Work around some /usr/include/sys/signal.h brokenness
    configure.cflags-append -D_MCONTEXT64_T -D_UCONTEXT64_T
}

depends_build   port:gettext

depends_lib     port:gettext-runtime \
                port:pcre2

configure.checks.implicit_function_declaration.whitelist-append \
                __fpending \
                MIN \
                strchr \
                unreachable

# Fix for Rosetta: https://trac.macports.org/ticket/64497
platform darwin 10 {
    if {${build_arch} eq "ppc"} {
        configure.args-append \
                --build=powerpc-apple-darwin${os.major}
    }
}

pre-test {
    reinplace "s|base64 -d|base64 --decode|g" ${worksrcpath}/tests/pcre-jitstack
}

test.run        yes
test.target     check

post-destroot {
    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 libsigsegv description {Use libsigsegv} {
    depends_lib-append \
                port:libsigsegv

    configure.args-append   --with-libsigsegv
}

notes "
This port previously installed itself without a g* prefix, thus overshadowing\
system binaries such as grep, fgrep, and egrep. The port is now changed so that\
it does install with a g* prefix, like other GNU ports. This means that you'll\
now find GNU grep at ${prefix}/bin/ggrep. If you dislike typing ggrep, you can\
create a shell alias or you can add ${prefix}/libexec/gnubin to your PATH,\
wherein non-g* prefixed symlinks are installed. In other words,\
${prefix}/libexec/gnubin contains GNU binaries without any prefix to the file\
names, so you can type grep and get GNU grep just as before.
"
