#!/bin/bash

# Slackware build script for nullidentd

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

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

# This is "mature" software. It's feature-complete, and hasn't been
# updated since last century. I'm effectively repackaging the Debian
# package, which is maintained and would receive security updates, if
# any were needed. The _5 in VERSION matches the -5 Debian patchlevel.

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

PRGNAM=nullidentd
VERSION=${VERSION:-1.0_5}
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"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
else
  SLKCFLAGS="-O2"
fi

set -e

SRCVER="$( echo $VERSION | cut -d_ -f1 )"

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$SRCVER
tar xvf $CWD/${PRGNAM}_$SRCVER.orig.tar.gz
cd $PRGNAM-$SRCVER
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 {} +

# 20240907 bkw: patches came from Debian's -5 build.
patch -p1 < $CWD/01_random_usernames.diff
patch -p1 < $CWD/02_makefile.diff

# 20240907 bkw: use our flags, build stripped binary, fix a warning.
sed -i "s/-O2/$SLKCFLAGS -Wl,-s/" Makefile
sed -i '1i#include <string.h>' $PRGNAM.c

mkdir -p $PKG/usr/{sbin,man/man8}
make install DESTDIR=$PKG

gzip -9 < $CWD/$PRGNAM.8 > $PKG/usr/man/man8/$PRGNAM.8.gz

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a CHANGELOG COPYING README $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh
cat $CWD/douninst.sh > $PKG/install/douninst.sh

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