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

# man-db.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Written by B. Watson (yalhcru@gmail.com)
#
# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.
#
# 20171128 volkerdi:
# Make some changes to the SlackBuild to bring it in line with the usual
# Slackware style, and then pull it into Slackware. Thanks to B. Watson.
# License remains WTFPL, because WTF not :-) I am adding the offical WTFPL
# no warranty disclaimer, however:
#
# /* This program is free software. It comes without any warranty, to
#  * the extent permitted by applicable law. You can redistribute it
#  * and/or modify it under the terms of the Do What The Fuck You Want
#  * To Public License, Version 2, as published by Sam Hocevar. See
#  * http://www.wtfpl.net/ for more details. */
#

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

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

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Set paths for the package's install locations:
BINDIR=/usr/bin
MANDIR=/usr/man

# Version of man2html:
MAN2HTML=${MAN2HTML:-20180101}

### Build libpipeline #####################
# libpipeline is a pretty niche-market library. It used to be bundled
# in the man-db source, and as far as I can tell, nothing else uses it.
# Instead of making it a separate build and external dep, we'll just
# bundle it here.
LIBNAM=libpipeline
pushd $CWD
LIBVER=${LIBVER:-$(echo $LIBNAM-*.tar.!(*sign|*asc|*sig) | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
echo $LIBVER
popd

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

# Configure:
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --sysconfdir=/etc \
   --localstatedir=/var \
   --mandir=/usr/man \
   --docdir=/usr/doc/$PKGNAM-$VERSION/$LIBNAM-$LIBVER \
   --host=${SLK_ARCH_HOST} \
   --build=${SLK_ARCH_BUILD} || failconfig

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

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

mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION/$LIBNAM-$LIBVER
cp -fav COPYING* ChangeLog NEWS* README* TODO* \
  $PKG/usr/doc/$PKGNAM-$VERSION/$LIBNAM-$LIBVER

### Build man-db #####################

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

#
# Apply Patches:
#
# Apply patches:
# When called with --quiet, man-db should not warn over long whatis:
cat $CWD/man-db.quiet.whatis.warnings.patch | patch -p1 --verbose || exit 1

# Do not pass -i (ignore case) option to less:
cat $CWD/man-db.no.less.i.option.patch | patch -p1 --verbose || exit 1

# Configure:
#
# The circumlocutions below are needed because doing it the sane way:
# libpipeline_LIBS="-L$PKG/usr/lib$LIBDIRSUFFIX -lpipeline"
# gets broken by libtool (it *insists* on replacing -lpipeline with
# /usr/lib64/libpipeline.so, which will fail if it's an older version).
LD_LIBRARY_PATH="$PKG/usr/lib$LIBDIRSUFFIX" \
LDFLAGS="$PKG/usr/lib$LIBDIRSUFFIX/libpipeline.so" \
libpipeline_CFLAGS="-I$PKG/usr/include" \
libpipeline_LIBS="-L$PKG/usr/lib$LIBDIRSUFFIX $PKG/usr/lib$LIBDIRSUFFIX/libpipeline.so" \
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --disable-setuid \
   --disable-cache-owner \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --sysconfdir=/etc \
   --localstatedir=/var \
   --mandir=$MANDIR \
   --bindir=$BINDIR \
   --sbindir=${BINDIR/bin/sbin} \
   --docdir=/usr/doc/$PKGNAM-$VERSION \
   --host=${SLK_ARCH_HOST} \
   --build=${SLK_ARCH_BUILD} || failconfig

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

# Install in to package:
make install-strip DESTDIR=$PKG || failinstall

# Add man2html
( cd $PKG/usr/bin
  tar --strip-components=1 -xvf $CWD/other-scripts-${MAN2HTML}.tar.!(*sign|*asc|*sig) \
    other-scripts-${MAN2HTML}/man2html
  chmod 755 man2html )

# This stuff is for systemd, we don't need it (and it might confuse
# people coming from systemd distros):
rm -rf $PKG/usr/lib/tmpfiles.d/

# No special ownership or perms needed here since we --disable-setuid
mkdir -p $PKG/var/cache/man

# Only ship a daily cronjob, don't need a weekly one like Debian has.
mkdir -p $PKG/etc/cron.daily
install -vpm755 $CWD/$PKGNAM.cron $PKG/etc/cron.daily/$PKGNAM

# Choose a pager (defaults to 'most'):
mkdir -p $PKG/etc/profile.d
cp -a $CWD/man-db.sh $CWD/man-db.csh $PKG/etc/profile.d
chmod 755 $PKG/etc/profile.d/*
mv -fv $PKG/etc/profile.d/man-db.csh $PKG/etc/profile.d/man-db.csh.new
mv -fv $PKG/etc/profile.d/man-db.sh $PKG/etc/profile.d/man-db.sh.new

# Modified default config, customized for Slackware. See the conf file for
# list of changes.
cat $CWD/man_db.conf.new \
  | sed "s,@LIBDIRSUFFIX@,$LIBDIRSUFFIX,g" \
  > $PKG/etc/man_db.conf.new
# Get rid of any existing file put there by "make install":
rm -rf $PKG/etc/man_db.conf

# Add documentation:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
   ChangeLog FAQ* NEWS* README* COPYING* docs/HACKING* docs/INSTALL.quick \
   docs/TODO* docs/*lsm docs/*example* \
   $PKG/usr/doc/$PKGNAM-$VERSION
#changelogliposuction ChangeLog $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 -p      # run makepkg -l y -c n -p (for symlink pre-pend code)

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