#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-nfs-server

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

# Explode the package framework:
cd $PKG
explodepkg $CWD/_nfs-server.tar.gz

# Find the size of a file:
filesize() {
 SIZE=`ls -l -d -G $1 | cut -b23-32`
 echo -n $SIZE
}

echo "+======================+"
echo "| nfs-server-2.2beta47 |"
echo "+======================+"
cd $TMP
tar xzvf $CWD/nfs-server-2.2beta47.tar.gz
cd nfs-server-2.2beta47
mkdir -p $PKG/usr/doc/nfs-server-2.2beta47
cp -a README NEWS $PKG/usr/doc/nfs-server-2.2beta47
chmod 644 $PKG/usr/doc/nfs-server-2.2beta47/*
chown root.root $PKG/usr/doc/nfs-server-2.2beta47/*
./BUILD
make
strip rpc.mountd rpc.nfsd showmount
cat rpc.mountd > $PKG/usr/sbin/rpc.mountd
cat rpc.nfsd > $PKG/usr/sbin/rpc.nfsd
cat showmount > $PKG/usr/sbin/showmount
cat exports.man | gzip -9c > $PKG/usr/man/man5/exports.5.gz
for page in mountd.man nfsd.man showmount.man ; do
  cat $page | gzip -9c > $PKG/usr/man/man8/`basename $page .man`.8.gz
done

# Build the package:
cd $PKG
tar czvf $TMP/nfs-server.tgz .

# Warn of zero-length files:
for file in `find . -type f -print` ; do
 if [ "`filesize $file`" = "0" ]; then
  echo "WARNING: zero length file $file"
 fi
 if [ "`filesize $file`" = "20" ]; then
  echo "WARNING: possible empty gzipped file $file"
 fi
done

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP/nfs-server-2.2beta47
 rm -rf $PKG
fi
