#!/bin/bash

# nfs-utils.SlackBuild
# Heavily based on the original Slackware build scripts &
# Slack390's prototype script by Mark Post.
# Modified by Stuart Winter <mozes@slackware.com>
# 26-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/$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" ;; # -D_GNU_SOURCE -DHAVE_TCP_WRAPPER" ;;
   *)       export SLKCFLAGS="" ;;
esac

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

# Take the relevant bits from the nfs package framework:
tar pxvvf $CWD/_nfs-utils.tar.gz -C$PKG # etc install
# Add startup script:
cat $CWD/rc.nfsd > $PKG/etc/rc.d/rc.nfsd.new
# Off by default, unless there's a previous version that's turned on:
chmod 644 $PKG/etc/rc.d/rc.nfsd.new
# Create the rest of the package framework:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION/{nhfsstone,statd}
mkdir -p $PKG/var/log/nfsd

# Apply patches:
zcat $CWD/nfs-utils.lwrap.needs.lnsl.diff.gz | patch -p1 --verbose || exit 1

#for i in $PORTCWD/sources/*.patch ; do
#  auto_apply_patch $i || exit 1
#done

### for ARM:added --enable-nfsv41
###
# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --mandir=/usr/man \
   --with-statedir=/var/lib/nfs \
   --enable-nfsv3 \
   --enable-nfsv4=no \
   --enable-nfsv41=no \
   --enable-mountconfig \
   --enable-gss=no \
   --enable-tirpc=no \
   --program-prefix= \
   --program-suffix= \
   --build=$ARCH-slackware-linux-gnueabi || failconfig

# Build:
make $NUMJOBS || failmake

# Install into package:
make install DESTDIR=$PKG || failinstall

# Add nfsmount.conf and man page:
cat ./utils/mount/nfsmount.conf > $PKG/etc/nfsmount.conf.new
mkdir -p $PKG/usr/man/man5
cat ./utils/mount/nfsmount.conf.man > $PKG/usr/man/man5/nfsmount.conf.5
# Set default protocol version 3:
sed -i "s/# Defaultvers=4/Defaultvers=3/g" $PKG/etc/nfsmount.conf.new


# These might be in use:
( cd $PKG/var/lib/nfs
  for config_file in etab rmtab state xtab ; do
    mv -fv ${config_file} ${config_file}.new
  done
)

# No NFSv4 yet, so remove these:
rm -f $PKG/sbin/*nfs4

# Why were we doing this?
mkdir -pm755 $PKG/sbin
mv $PKG/usr/sbin/rpc.statd $PKG/sbin
( cd $PKG/usr/sbin ; ln -vsf ../../sbin/rpc.statd . )

# Copy docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a \
  AUTHORS COPYING* INSTALL NEWS README* \
  $PKG/usr/doc/nfs-utils-$VERSION
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION/statd
cp -a \
  utils/statd/COPYING utils/statd/COPYRIGHT utils/statd/TODO \
  $PKG/usr/doc/$PKGNAM-$VERSION/statd
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# 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
slackmp         # run makepkg -l y -c n

