#!/bin/bash

# Slackware build script for iselect

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20241203 bkw: update for v1.4.2.
# - use Debian URL to avoid content-disposition kerfluffle.
# - don't use Debian packaging, just the raw source.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=iselect
VERSION=${VERSION:-1.4.2}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
# 20241203 bkw: use own top-level dir with sane name.
rm -rf $PRGNAM-$VERSION
mkdir -p $PRGNAM-$VERSION
cd $PRGNAM-$VERSION
tar xvf $CWD/${PRGNAM}_$VERSION.orig.tar.gz --strip-components=1
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} + -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} +

# 20241203 bkw: I hate this. Upstream claims our autoconf is too
# old... but it works just fine. I guess he just didn't test it, and
# put the version number he happened to have installed.
sed -i '/^AC_PREREQ/s,2\.71,2.69,' configure.ac

autoreconf -if

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --disable-static \
  --build=$ARCH-slackware-linux

make CFLAGS="$SLKCFLAGS -fcommon" LDFLAGS="-Wl,-s"
# warning: DESTDIR not supported here!
make install prefix=$PKG/usr
gzip -9 $PKG/usr/man/man*/*

# 20241203 bkw: This used to be provided separately as part of
# Debian's packaging. It's now included in the src, but not installed
# by default.
mv example/screen-ir/screen-ir $PKG/usr/bin
gzip -9c < example/screen-ir/screen-ir.1 > $PKG/usr/man/man1/screen-ir.1.gz
rm -rf example/screen-ir

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a LICENSES README *.txt example $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
