#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# proftpd.SlackBuild
# Heavily based on the original Slackware build script,
# Modified by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# 09-Jul-2004
#
# Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2016, 2017, 2018, 2020  Patrick J. Volkerding, Sebeka, MN, USA
# 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.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
tar xf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-*/ || failextract
slackhousekeeping

# To save on maintenance, and since we carry the same versions as x86 Slackware,
# let's pull the version numbers from the x86 trunk:
slack_setvarfromupstream PROFTPD_MODS

# The contrib mod_ldap module makes a lot of noise if LDAP isn't in use.
# We could compile it as a shared object and require that the user configure
# the conf file to load it, but let's just mute the problematic output.
# Odds are good that someone wanting to use LDAP isn't going to have the issues
# that are warned about.
cat $CWD/proftpd.mute.ldap.warnings.diff | patch -p1 --verbose || exit 1

# Configure:
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --mandir=/usr/man \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --sysconfdir=/etc \
   --enable-ctrls \
   --enable-autoshadow \
   --enable-ipv6 \
   --enable-dso \
   --enable-nls \
   --localstatedir=/var/run \
   --with-lastlog \
   --with-modules="${PROFTPD_MODS}" \
   --build=${SLK_ARCH_BUILD} || failconfig
#   --no-ipv6 \

# Create package framework:
mkdir -vpm755 $PKG/{etc,usr/doc/$PKGNAM-$VERSION,home/ftp}
# Directory used by mod_ban:
mkdir -vpm755 $PKG/var/db/proftpd

# Build:
# Parallel builds do not work on ARM.
make $NUMJOBS || make || failmake

# Install:
make install DESTDIR=$PKG || failinstall
rmdir $PKG/usr/libexec 2> /dev/null
rm -f $PKG/etc/*
install -vpm644 $CWD/etc/proftpd.conf $PKG/etc/proftpd.conf.new
install -vpm644 $CWD/etc/ftpusers $PKG/etc/ftpusers.new

# With no library to link against, these are useless:
rm -rfv $PKG/usr/include

# Install docs:
cp -a COPYING* CREDITS INSTALL NEWS README* \
      contrib/README.mod_wrap contrib/README.ratio doc sample-configurations \
      $PKG/usr/doc/$PKGNAM-$VERSION
cp -a contrib/README $PKG/usr/doc/proftpd-$VERSION/README.contrib
changelogliposuction ChangeLog $PKGNAM $VERSION

# Bloat removal:
( cd $PKG/usr/doc
  find . -name "*.sgml" -exec rm {} \;
  find . -name "*.pdf" -exec rm {} \;
)
rmdir $PKG/usr/share/locale
rmdir $PKG/usr/share

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Special ownerships:
chown proftpd:proftpd $PKG/var/db/proftpd

slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
