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

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

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

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
rm -rf $PKG
mkdir -p $PKG

cd $TMP
tar xzvf $CWD/netkit-timed-0.17.tar.gz || exit 1
cd netkit-timed-0.17
chown -R root:root .
zcat $CWD/netkit-timed-0.17.diff.gz | patch -p1 --backup --verbose --suffix=.orig -E || exit
# Fixes undefined reference to CLK_TCK
CFLAGS="-DCLK_TCK=HZ" \
./configure --prefix=/usr
make || exit 1
cd timed/timed
strip timed
mkdir -p $PKG/usr/sbin
cat timed > $PKG/usr/sbin/in.timed
chmod 755 $PKG/usr/sbin/in.timed
mkdir -p $PKG/usr/man/man8
cat timed.8 | gzip -9c > $PKG/usr/man/man8/timed.8.gz
cd ../timedc
strip timedc
cat timedc > $PKG/usr/sbin/timedc
chmod 755 $PKG/usr/sbin/timedc
cat timedc.8 | gzip -9c > $PKG/usr/man/man8/timedc.8.gz
cd ../..
mkdir -p $PKG/usr/doc/netkit-timed-0.17
cp -a README $PKG/usr/doc/netkit-timed-0.17
chown root:root $PKG/usr/doc/netkit-timed-0.17/*
chmod 644 $PKG/usr/doc/netkit-timed-0.17/*

# Finish up the package:
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

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