#!/bin/sh
CWD=`pwd`
PKG=/tmp/package-ntp

VERSION=4.2.0
ARCH=i486
BUILD=1

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mcpu=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -march=x86_64"
fi

rm -rf $PKG
mkdir -p $PKG
cd /tmp
rm -rf ntp-$VERSION
tar xjvf $CWD/ntp-$VERSION.tar.bz2
cd ntp-$VERSION
chown -R root.root .
find . -perm 664 -exec chmod 644 {} \;
find . -perm 777 -exec chmod 755 {} \;
find . -perm 2777 -exec chmod 755 {} \;
find . -perm 775 -exec chmod 755 {} \;
find . -perm 2755 -exec chmod 755 {} \;
find . -perm 774 -exec chmod 644 {} \;
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --localstatedir=/var \
  --sysconfdir=/etc \
  --bindir=/usr/sbin \
  --program-prefix= \
  --program-suffix= \
  $ARCH-slackware-linux
make
make install DESTDIR=$PKG
mkdir -p $PKG/etc/ntp
cat $CWD/ntp.conf > $PKG/etc/ntp.conf.new
cat $CWD/ntp.keys > $PKG/etc/ntp/ntp.keys.new
chmod 600 $PKG/etc/ntp/ntp.keys.new
touch $PKG/etc/ntp/step-tickers
mkdir -p $PKG/usr/doc/ntp-$VERSION
cp -a \
  COPYRIGHT ChangeLog INSTALL NEWS README* TODO WHERE-TO-START \
  *.y2kfixes clockstuff conf html scripts \
  $PKG/usr/doc/ntp-$VERSION
mkdir $PKG/usr/doc/ntp-$VERSION/util
cp -a util/README $PKG/usr/doc/ntp-$VERSION/util
mkdir $PKG/usr/doc/ntp-$VERSION/ntpdate
cp -a ntpdate/README $PKG/usr/doc/ntp-$VERSION/ntpdate
mkdir $PKG/usr/doc/ntp-$VERSION/ntptrace
cp -a ntptrace/README $PKG/usr/doc/ntp-$VERSION/ntptrace
chown -R root.bin $PKG/usr/sbin
( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh

cd $PKG
makepkg -l y -c n ../ntp-$VERSION-$ARCH-$BUILD.tgz