#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-proftpd

VERSION=1.3.1
DIRVER=1.3.1
ARCH=${ARCH:-i486}
BUILD=1

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2"
fi

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG

cd $TMP
rm -rf proftpd-$VERSION
tar xjvf $CWD/proftpd-$VERSION.tar.bz2 || exit 1
cd proftpd-$DIRVER || exit 1

chown -R root:root .
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --sysconfdir=/etc \
  --enable-autoshadow \
  --enable-ctrls \
  --enable-ipv6 \
  --localstatedir=/var/run \
  --with-modules=mod_readme:mod_ratio:mod_tls:mod_wrap:mod_ctrls_admin \
  --build=$ARCH-slackware-linux
# This caused funny messages about "Quotas off" with every FTP command,
# and mod_wrap gets a compile error:
# --with-modules= ... mod_quota ...
make || exit 1
make install DESTDIR=$PKG || exit 1
# With no library to link against, these are useless:
rm -rf $PKG/usr/include
rm -f $PKG/etc/*
rmdir $PKG/usr/libexec 2> /dev/null
cat $CWD/etc/proftpd.conf > $PKG/etc/proftpd.conf.new
cat $CWD/etc/ftpusers > $PKG/etc/ftpusers.new
mkdir -p $PKG/home/ftp
gzip -9 $PKG/usr/man/man?/*.?
mkdir -p $PKG/usr/doc/proftpd-$VERSION
cp -a COPYING CREDITS INSTALL NEWS README* \
  contrib/README.mod_wrap contrib/README.ratio \
  $PKG/usr/doc/proftpd-$VERSION
head -c 93 ChangeLog > $PKG/usr/doc/proftpd-$VERSION/ChangeLog
touch -r ChangeLog $PKG/usr/doc/proftpd-$VERSION/ChangeLog
cp -a contrib/README $PKG/usr/doc/proftpd-$VERSION/README.contrib
cp -a doc sample-configurations $PKG/usr/doc/proftpd-$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
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)
mkdir -p $PKG/install
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/proftpd-$VERSION-$ARCH-$BUILD.tgz