#!/bin/bash

# netatalk.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 30-Sep-2004

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

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

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

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar jxvvf $CWD/$PACKAGE-$VERSION.tar.bz2
cd $PACKAGE-$VERSION
slackhousekeeping

# Apply patches:
zcat $CWD/netatalk.afpdconf.diff.gz | patch -p1 --verbose || failpatch
zcat $CWD/netatalk.etc2ps.diff.gz   | patch -p1 --verbose || failpatch

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --sysconfdir=/etc \
   --libexecdir=/usr/sbin \
   --localstatedir=/var \
   --disable-static \
   --disable-pam \
   --with-shadow || failconfig

# Build:
make || failmake

# Explode the package framework (the main thing we want from here
# is the rc script and /etc/netatalk/afppasswd)
( cd $PKG && explodepkg $CWD/_netatalk.tar.gz )

# Install:
make install DESTDIR=$PKG

# Add package description & install script
# (usually I do this right at the end but we need to add bits to
# doinst.sh)
( cd $PKG && slackdesc )

# This is normally installed setuid root, but I'm not 100% sure I trust it yet.
# I see it uses strcpy() in a few places...
chmod 755 $PKG/usr/bin/afppasswd

# Prevent config overwrites:
( cd $PKG/etc/netatalk
  for file in AppleVolumes.default AppleVolumes.system afpd.conf afppasswd atalkd.conf netatalk.conf papd.conf ; do
    mv $file ${file}.new
    echo "config etc/netatalk/${file}.new" >> $PKG/install/doinst.sh
  done )
echo "rm -f etc/netatalk/afppasswd.new" >> $PKG/install/doinst.sh

# Install docs:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
ecp -a CONTRIBUTORS COPYING COPYRIGHT NEWS README TODO VERSION \
      $PKG/usr/doc/$PACKAGE-$VERSION
rm -rf $PKG/usr/doc/$PACKAGE-$VERSION/doc/{Makefile*,htmldocs,*.pdf}

# "make install" creates this directory with chmod 0777
# I'd rather use 755, but I'll assume since this is a
# new directory that it is needed for something.  This
# will help it a bit anyway...
chmod 1777 $PKG/var/spool/netatalk

# 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 (root:bin), slack644docs
slackdesc       # install slack-desc and doinst.sh

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

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