# Build recipe for ncurses.
#
# Copyright (c) 2016-2022 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=ncurses
version=6.3-20220115
release=2

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

tarname=${program}-${version}.tgz
termcap_file=termcap-20210816.src.gz

# Remote source(s)
fetch="
  https://invisible-mirror.net/archives/ncurses/current/$tarname
  https://invisible-mirror.net/archives/ncurses/current/$termcap_file
"

description="
Emulation library of Curses.

The Ncurses (new curses) library is a free software emulation of
curses in System V Release 4.0.  The ncurses code was developed
under GNU/Linux.  It has been in use for some time with OpenBSD
as the system curses library, and on FreeBSD and NetBSD as an
external package.

The distribution includes the library and support utilities,
including a terminfo compiler \"tic\", a decompiler \"infocmp\",
clear, tput, tset, and a termcap conversion tool \"captoinfo\".
"

homepage=https://www.gnu.org/software/ncurses
license=Custom

# Source documentation
docs="ANNOUNCE AUTHORS COPYING NEWS README TO-DO VERSION"
docsdir="${docdir}/${program}-${version}"

_DEFAULT_NCURSES_BUILD_OPTIONS="
  --libdir=/usr/lib${libSuffix} \
  --mandir=$mandir \
  --enable-overwrite \
  --enable-symlinks \
  --enable-pc-files \
  --enable-xmc-glitch \
  --enable-colorfgbg \
  --enable-hard-tabs \
  --disable-stripping \
  --with-pkg-config-libdir=/usr/lib${libSuffix}/pkgconfig \
  --with-manpage-format=normal \
  --with-shared \
  --with-termlib=tinfo \
  --with-ticlib=tic \
  --with-gpm \
  --without-tests \
  --without-debug \
  --without-profile \
  --build="$(gcc -dumpmachine)"
"

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

    cd "$srcdir"

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

    unset -v TERMINFO;	# Just in case.

    ./configure CPPFLAGS="$QICPPFLAGS -D_GNU_SOURCE" \
    CFLAGS="$QICFLAGS" CXXFLAGS="$QICXXFLAGS" LDFLAGS="$QILDFLAGS" \
     $configure_args --includedir=/usr/include/ncurses \
      $_DEFAULT_NCURSES_BUILD_OPTIONS \
       --with-manpages --enable-widec --with-normal

    make -j${jobs} V=1
    make -j${jobs} DESTDIR="$destdir" install

    unset -v _DEFAULT_NCURSES_BUILD_OPTIONS

    # Provide non-widec compatibility

    for name in form panel menu
    do
        rm -f "${destdir}/usr/lib${libSuffix}/lib${name}.so" \
              "${destdir}/usr/lib${libSuffix}/lib${name}.a"

        echo "INPUT(-l${name}w)" > \
         "${destdir}/usr/lib${libSuffix}/lib${name}.so"
        echo "INPUT(-l${name}w)" > \
         "${destdir}/usr/lib${libSuffix}/lib${name}.a"

        (
            cd "${destdir}/usr/lib${libSuffix}/pkgconfig" && \
             ln -sf ${name}w.pc ${name}.pc
        )
    done
    unset -v name

    # Do not leave behind this symbolic link for pkgconfig
    ln -sf ncursesw.pc "${destdir}/usr/lib${libSuffix}/pkgconfig/ncurses.pc"

    # Make symlinks for compatibility
    (
        cd "${destdir}/usr/include" || exit 1
        ln -sf ncurses ncursesw
        for header in ncurses/*
        do
            ln -sf $header .
        done
    )

    # Insert termcap file provided by "Thomas E. Dickey"
    mkdir -p "${destdir}/etc"
    zcat "${tardir}/$termcap_file" > "${destdir}/etc/termcap"
    chmod 644 "${destdir}/etc/termcap"
    unset -v termcap_file

    touch "${destdir}/etc/.graft-config"

    # Make sure to link against -ltinfo
    (
        major_version=$(echo $version | cut -f 1 -d .)

        cd "${destdir}/usr/lib${libSuffix}" || exit 1
        rm -f libncursesw.so libncurses.so libncurses.a libtermcap.so libtermcap.a

        echo "INPUT(libncursesw.so.${major_version} -ltinfo)" > libncurses.so
        echo "INPUT(libncursesw.a -ltinfo)" > libncurses.a

        echo "INPUT(libncursesw.so.${major_version} -ltinfo)" > libncursesw.so

        echo "INPUT(-ltinfo)" > libtermcap.so
        echo "INPUT(-ltinfo)" > libtermcap.a
    )

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip --force -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"
}

