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

# autofs.SlackBuild
# Heavily based on the original Slackware build script,
# Modified by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# 08-Jul-2004
#
# Copyright 2010, 2013  Patrick J. Volkerding, Sebeka, Minnesota, 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 xvvf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# Configure:
#CFLAGS="$SLKCFLAGS -fgnu89-inline" \
#CPPFLAGS="$SLKCFLAGS -fgnu89-inline" \
CFLAGS="$SLKCFLAGS" \
CPPFLAGS="$SLKCFLAGS" \
LDFLAGS="-Wl,-z,now" \
./configure \
   --prefix=/usr \
   --mandir=/usr/man \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --with-libtirpc \
   --with-openldap \
   --without-sasl \
   --build=${SLK_ARCH_BUILD} || failconfig

# Nasty hack since adding -fgnu89-inline to CFLAGS is not enough.
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=777787
#sed -i 's?-rdynamic?-rdynamic -fgnu89-inline?g' daemon/Makefile
#sed -i '/LIBS = -ldl/aDAEMON_LDFLAGS += -fgnu89-inline' daemon/Makefile

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

# Install:
make install DESTDIR=$PKG || exit 1

# Add /run directory:
mkdir -p $PKG/run

# Referenced in the auto.master config file, but not created:
mkdir -p $PKG/etc/auto.master.d

# Install rc.autofs.new:
mkdir -p $PKG/etc/rc.d
install -vpm644 samples/rc.autofs $PKG/etc/rc.d/rc.autofs.new
# Fix confdir:
sed -i "s|/etc/sysconfig|/etc/default|g" $PKG/etc/rc.d/rc.autofs.new

# Install sample config files:
for file in auto.master auto.misc auto.net auto.smb autofs_ldap_auth.conf ; do
  install -vpm644 samples/$file $PKG/etc/${file}
done
install -vpm644 samples/autofs.conf.default $PKG/etc/autofs.conf
mkdir -p $PKG/etc/default
install -vpm644 samples/autofs.init.conf $PKG/etc/default/autofs.new

# Support existing user-edited config files:
# Note any file attribute/file mask special permissions (see prior to 'slackmp'):
for file in auto.master auto.misc autofs.conf autofs_ldap_auth.conf ; do
  mv -fv $PKG/etc/$file $PKG/etc/${file}.new
done

# Process docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a \
  COPYING* COPYRIGHT CREDITS INSTALL README* \
  samples \
  $PKG/usr/doc/$PKGNAM-$VERSION
changelogliposuction CHANGELOG $PKGNAM $VERSION

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
#slackstriprpaths     # strip rpaths
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 permissions:
chmod 600 $PKG/etc/autofs_ldap_auth.conf.new
chmod 755 $PKG/etc/auto.smb $PKG/etc/auto.net

slackmp         # run makepkg -l y -c n

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