#!/bin/bash

# nfs-utils.SlackBuild
# Heavily based on the original Slackware build scripts &
# Slack390's prototype script by Mark Post.
# Modified by Stuart Winter <stuart@armedslack.org>
# 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="-march=armv4 -mtune=xscale -D_GNU_SOURCE -DHAVE_TCP_WRAPPER" ;;
   powerpc) export SLKCFLAGS="" ;;
   sparc)   export SLKCFLAGS="" ;;
   *)       export SLKCFLAGS="" ;;
esac

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

# Set CFLAGS.  If I don't add -D_GNU_SOURCE above then statd fails to build.
# However, after the failed build if I cd into the source dir and make from there
# (outside of this build script) then it adds -D_GNU_SOURCE -DHAVE_TCP_WRAPPER 
# which enables statd to build. I haven't investigated why these flags are not 
# used when building from this script, but adding them in here causes the build
# to complete. 
# This does need some attention.
# fgrep -lr -- '-O2' . | xargs sed -i 's/-O2/-O2 '"$SLKCFLAGS"'/g'

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

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --mandir=/usr/man \
   --with-statedir=/var/lib/nfs \
   --enable-nfsv3 \
   --enable-nfsv4=no \
   --enable-gss=no \
   --enable-secure-statd \
   --program-prefix= \
   --program-suffix= \
   --build=$ARCH-slackware-linux || failconfig

# Build:
make $NUMJOBS || failmake

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

# 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

# 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

cat << EOF > $PKG/install/doinst.sh
#!/bin/sh
config() {
  NEW="\$1"
  OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)"
  # If there's no config file by that name, mv it over:
  if [ ! -r \$OLD ]; then
    mv \$NEW \$OLD
  elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then # toss the redundant copy
    rm \$NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}
EOF
( cd $PKG/var/lib/nfs
  for config_file in etab.new rmtab.new state.new xtab.new ; do
    if [ -r ${config_file} ]; then
      echo "config var/lib/nfs/${config_file}" >> $PKG/install/doinst.sh
    fi
  done
)
# If these aren't moved into place, they're junk:
echo "rm -f var/lib/nfs/*.new" >> $PKG/install/doinst.sh
cat << EOF >> $PKG/install/doinst.sh
if [ -x etc/rc.d/rc.nfsd ]; then
  chmod 755 etc/rc.d/rc.nfsd.new
else
  chmod 644 etc/rc.d/rc.nfsd.new
fi
config etc/rc.d/rc.nfsd.new
config etc/exports.new
EOF

slackmp         # run makepkg -l y -c n
