#!/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.bz2
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-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

# 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
