#!/bin/bash

# Slackware build script for httping

# Copyright 2010, Steven King
# Modified and now maintained by B. Watson <urchlay@slackware.uk>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# 20261017 bkw:
# - update for v4.4.0.
# - upstream moved to github.
# - upstream switched to cmake.
# - install .nl and .ru man pages.

# 20260806 bkw:
# - take over maintenance.
# - use v1.4.4. this was originally for v2.4, which has vanished from
#   upstream's website. no idea why.

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

PRGNAM=httping
SRCNAM=HTTPing
VERSION=${VERSION:-4.4.0}
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" -o "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=$ARCH -mtune=i686"
elif [ "$ARCH" = "x86_64" -o "$ARCH" = "aarch64" ]; then
  SLKCFLAGS="-O2 -fPIC"
else
  SLKCFLAGS="-O2"
fi
SLKCFLAGS+=" -DNDEBUG"

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $SRCNAM-$VERSION
tar xvf $CWD/$SRCNAM-$VERSION.tar.gz --no-same-owner
cd $SRCNAM-$VERSION
find . ! -type l    -a \( \
  \(     -perm /111 -a ! -perm 755 -a -exec chmod -f 755 {} + \) -o \
  \(   ! -perm /111 -a ! -perm 644 -a -exec chmod -f 644 {} + \) \)

DOC=/usr/doc/$PRGNAM-$VERSION
PKGDOC=$PKG$DOC

# 20261017 bkw: upstream switched to cmake. N.B. nonstandard use
# of DCMAKE_INSTALL_MANDIR: it normally is the path to the "man/"
# dir, not "man/man1" like it is here. Also, upstream unconditionally
# sets CMAKE_BUILD_TYPE to Debug. The -D_GNU_SOURCE is to pull in
# a prototype for strcasestr(), to kill some warnings... but it
# causes new warnings (less scary ones: "_GNU_SOURCE" redefined).
sed -i '/CMAKE_BUILD_TYPE/s,Debug,Release,' CMakeLists.txt

# 20261017 bkw:
# USE_SSL=ON because almost every web server requires it.
# USE_TUI=ON to enable the -K option.
# USE_GETTEXT=ON to enable 18n (for what it's worth).
mkdir -p build
cd build
  cmake \
    -DUSE_TUI=ON \
    -DUSE_SSL=ON \
    -DUSE_GETTEXT=ON \
    -DCMAKE_C_FLAGS_RELEASE="$SLKCFLAGS -D_GNU_SOURCE" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_INSTALL_LIBDIR=lib$LIBDIRSUFFIX \
    -DCMAKE_INSTALL_MANDIR=man/man1 \
    -DCMAKE_INSTALL_DOCDIR=doc/$PRGNAM-$VERSION \
    -DCMAKE_BUILD_TYPE=Release ..
  make VERBOSE=1
  make install/strip DESTDIR=$PKG
cd ..
gzip -9 $PKG/usr/man/man*/*

# 20261017 bkw:
# Dutch and Russian man pages aren't installed by 'make install'.
for i in nl ru; do
  mkdir -p $PKG/usr/man/$i/man1
  gzip -9c < $PRGNAM-$i.1 > $PKG/usr/man/$i/man1/$PRGNAM.1.gz
done

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
