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

# dovecot.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright 2006, 2010  Alan Hicks, Lizella, GA
# Copyright 2013, 2014, 2015, 2016, 2017  Mario Preksavec, Zagreb, Croatia
# Copyright 2017  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

if [ "$(grep "^dovecot:x:94:94" /etc/passwd)" = "" -o \
     "$(grep "^dovecot:x:94:" /etc/group)" = "" -o \
     "$(grep "^dovenull:x:95:95" /etc/passwd)" = "" -o \
     "$(grep "^dovenull:x:95:" /etc/group)" = "" ]; then
cat << EOF
The dovecot and dovenull user/group do not exist on this system.
Before running this script, upgrade your 'etc' package.
EOF
exit 1
fi

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd ${PKGNAM}*/ || exit 1
slackhousekeeping

if [ ! -L /lib${LIBDIRSUFFIX}/libpam.so.? ]; then # no PAM
   # Ship config set up for authentication from /etc/passwd and /etc/shadow.
   zcat $CWD/dovecot.default.shadow.auth.diff.gz | patch -p1 --verbose || exit 1
   PAM_OPTIONS="--without-pam"
 else # Yes, we have PAM:
   mkdir -p $PKG/etc/pam.d
   cp -a $CWD/dovecot.pam $PKG/etc/pam.d/dovecot.new
   chown root:root $PKG/etc/pam.d/dovecot.new
   chmod 644 $PKG/etc/pam.d/dovecot.new
   PAM_OPTIONS="--with-pam"
fi

# Since this package installs working config files in /etc/dovecot, there isn't
# any need to send people to /usr/doc. Also, make sure that TLS is recommended.
zcat $CWD/dovecot.config.README.diff.gz | patch -p1 --verbose || exit 1

# Need -lnsl for -lwrap:
sed -i "s/LIBWRAP_LIBS=-lwrap/LIBWRAP_LIBS=\"-lwrap -lnsl\"/g" configure

# Add -ldl to LDFLAGS for sqlite
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
LDFLAGS="-ldl" \
./configure \
   --prefix=/usr \
   --sysconfdir=/etc \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --localstatedir=/var \
   --docdir=/usr/doc/$PKGNAM-$VERSION \
   --mandir=/usr/man \
   --with-notify=inotify \
   $PAM_OPTIONS \
   --with-ldap=plugin \
   --with-lucene \
   --with-mysql \
   --with-sqlite \
   --with-libcap \
   --with-libwrap \
   --disable-static \
   --host=${SLK_ARCH_HOST} \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
make $NUMJOBS || make || failmake

# Install into package framework:
make install DESTDIR=$PKG || failinstall

# Install sample configuration files:
mkdir -vpm755 $PKG/etc/dovecot
cp -fav $PKG/usr/doc/$PKGNAM-$VERSION/example-config/* $PKG/etc/dovecot
( cd $PKG/etc/dovecot
  for file in *.conf *.ext conf.d/*.conf conf.d/*.ext ; do
    mv -fv $file ${file}.new
  done )

mkdir -p $PKG/etc/rc.d
install -vpm644 $CWD/rc.dovecot $PKG/etc/rc.d/rc.dovecot.new

# Add greylist.pl per postfix documentation:
install -vpm755 examples/smtpd-policy/greylist.pl $PKG/usr/libexec/postfix/

# Add documentation:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
  AUTHORS COPYING* ChangeLog NEWS README TODO \
  $PKG/usr/doc/$PKGNAM-$VERSION
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file
changelogliposuction NEWS $PKGNAM $VERSION

# 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      # set standard Slackware file/dir permissions and ownerships
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

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