#!/bin/sh
# Build samba for Slackware.
CWD=`pwd`
VERSION=2.2.8

cd /tmp
tar xjvf $CWD/samba-$VERSION.tar.bz2
cd samba-$VERSION
chown -R root.root .
find . -perm 664 | xargs chmod 644

if [ -r /usr/lib/libcups.so ]; then
  echo
  echo "We've found libcups on your system."
  echo
  echo "Hit enter to build a version of samba with CUPS support"
  echo -n "(and with a CUPS dependency):  "
  read foo
  CUPS="--enable-cups"
fi

## NOTE (2003-03-07):  libcrypto no longer defines crypt(), so these
## patches are probably no longer needed (but also likely don't hurt anything)

## We must define LIBS with -lcrypt first, or we will end up using
## the crypt() from libcrypto, which doesn't support MD5.
#zcat $CWD/samba.ssl.diff.gz | patch -p1 --verbose --backup --suffix=.orig

## Same story.  Building with CUPS will put SSL ahead of -lcrypt if we
## don't patch it.  Thanks to Peter Christy for reporting this problem
## and helping to figure out a workaround.
#zcat $CWD/samba.cups.diff.gz | patch -p1 --verbose --backup --suffix=.orig

cd source
# Samba adds -O in addition to what's pre-supplied.
# Usually I go for -O2, but I trust their judgement.
CFLAGS="-march=i386 -mcpu=i686" ./configure \
  $CUPS \
  --with-fhs \
  --prefix=/usr \
  --localstatedir=/var \
  --bindir=/usr/bin \
  --sbindir=/usr/sbin \
  --with-lockdir=/var/cache/samba \
  --sysconfdir=/etc \
  --with-configdir=/etc/samba \
  --with-privatedir=/etc/samba/private \
  --with-codepagedir=/etc/codepages \
  --with-smbmount \
  --with-quotas \
  --with-syslog \
  --with-utmp \
  --with-sambabook=/usr/share/swat/using_samba \
  --with-ssl \
  --with-sslinc=/usr/include/openssl \
  --with-ssllib=/usr \
  --with-msdfs \
  --with-vfs \
  --with-libsmbclient \
  i386-slackware-linux

make

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

make install

# Make sure libsmbclient gets installed:
if [ -r bin/libsmbclient.so ]; then
  cp -a bin/libsmbclient.so /usr/lib/libsmbclient.so.0.0
  chown root.root /usr/lib/libsmbclient.so.0.0
  chmod 755 /usr/lib/libsmbclient.so.0.0
  ( cd /usr/lib
    rm -f libsmbclient.so.0 libsmbclient.so
    ln -sf libsmbclient.so.0.0 libsmbclient.so.0
    ln -sf libsmbclient.so.0.0 libsmbclient.so )
  cp -a include/libsmbclient.h /usr/include/libsmbclient.h
  chown root.root /usr/include/libsmbclient.h
  chmod 644 /usr/include/libsmbclient.h
fi

rmdir /usr/private /usr/share/samba/codepages

cat $CWD/smb.conf-sample > /etc/samba/smb.conf-sample

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

cd ..
cp -a COPYING Manifest README Read-Manifest-Now Roadmap WHATSNEW.txt docs examples \
  /usr/doc/samba-$VERSION
cp -a swat/README /usr/doc/samba-$VERSION/swat
( cd /usr/doc/samba-$VERSION/docs/htmldocs
  rm -rf using_samba
  ln -sf /usr/share/samba/swat/using_samba . )
mkdir -p /install
cat $CWD/slack-desc > /install/slack-desc
zcat $CWD/doinst.sh.gz > /install/doinst.sh

cat << EOF

*** Be sure the package contains:

drwx------   2 root     root         1024 Mar 12 13:21 /etc/samba/private
drwxr-xr-x   2 root     root         4096 May  3 15:46 /var/cache/samba/
drwxr-xr-x    2 root     root          48 Aug 29 13:06 /var/log/samba/
drwxrwxrwt   2 root     root         1024 Mar 12 13:21 /var/spool/samba/

EOF
