#!/bin/bash

# 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="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.?z*
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

cd source3


# 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" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --bindir=/usr/bin \
  --sbindir=/usr/sbin \
  --mandir=/usr/man \
  --sysconfdir=/etc \
  --with-configdir=/etc/samba \
  --with-piddir=/var/run \
  --with-privatedir=/etc/samba/private \
  --localstatedir=/var \
  --with-lockdir=/var/cache/samba \
  --with-swatdir=/usr/share/swat \
  --with-logfilebase=/var/log/samba \
  --enable-cups \
  --enable-static=no \
  --enable-shared=yes \
  --with-acl-support=yes \
  --with-automount \
  --with-cifsmount \
  --with-cifsumount \
  --with-quotas \
  --with-syslog \
  --with-utmp \
  --with-libsmbclient \
  --with-winbind \
  --with-ldap \
  --build=$ARCH-slackware-linux-gnueabi || failconfig

# Build:
make || failmake

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

# Install libnss_win* libraries:
mkdir -p $PKG/lib${LIBDIRSUFFIX}
cp -a ../nsswitch/libnss_winbind.so $PKG/lib${LIBDIRSUFFIX}/libnss_winbind.so.2
cp -a ../nsswitch/libnss_wins.so $PKG/lib${LIBDIRSUFFIX}/libnss_wins.so.2
( cd $PKG/lib${LIBDIRSUFFIX}
  ln -sf libnss_winbind.so.2 libnss_winbind.so
  ln -sf libnss_wins.so.2 libnss_wins.so
)

## Make sure that other programs can link against libsmbclient:
#( cd $PKG/usr/lib${LIBDIRSUFFIX}
#  for FILE in samba/libsmbclient.so* ; do
#    ln -sf $FILE
#  done
#)
# Add some (u)mount links:
mkdir -p $PKG/sbin
( cd $PKG/sbin
  # SMBFS is obsolete in the kernel now -- use mount.cifs
  #rm -f mount.smbfs ; ln -sf /usr/bin/smbmount mount.smbfs
  # Add a wrapper script from Debian that's included in the upstream Samba
  # sources to help out people (like me ;-) who don't read every speck of
  # documentation:
  cat $TMPBUILD/samba-$VERSION/examples/scripts/mount/mount.smbfs > mount.smbfs
  chmod 755 $PKG/sbin/mount.smbfs
  # I suppose this wouldn't be the worst idea in the world, either:
  ( cd $PKG/usr/bin ; ln -sf /sbin/mount.smbfs smbmount )
  rm -f mount.cifs ; ln -sf /usr/sbin/mount.cifs mount.cifs
  # smbumount was giving me problems so it seems wise to play it safe
  #rm -f umount.smbfs ; ln -sf /usr/bin/smbumount umount.smbfs
  #rm -f umount.cifs ; ln -sf /usr/sbin/umount.cifs umount.cifs
)

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

if [ ! -r $PKG/usr/bin/smbget ]; then
  rm -f $PKG/usr/share/man/man1/smbget.1
  rm -f $PKG/usr/share/swat/help/smbget.1.html
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
cd ..
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
mkdir -p $PKG/usr/doc/samba-$VERSION/docs
( cd $PKG/usr/doc/samba-$VERSION/docs
  ln -sf /usr/share/swat/help htmldocs
)
# 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
