#!/bin/bash
#
# Slackware build script for fail2ban
#
# Copyright (c) 2008-2015, Nishant Limbachia, Hoffman Estates, IL, USA
# <nishant _AT_ mnspace _DOT_ net>
# Copyright 2015-2025 Corrado Franco (https://corradofranco.it)
# All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version, with the following exception:
# the text of the GPL license may be omitted.

# This program is distributed in the hope that it will be useful, but
# without any warranty; without even the implied warranty of
# merchantability or fitness for a particular purpose. Compiling,
# interpreting, executing or merely reading the text of the program
# may result in lapses of consciousness and/or very being, up to and
# including the end of all existence and the Universe as we know it.
# See the GNU General Public License for more details.

# You may have received a copy of the GNU General Public License along
# with this program (most likely, a file named COPYING).  If not, see
# <http://www.gnu.org/licenses/>.
#
# Latest version of this SlackBuild at https://github.com/conraid/SlackBuilds

set -eu

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

PKGNAM=fail2ban
SRCVERSION=1.1.0
VERSION=$(echo $SRCVERSION | head -c7)
BUILD=${BUILD:-4}
TAG=${TAG:-cf}
TMP=${TMP:-/tmp/pkg}
PKG=$TMP/package-$PKGNAM
OUTPUT=${OUTPUT:-/tmp}

DOCFILES="CONTRIBUTING* COPYING ChangeLog DEVELOP FILTERS MANIFEST README.md RELEASE THANKS TODO doc/run-rootless.txt files"

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

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME:-""}" ]; then
  echo "$PKGNAM-$VERSION-$ARCH-${BUILD}${TAG}.txz"
  exit 0
fi

if [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=pentium4 -mtune=generic"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -march=x86-64 -mtune=generic -fPIC -pipe -fomit-frame-pointer"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PKGNAM-$SRCVERSION
tar xvf $CWD/$PKGNAM-$SRCVERSION.tar.gz || tar xvf $CWD/${SRCVERSION}.tar.gz
cd $PKGNAM-$SRCVERSION

chown -R root:root .
chmod -R u+w,go+r-w,a-s .

CFLAGS="$SLKCFLAGS" python3 setup.py install --root=$PKG

# install and zip man pages
mkdir -p $PKG/usr/man/{man1,man5}
install -m 0644 man/fail2ban-client.1 man/fail2ban-regex.1 \
  man/fail2ban-server.1 man/fail2ban.1 $PKG/usr/man/man1
install -m 0644 man/jail.conf.5 $PKG/usr/man/man5
find $PKG/usr/man -type f -exec gzip -9 {} \;

# move config files to .new
for FILE in $(find $PKG/etc/fail2ban -type f); do
  mv "$FILE" "${FILE}.new"
done

# Patch to Slackware version
sed 's/paths-debian.conf/paths-slackware.conf/' -i $PKG/etc/fail2ban/jail.conf.new

# install startup script
install -D -m 0644 $CWD/files/rc.fail2ban $PKG/etc/rc.d/rc.fail2ban.new
install -D -m 0644 $CWD/files/paths-slackware.conf $PKG/etc/fail2ban/paths-slackware.conf.new

# remove non-slackware specific stuff
rm -f $PKG/etc/fail2ban/paths-{debian,fedora,freebsd,osx}.conf.new

# install logrotate script
install -Dm644 $CWD/files/fail2ban.logrotate $PKG/etc/logrotate.d/fail2ban.new

# make directory for socket, pid file & db file
mkdir -p $PKG/{var/lib,run}/fail2ban

# Install bash completion
install -Dm644 files/bash-completion $PKG/usr/share/bash-completion/completions/fail2ban

# remove share/doc
rm -rf $PKG/usr/share

mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a $DOCFILES $PKG/usr/doc/$PKGNAM-$VERSION || true
cat $CWD/${PKGNAM}.SlackBuild > $PKG/usr/doc/$PKGNAM-$VERSION/${PKGNAM}.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
if [ -e $CWD/slack-required ]; then
  cat $CWD/slack-required | tee $PKG/install/slack-required $PKG/usr/doc/$PKGNAM-$VERSION/slack-required > /dev/null 2>&1
fi
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PKGNAM-$VERSION-$ARCH-$BUILD${TAG}.txz
