#!/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/$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="-march=armv3 -mtune=xscale -D_GNU_SOURCE -DHAVE_TCP_WRAPPER" ;;
   powerpc) export SLKCFLAGS="" ;;
   sparc)   export SLKCFLAGS="" ;;
   *)       export SLKCFLAGS="" ;;
esac

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

# Take the relevant bits from the nfs package framework:
tar pzxvvf $CWD/_nfs-utils.tar.gz -C$PKG # etc install
# Add startup script:
cat $CWD/rc.nfsd > $PKG/etc/rc.d/rc.nfsd.new
# Create the rest of the package framework:
mkdir -p $PKG/usr/doc/$PACKAGE-$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'

# Configure:
./configure \
   --prefix=/usr \
   --with-statedir=/var/lib/nfs \
   --enable-nfsv3 \
   --enable-secure-statd \
   --enable-nfsv4=no \
   --enable-gss=no \
   --program-prefix= \
   --program-suffix= || failconfig

# Build:
make || failmake

# Install into package:
#make install_prefix=$PKG \
#     MANDIR=$PKG/usr/man \
#     SBINDIR=$PKG/usr/sbin \
#     install || failinstall
#make install DESTDIR=$PKG || failinstall
#
# Let's go back to Pat's stuff since 'make install'
# installs into other places:
strip utils/exportfs/exportfs
cat utils/exportfs/exportfs > $PKG/usr/sbin/exportfs
cat utils/exportfs/exportfs.man | gzip -9c > $PKG/usr/man/man8/exportfs.8.gz
cat utils/exportfs/exports.man | gzip -9c > $PKG/usr/man/man5/exports.5.gz
strip utils/lockd/lockd
cat utils/lockd/lockd > $PKG/sbin/rpc.lockd
cat utils/lockd/lockd.man | gzip -9c > $PKG/usr/man/man8/lockd.8.gz
strip utils/mountd/mountd
cat utils/mountd/mountd > $PKG/usr/sbin/rpc.mountd
cat utils/mountd/mountd.man | gzip -9c > $PKG/usr/man/man8/mountd.8.gz
strip utils/nfsd/nfsd
cat utils/nfsd/nfsd > $PKG/usr/sbin/rpc.nfsd
cat utils/nfsd/nfsd.man | gzip -9c > $PKG/usr/man/man8/nfsd.8.gz
strip utils/nfsstat/nfsstat
cat utils/nfsstat/nfsstat > $PKG/usr/sbin/nfsstat
cat utils/nfsstat/nfsstat.man | gzip -9c > $PKG/usr/man/man8/nfsstat.8.gz
strip utils/nhfsstone/nhfsstone
cat utils/nhfsstone/nhfsgraph > $PKG/usr/sbin/nhfsgraph
cat utils/nhfsstone/nhfsnums > $PKG/usr/sbin/nhfsnums
cat utils/nhfsstone/nhfsrun > $PKG/usr/sbin/nhfsrun
cat utils/nhfsstone/nhfsstone > $PKG/usr/sbin/nhfsstone
cat utils/nhfsstone/nhfsgraph.man | gzip -9c > $PKG/usr/man/man8/nhfsgraph.8.gz
cat utils/nhfsstone/nhfsnums.man | gzip -9c > $PKG/usr/man/man8/nhfsnums.8.gz
cat utils/nhfsstone/nhfsrun.man | gzip -9c > $PKG/usr/man/man8/nhfsrun.8.gz
cat utils/nhfsstone/nhfsstone.man | gzip -9c > $PKG/usr/man/man8/nhfsstone.8.gz

strip utils/showmount/showmount
cat utils/showmount/showmount > $PKG/usr/sbin/showmount
cat utils/showmount/showmount.man | gzip -9c > $PKG/usr/man/man8/showmount.8.gz
strip utils/statd/statd
cat utils/statd/statd > $PKG/sbin/rpc.statd
cat utils/statd/statd.man | gzip -9c > $PKG/usr/man/man8/statd.8.gz

# Install docs:
cp -favv utils/nhfsstone/DISCLAIMER utils/nhfsstone/README utils/nhfsstone/README.linux \
         $PKG/usr/doc/$PACKAGE-$VERSION/nhfsstone
cp -favv utils/statd/COPYING utils/statd/COPYRIGHT utils/statd/TODO \
        $PKG/usr/doc/$PACKAGE-$VERSION/statd
cp -favv COPYING ChangeLog README \
         $PKG/usr/doc/$PACKAGE-$VERSION

# Create manpage symlinks:
# These are already symlinks so I don't need to re-make them.
# the slack buildkit functions will fix up the gzipping and re-linking.
#( cd $PKG/usr/man/man8
#  rm -rf rpc.lockd.8   && ln -sf lockd.8   rpc.lockd.8
#  rm -rf rpc.mountd.8  && ln -sf mountd.8  rpc.mountd.8
#  rm -rf rpc.nfsd.8    && ln -sf nfsd.8    rpc.nfsd.8
#  rm -rf rpc.rquotad.8 && ln -sf rquotad.8 rpc.rquotad.8 
#  rm -rf rpc.statd.8   && ln -sf statd.8   rpc.statd.8  )

# Junk:
( cd $PKG && rm -rf usr/man/man7 var/lib/nfs/* usr/sbin/rpc.rquotad )

# 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

# 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
