#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# ncurses.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright 2000-2025  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Detect whether we're building for /patches:
# This function sets the variable 'SLACKPATCHING'
slack_findpkgstore_is_stablerelease

# Temporary build locations:
shm_tmp # Use /dev/shm if >8GB RAM is available & not mounted 'noexec'
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
tar xf $CWD/$PKGNAM-*.tar.!(*sign|*asc|*sig)
cd $PKGNAM-*/ || failextract
slackhousekeeping

# Configure:
slack_autotoolsprep
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --mandir=/usr/man \
   --with-manpage-format=normal \
   --enable-widec \
   --enable-colorfgbg \
   --enable-hard-tabs \
   --enable-overwrite \
   --enable-xmc-glitch \
   --with-cxx-binding \
   --with-cxx-shared \
   --with-shared \
   --with-versioned-syms \
   --with-termlib=tinfo \
   --with-ticlib=tic \
   --without-debug \
   --without-normal \
   --without-profile \
   --enable-symlinks \
   --enable-pc-files \
   --with-pkg-config-libdir=/usr/lib${LIBDIRSUFFIX}/pkgconfig \
   --without-ada \
   --program-suffix="" \
   --program-prefix="" \
   --disable-root-access \
   --disable-setuid-environ \
   --host=${SLK_ARCH_HOST} \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
make $NUMJOBS || make || failmake

# Install into package framework:
make install DESTDIR=$PKG || failinstall

# I wonder if anything still looks here?
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}
( cd $PKG/usr/lib${LIBDIRSUFFIX}
  rm -rf terminfo
  ln -sf ../share/terminfo . )
# Upstream only uses /usr/lib/terminfo now. Make that too.
mkdir -p $PKG/usr/lib
( cd $PKG/usr/lib
  rm -rf terminfo
  ln -sf ../share/terminfo . )

# Make a termcap file in case anyone actually needs it:
echo "#       /etc/termcap" > misc/termcap
grep \$Revision: misc/terminfo.src >> misc/termcap
grep \$Date: misc/terminfo.src >> misc/termcap
echo "#" >> misc/termcap
echo "#       Converted from terminfo.src for Slackware:" >> misc/termcap
echo "#       tic -C -t terminfo.src > termcap" >> misc/termcap
echo "#" >> misc/termcap
echo "#       This file is included to support legacy applications using libtermcap." >> misc/termcap
echo "#       Modern applications will use ncurses/terminfo instead." >> misc/termcap
echo "#" >> misc/termcap
echo "#------------------------------------------------------------------------------" >> misc/termcap
echo "#" >> misc/termcap
tic -C -t misc/terminfo.src >> misc/termcap
touch -r misc/terminfo.src misc/termcap
mkdir -vpm755 $PKG/etc
install -vpm644 misc/termcap $PKG/etc/termcap

# Move the ncurses libraries into /lib (for no reason basically, because we DO
# NOT officially support a separate /usr partition):
mkdir -vpm755 $PKG/lib${LIBDIRSUFFIX}
( cd $PKG/usr/lib${LIBDIRSUFFIX}
  mv -fv lib*.so.?.? lib*.so.? $PKG/lib${LIBDIRSUFFIX}
  for file in *.so ; do
    if [ -L $file ]; then
       rm -f $file
       ln -vsf ../../lib${LIBDIRSUFFIX}/${file}.? $file
    fi
  done
  # Also link to the soname:
  for file in $PKG/lib${LIBDIRSUFFIX}/lib*.so.? ; do
    ln -vsf ../../lib${LIBDIRSUFFIX}/$(basename ${file}) .
  done
)

# Make sure anything requesting the non-wide libraries will be directed to
# the wide ones:
for library in form menu ncurses ncurses++ panel ; do
  # Add a loader script:
  echo "INPUT(-l${library}w)" > $PKG/usr/lib${LIBDIRSUFFIX}/lib${library}.so
  # Make a pkg-config file symlink:
  ln -vsf ${library}w.pc $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig/${library}.pc
done

# Support the obsolete:
echo "INPUT(-ltinfo)" > $PKG/usr/lib${LIBDIRSUFFIX}/libtermcap.so

# In case anything is linked to the non-wide ncurses libraries, make some
# compatibility symlinks:
( cd $PKG/lib${LIBDIRSUFFIX}
  SONAME=$(echo libncursesw.so.? | rev | cut -f 1 -d .)
  for library in form menu ncurses ncurses++ panel ; do
     ln -vsf lib${library}w.so.$SONAME lib${library}.so.$SONAME
  done
)

# Use linker loader scripts to make sure -tinfo dependency works:
( cd $PKG/usr/lib${LIBDIRSUFFIX}
  rm -fv libcurses.so libcursesw.so libncurses.so libncursesw.so libtermcap.so
  echo "INPUT(-lncurses)" > libcurses.so
  echo "INPUT(-lncursesw)" > libcursesw.so
  echo "INPUT(libncurses.so.6 -ltinfo)" > libncurses.so
  echo "INPUT(libncursesw.so.6 -ltinfo)" > libncursesw.so
  echo "INPUT(-ltinfo)" > libtermcap.so
)

# NOTE 2024-04-29: I'm not sure these changes are still needed.
# Maybe GazL knows.
#
# Use various upstream/fixed/better terminfo files to update the terminfo
# database. Only *.terminfo files in $CWD/terminfo will be used.
export TERMINFO=$PKG/usr/share/terminfo
for tfile in $CWD/terminfo/*.terminfo ; do
  if [ -r $tfile ]; then
     progs/tic -v $tfile
  fi
done
unset TERMINFO

# For ghostty terminfo (by default it sets TERM to xterm-ghostty):
if [ ! -L $PKG/usr/share/terminfo/x/xterm-ghostty ]; then
   ln -rvfs $PKG/usr/share/terminfo/g/ghostty $PKG/usr/share/terminfo/x/xterm-ghostty
fi

# Add documentation:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION/{c++,html}
cp -fav \
  ANNOUNCE AUTHORS COPYING* INSTALL MANIFEST NEWS README* TO-DO VERSION \
  $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
  c++/NEWS c++/PROBLEMS c++/README-first \
  $PKG/usr/doc/$PKGNAM-$VERSION/c++/
cp -fav \
  doc/html/*.html \
  $PKG/usr/doc/ncurses-$VERSION/html/
changelogliposuction NEWS $PKGNAM $VERSION # Trim down a "ChangeLog" file

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall        # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i      # compress man & info pages and delete usr/info/dir
slackslack           # set standard Slackware file/dir permissions and ownerships
slackdesc            # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
