#!/bin/bash

# Slackware build script for sslh

# Script originally created by mara <email removed>.
# Modified, updated, and now maintained by B. Watson <urchlay@slackware.uk>.
# Original script had no license. Modified version is licensed under
# the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20250706 bkw: normally I would go with the latest release, but there
# are some pretty important-looking fixes in the last few git commits.
# There's not a single line of mara's original script left, other than
# boilerplate code copied from the SBo template, so I don't feel bad
# relicensing it.

# 20250719 bkw:
# - updated for latest git, v2.2.4+20250630_11da63c.
# - fix almost everything: README, slack-desc, rc script...
# - allow libev to be optional and autodetected.
# - document option libbsd dep in README.
# - add /etc/default/sslh and make the rc.sslh script use it.
# - make slack-desc reflect optional deps used.

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

PRGNAM=sslh
VERSION=${VERSION:-2.2.4+20250630_11da63c}
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" -o "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

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

# fix slackware paths in manpage
sed -i 's,init.d/sslh,rc.d/rc.sslh,' sslh.pod

# 20250706 bkw: configure script ignores CFLAGS in the env.
sed -i "s/-O2/$SLKCFLAGS/" Makefile.in

# 20250706 bkw: -lnsl allows configure to detect libwrap properly.
LIBS="-lnsl" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --build=$ARCH-slackware-linux

# 20250707 bkw: manual install, since there's 3 binaries and only
# one gets installed with 'make install'.
PBIN=$PKG/usr/sbin
PMAN=$PKG/usr/man/man8
mkdir -p $PBIN $PMAN

make sslh-fork sslh-select sslh.8.gz
install -m0644 -oroot -groot sslh.8.gz $PMAN
make sslh-ev || true # allow this to fail (if optional libev is missing)

for i in ev fork select; do
  bin=sslh-$i
  if [ -e $bin ]; then
    install -s -m0755 -oroot -groot $bin $PBIN
    ln -s sslh.8.gz $PMAN/$bin.8.gz
  fi
done

ln -s sslh-fork $PBIN/sslh

WITHBSD=WITHOUT; WITHEV=WITHOUT
objdump -p $PKG/usr/sbin/sslh | grep -q 'NEEDED.*libbsd' && WITHBSD=WITH
[ -e $PKG/usr/sbin/sslh-ev ] && WITHEV=WITH

mkdir -p $PKG/etc/{rc.d,sslh,default}
cp -a *.cfg $PKG/etc/sslh
cat basic.cfg > $PKG/etc/sslh/sslh.cfg.new
cat $CWD/rc.sslh > $PKG/etc/rc.d/rc.sslh.new
cat $CWD/sslh.default > $PKG/etc/default/sslh.new

# 20250707 bkw: don't need these in a slackware package:
rm -f doc/README.MacOSX doc/README.Windows.md doc/INSTALL.md

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

mkdir -p $PKG/install
sed -e "s,@WITHBSD@,$WITHBSD," -e "s,@WITHEV@,$WITHEV," \
  < $CWD/slack-desc \
  > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

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