#!/bin/bash
set +o posix
# samba.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
# 06-Aug-2004

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

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

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

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-march=armv5te -O2 -g"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.xz
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

if [ ! -d source3/lib/cmdline ]; then
  ( cd source3/lib
    mkdir cmdline
    cd cmdline
    ln -sf ../../../source3/include/popt_common.h . )
fi

# Apply patches:
tar xvvf $PORTCWD/sources/sam*debian*
while read i ; do
  auto_apply_patch debian/patches/$i || exit 1
done< <(cat debian/patches/series)

# Some of these options could be auto-detected, but declaring them
# here doesn't hurt and helps document what features we're trying to
# build in.
#LDFLAGS="-Wl,--no-as-needed" \
CFLAGS="$SLKCFLAGS -fstack-protector --param=ssp-buffer-size=4" \
CPPFLAGS="-D_FORTIFY_SOURCE=2" \
CXXFLAGS="$SLKCFLAGS -fstack-protector --param=ssp-buffer-size=4" \
FFLAGS="$SLKCFLAGS" \
LDFLAGS="-Wl,-z,relro -Wl,--no-as-needed" \
./configure \
  --enable-fhs \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --includedir=/usr/include \
  --bindir=/usr/bin \
  --sbindir=/usr/sbin \
  --mandir=/usr/man \
  --sysconfdir=/etc \
  --with-configdir=/etc/samba \
  --with-privatelibdir=/usr/lib${LIBDIRSUFFIX} \
  --with-modulesdir=/usr/lib${LIBDIRSUFFIX} \
  --with-piddir=/var/run \
  --with-privatedir=/etc/samba/private \
  --with-lockdir=/var/cache/samba \
  --with-logfilebase=/var/log/samba \
  --localstatedir=/var \
  --enable-cups \
  --with-automount \
  --with-quotas \
  --with-syslog \
  --with-utmp \
  --with-winbind \
  --with-ldap \
  --without-pam \
  --with-acl-support \
  --with-ads \
  --build=$ARCH-slackware-linux-gnueabi || failconfig

# Build:
# -j options don't seem to work...
JOBS=6
MAXJOBS=6
export JOBS MAXJOBS
make || failmake

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

# Install pkgconfig files:
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig
if [ ! -r $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig/talloc.pc ]; then
  cat $CWD/talloc.pc | sed -e "s/\/lib/\/lib${LIBDIRSUFFIX}/" > $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig/talloc.pc
fi
if [ ! -r $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig/pytalloc-util.pc ]; then
  cat $CWD/pytalloc-util.pc | sed -e "s/\/lib/\/lib${LIBDIRSUFFIX}/" > $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig/pytalloc-util.pc
fi

# Install talloc.h:
if [ ! -r $PKG/usr/include/talloc.h ]; then
  cp -a lib/talloc/talloc.h $PKG/usr/include
  chown root:root $PKG/usr/include/talloc.h
  chmod 644 $PKG/usr/include/talloc.h
fi

# Make symlink for pytalloc.h in /usr/include:
if [ ! -r $PKG/usr/include/pytalloc.h ]; then
  if [ -r $PKG/usr/include/samba-4.0/pytalloc.h ]; then
    ( cd $PKG/usr/include ; ln -sf samba-4.0/pytalloc.h . )
  fi
fi

# Make symlink for libtalloc.so:
if [ ! -r $PKG/usr/lib${LIBDIRSUFFIX}/libtalloc.so -a -r $PKG/usr/lib${LIBDIRSUFFIX}/libtalloc.so.? ]; then
  ( cd $PKG/usr/lib${LIBDIRSUFFIX} ; ln -sf libtalloc.so.? libtalloc.so )
fi

# Make symlink for libpytalloc-util.so:
if [ ! -r $PKG/usr/lib${LIBDIRSUFFIX}/libpytalloc-util.so -a -r $PKG/usr/lib${LIBDIRSUFFIX}/libpytalloc-util.so.? ]; then
  ( cd $PKG/usr/lib${LIBDIRSUFFIX} ; ln -sf libpytalloc-util.so.? libpytalloc-util.so )
fi

# Install the smbprint script:
install -m0744 packaging/printing/smbprint $PKG/usr/bin/smbprint

# Add a sample config file:
cat $CWD/smb.conf.default > $PKG/etc/samba/smb.conf-sample

# Setup a default lmhosts file:
echo "127.0.0.1        localhost" > $PKG/etc/samba/lmhosts.new

if [ ! -r $PKG/usr/bin/smbget ]; then
  rm -f $PKG/usr/share/man/man1/smbget.1
fi

# We'll add rc.samba to the init directory, but chmod 644 so that it doesn't
# start by default:
mkdir -p $PKG/etc/rc.d
cat $CWD/rc.samba > $PKG/etc/rc.d/rc.samba.new
chmod 644 $PKG/etc/rc.d/rc.samba.new

# PAM related stuff we don't use:
rm -r $PKG/usr/share/locale
rm -f $PKG/usr/man/man8/pam*

mv $PKG/usr/share/man $PKG/usr
cp -a \
  COPYING* MAINTAINERS Manifest PFIF.txt README* \
  Read-Manifest-Now Roadmap WHATSNEW.txt docs examples \
  $PKG/usr/doc/samba-$VERSION
# These are installed elsewhere:
rm -rf $PKG/usr/doc/samba-$VERSION/docs/htmldocs \
       $PKG/usr/doc/samba-$VERSION/docs/manpages
# Empty now?
rmdir $PKG/usr/doc/samba-$VERSION/docs 2> /dev/null

# I'm sorry, but when all this info is included in HTML, adding 7MB worth of
# PDF files just to have extra artwork is more fluff than I'll agree to.
rm -f $PKG/usr/doc/samba-$VERSION/docs/*.pdf
# Also redundant also:
rm -rf $PKG/usr/doc/samba-$VERSION/docs/docbook

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
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

# Finally ensure permissions, which may have got broken by the 'slackslack'
# above:
chmod 700  etc/samba/private
chmod 755  var/cache/samba/
chmod 755  var/log/samba/
chmod 1777 var/spool/samba/

slackmp         # run makepkg -l y -c n

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