#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
PKG=$TMP/package-portmap
rm -rf $PKG
mkdir -p $PKG

VERSION=${VERSION:-6.0}
ARCH=${ARCH:-x86_64}
BUILD=${BUILD:-1}
DISTRO=${DISTRO:-slamd64}

if [ $DISTRO = slackware ]; then
	PKGARCH=$ARCH
else
	PKGARCH=${ARCH}_${DISTRO}
fi

if [ $DISTRO = slamd64 ]; then
	LIBSUFFIX=64
	SLKLDFLAGS="-L/usr/lib64"
else
	LIBSUFFIX=
	SLKLDFLAGS=
fi

cd $TMP
rm -rf portmap_6.0
tar xjvf $CWD/portmap-6.0.tar.bz2 || exit 1
cd portmap_6.0
chown -R root:root .
zcat $CWD/portmap.lwrap.needs.lnsl.diff.gz | patch -p1 --backup --suffix=.orig || exit 1
mkdir -p $PKG/usr/doc/portmap-6.0
cp -a \
  BLURB* CHANGES README* \
  $PKG/usr/doc/portmap-6.0
chmod 644 $PKG/usr/doc/portmap-6.0/*
make || exit 1
strip portmap pmap_dump pmap_set
mkdir -p $PKG/sbin
cat portmap > $PKG/sbin/rpc.portmap
chmod 755 $PKG/sbin/rpc.portmap
mkdir -p $PKG/usr/sbin
cat pmap_dump > $PKG/usr/sbin/pmap_dump
cat pmap_set > $PKG/usr/sbin/pmap_set
chmod 755 $PKG/usr/sbin/*
mkdir -p $PKG/usr/man/man8
for file in *.8 ; do
  cat ${file} | gzip -9c > $PKG/usr/man/man8/${file}.gz
done
( cd $PKG/usr/man/man8 ; ln -sf portmap.8.gz rpc.portmap.8.gz )
# Now let's add a recent version of /etc/rpc for good measure:
if [ -r /etc/rpc ]; then
  mkdir -p $PKG/etc
  cp -a /etc/rpc $PKG/etc/rpc
else
  echo "WARNING: no /etc/rpc on this system, can't include!"
  echo -n "hit enter..."
  read errorfodder
  echo
fi
mkdir -p $PKG/etc/rc.d
zcat $CWD/rc.rpc.gz > $PKG/etc/rc.d/rc.rpc.new
# Off by default.
chmod 644 $PKG/etc/rc.d/rc.rpc.new
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh

# Build the package:
cd $PKG
makepkg -l y -c n $TMP/portmap-$VERSION-$PKGARCH-$BUILD.tgz

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/portmap_6.0
  rm -rf $PKG
fi