#!/bin/bash

# Slackware build script for ipcalc-ng

# Original author: Alexander Verbovetsky, Moscow, Russia
# Modified & now maintained by B. Watson <urchlay@slackware.uk>.

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

# 20250924 bkw: v1.0.3, BUILD=2
# - take over maintenance.
# - relicense as WTFPL.
# - add COPYING to doc dir.
# - script cleanup.
# - remove libmaxminddb from REQUIRES, since I couldn't make it work.
# - mention GeoIP as an optional dep.
# - add prebuilt man page, get rid of rubygem-ronn and its deps.

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

PRGNAM=ipcalc-ng
VERSION=${VERSION:-1.0.3}
BUILD=${BUILD:-2}
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"
elif [ "$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 ipcalc-$VERSION
tar xvf $CWD/ipcalc-$VERSION.tar.gz
cd ipcalc-$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 {} +

# 20250925 bkw: undocumented, for use by me.
if [ "${MAKEMAN:-no}" != "yes" ]; then
  # don't build the man page (no -D to disable it, have to sed).
  sed -i '/^ronn/,/^$/d' meson.build
  echo "==== Using prebuilt man page."
else
  if ! ronn --version >/dev/null; then
    echo "!!!! Can't build man page, install rubygem-ronn and its deps"
    exit 0
  fi
  echo "==== Building man page."
fi

# 20250925 bkw: it only builds with one of libmaxminddb or GeoIP,
# not both. I couldn't actually get libmaxminddb to work, so I
# removed it from REQUIRES and disabled it below. GeoIP is
# optional, autodetected, documented in README.
mkdir build
cd build
  CFLAGS="$SLKCFLAGS" \
  CXXFLAGS="$SLKCFLAGS" \
  meson .. \
    --buildtype=release \
    --infodir=/usr/info \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --localstatedir=/var \
    --mandir=/usr/man \
    --prefix=/usr \
    --sysconfdir=/etc \
    -Duse_maxminddb=disabled \
    -Dstrip=true
  "${NINJA:=ninja}"
  DESTDIR=$PKG $NINJA install
cd ..

if [ -e build/ipcalc.1 ]; then
  echo "==== Copying new man page to \$CWD"
  cp build/ipcalc.1 $CWD
fi

# 20250925 bkw: include a man page symlink named after the package
# name, to help out new users.
MAN1=$PKG/usr/man/man1
mkdir -p $MAN1
gzip -9c < $CWD/ipcalc.1 > $MAN1/ipcalc.1.gz
ln -s ipcalc.1.gz $MAN1/$PRGNAM.1.gz

# 20250925 bkw: did we build with GeoIP support? Can't use ldd because
# it gets dlopen'ed at runtime. This works:
WITHGEO=WITHOUT
strings $PKG/usr/bin/ipcalc | grep -q libGeoIP.so && WITHGEO=WITH

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

mkdir -p $PKG/install
sed "s,@WITHGEO@,$WITHGEO," \
  $CWD/slack-desc > $PKG/install/slack-desc

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