#!/bin/sh

# based on Slackware's build script
# creates only libsmbclient and ignores everything else

VERSION=${VERSION:-$(echo samba-*.tar.xz | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
BUILD=${BUILD:-1}

# Automatically determine the architecture we're building on:
if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) export ARCH=i486 ;;
    arm*) export ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
       *) export ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-samba
rm -rf $PKG
mkdir -p $TMP $PKG

cd $TMP
rm -rf samba-$VERSION
tar xvf $CWD/samba-$VERSION.tar.xz || exit 1
cd samba-$VERSION || exit 1

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

chown -R root:root .
find . \
  \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
  -exec chmod 755 {} \; -o \
  \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
  -exec chmod 644 {} \;

cd source3
if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-Os -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-Os"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-Os -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-Os"
  LIBDIRSUFFIX=""
fi

# LDFLAGS are needed to avoid problems with missing symbols.
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-external-libtalloc=no \
  --enable-static=no \
	--disable-cups \
	--enable-shared-libs \
	--disable-pie \
	--disable-relro \
	--disable-dnssd \
	--disable-avahi \
	--disable-fam \
	--disable-swat \
	--without-cluster-support \
	--without-dnsupdate \
	--without-sys-quotas \
	--without-ads \
	--without-ldap \
	--with-included-iniparser \
	--without-winbind \
	--with-libiconv="" \
  --build=$ARCH-slackware-linux

# -j options don't seem to work...
make || exit 1

mkdir -p \
         $PKG/usr/doc/samba-$VERSION \
         $PKG/var/spool/samba \
         $PKG/var/log/samba \
         $PKG/etc/samba/private \
         $PKG/var/cache/samba
chmod 700 $PKG/etc/samba/private
chmod 1777 $PKG/var/spool/samba

make installlibs DESTDIR=$PKG || exit 1

rm -Rf $PKG/usr/share
rm -Rf $PKG/usr/sbin
rm -Rf $PKG/usr/bin
rm -Rf $PKG/usr/man
rm -Rf $PKG/usr/doc
rm -Rf $PKG/usr/lib${LIBDIRSUFFIX}/libnetapi*
rm -Rf $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig/netapi.pc
rm -Rf $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig/wbclient.pc

# Install pkgconfig files:
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/pkgconfig
cp -a pkgconfig/*.pc $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

find $PKG | xargs file | grep -e "executable" -e "shared object" \
  | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null

cd $PKG
/sbin/makepkg -l y -c n $TMP/samba-stripped-$VERSION-$ARCH-$BUILD.txz

