# Build recipe for gettext-tiny.
#
# Copyright (c) 2018-2020 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=gettext-tiny
version=20200322_0e62c25
release=1

# Define a category for the output of the package name
pkgcategory=tools

tarname=${program}-${version}.tar.lz

# Remote source(s)
fetch=https://rsync.dragora.org/current/sources/$tarname

description="
An internationalisation and localisation system.

gettext-tiny provides lightweight replacements for tools typically used
from the GNU gettext suite, which is incredibly bloated and takes a lot
of time to build (in the order of an hour on slow devices).  The most
notable component is msgfmt which is used to create binary translation
files in the .mo format out of textual input files in .po format.
This is the most important tool for building software from source,
because it is used from the build processes of many software packages.

The gettext-tiny package is compatible with the Musl C Library.
"

homepage=https://github.com/sabotage-linux/gettext-tiny/
license=MIT

# Source documentation
docs="LICENSE README.md"
docsdir="${docdir}/${program}-${version}"

# Limit paralell jobs to '1'
jobs=1

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    # Do not force to some tools to be static
    sed -i 's/-static//g' Makefile

    make -j${jobs} LIBINTL=MUSL \
     CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS" \
     prefix=/usr libdir=/usr/lib${libSuffix} \
     DESTDIR="$destdir" install

    strip --strip-unneeded "${destdir}/usr/bin"/* 2> /dev/null || true;
    strip --strip-debug "${destdir}/usr/lib${libSuffix}/libintl.a"

    # Introduce our custom version of 'autopoint' which
    # works well under busybox's ash, mksh shells.

    cp -p "${worktree}/archive/gettext-tiny/autopoint" \
          "${destdir}/usr/bin/"
    chmod 755 "${destdir}/usr/bin/autopoint"

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}"/
}

