# -*- 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               clang_dependency 1.0
PortGroup               compiler_blacklist_versions 1.0
PortGroup               gnulib 1.0
PortGroup               muniversal 1.0

name                    gettext
version                 0.22.5
checksums               rmd160  ca4af263902a8e419a399639a6e77fb40307ffdc \
                        sha256  ec1705b1e969b83a9f073144ec806151db88127f5e40fe5a94cb6c8fa48996a0 \
                        size    26861674

categories              devel
maintainers             {ryandesign @ryandesign}
homepage                https://www.gnu.org/software/gettext/
master_sites            gnu
platforms               darwin freebsd linux
use_parallel_build      yes

# https://trac.macports.org/ticket/31167
compiler.blacklist-append   {clang < 211.10.1}

# Also needed by later clangs.
if {${os.platform} eq "darwin" && ${os.major} < 11 && ${cxx_stdlib} eq "libc++"} {
    clang_dependency.extra_versions 3.7
}

configure.args-append   ac_cv_prog_AWK=/usr/bin/awk \
                        ac_cv_path_GMSGFMT=: \
                        ac_cv_path_GREP=/usr/bin/grep \
                        ac_cv_path_MSGFMT=: \
                        ac_cv_path_MSGMERGE=: \
                        ac_cv_path_SED=/usr/bin/sed \
                        ac_cv_path_XGETTEXT=: \
                        --disable-csharp

# https://trac.macports.org/wiki/WimplicitFunctionDeclaration#strchr
configure.checks.implicit_function_declaration.whitelist-append strchr

if {${subport} in "${name} ${name}-runtime ${name}-tools-libs"} {
    set localbindir     ${workpath}/bin

    post-patch {
        file mkdir ${localbindir}

        # Don't find any Java programs.
        foreach prog {gcj java javac jikes} {
            set fp [open ${localbindir}/${prog} w]
            puts ${fp} {#!/bin/sh}
            puts ${fp} {exit 1}
            close ${fp}
            file attributes ${localbindir}/${prog} -permissions a+x
        }
    }

    configure.env-append \
                        PATH=${localbindir}:$env(PATH)

    configure.args-append \
                        --disable-java
}

test.run                yes
test.target             check

subport libasprintf {
    revision                0

    license                 LGPL-2.1+

    description             autosprintf library, part of gettext

    long_description        ${subport} makes the C formatted output \
                            routines (fprintf et al.) usable in C++ \
                            programs, for use with <string> strings \
                            and <iostream> streams. This library is \
                            part of gettext.

    worksrcdir              ${distname}/${name}-runtime/${subport}
}

subport libtextstyle {
    revision                0

    license                 GPL-3+

    description             a text styling library, part of gettext

    long_description        ${subport} provides an easy way to add styling \
                            to programs that produce output to a console or \
                            terminal emulator window. This library is part \
                            of gettext and a prerequisite for tools like \
                            msgfmt and friends.

    worksrcdir              ${distname}/${subport}

    depends_lib-append      port:libiconv \
                            port:ncurses
}

subport ${name}-runtime {
    revision                0

    # libintl is LGPL; programs are GPL
    license                 {LGPL-2.1+ GPL-3+}

    description             GNU internationalization (i18n) and \
                            localization (l10n) runtime library

    long_description        ${subport} provides libintl, the ${description}. \
                            It is part of gettext, which is used for writing \
                            multilingual programs. To build applications that \
                            use gettext, the tools provided by the separate \
                            gettext port are required.

    worksrcdir              ${distname}/${subport}

    depends_lib-append      port:libiconv

    configure.args-append   --disable-libasprintf \
                            --with-included-gettext

    post-destroot {
        delete ${destroot}${prefix}/lib/charset.alias
        delete ${destroot}${prefix}/share/locale/locale.alias
    }
}

subport ${name}-tools-libs {
    revision                0

    description             GNU internationalization (i18n) and \
                            localization (l10n) tools libraries

    long_description        ${subport} provides the required libraries \
                            for tools like msgfmt from gettext

    post-destroot {
        # Remove the executables provided by the gettext subport.
        # Just leave the libraries and headers for the tools.
        delete {*}[glob ${destroot}${prefix}/bin/*] \
            {*}[glob ${destroot}${prefix}/share/*]
    }
}

if {${subport} eq ${name}} {
    epoch                   2
    revision                0

    description             GNU internationalization (i18n) and \
                            localization (l10n) tools

    long_description        ${name} provides the tools like msgfmt for \
                            writing multilingual applications. The tools \
                            are required to build applications which use \
                            gettext. The runtime library libintl is in \
                            the separate gettext-runtime port.

    depends_lib-append      port:${name}-tools-libs

    installs_libs           no

    post-destroot {
        # Remove the libraries and headers which are provided
        # by gettext-tools-libs
        delete {*}[glob ${destroot}${prefix}/lib/*] \
            {*}[glob ${destroot}${prefix}/include/*]
    }

    livecheck.type      regex
    livecheck.url       https://ftp.gnu.org/gnu/gettext/?C=M&O=D
    livecheck.regex     ${name}-(\[0-9.\]+)\\.tar
} else {
    livecheck.type      none
}

if {${subport} in [list ${name} ${name}-tools-libs]} {
    license                 GPL-3+

    depends_build-append    port:ncurses

    depends_lib-append      port:libiconv \
                            port:libtextstyle \
                            port:${name}-runtime

    configure.args-append   --disable-openmp \
                            --with-included-libunistring \
                            --with-included-libxml \
                            --with-installed-libtextstyle \
                            --without-emacs

    # Don't use possibly installed cvs or git to create the autopoint
    # archive because the archives they create for each architecture
    # under muniversal will differ and cannot be merged.
    configure.args-append   --without-cvs \
                            --without-git

    # Don't use xz because then xz would be required by various ports
    # that just want to use gettext.
    configure.args-append   --without-xz

    # Prevent the lang-c++ test from failing.
    # TODO: report this to the developers; there's no reason why
    # libasprintf should be getting built again. Might be the same as
    # https://savannah.gnu.org/bugs/?58669
    configure.args-append   --disable-libasprintf

    # Install from the tools subpackage, so that only its files
    # are installed.
    destroot.dir           ${worksrcpath}/${name}-tools
}
