#!/bin/sh
TMP=${TMP:-/tmp}
CWD=`pwd`
PKG=$TMP/package-ntp

VERSION=4.2.4p5
ARCH=${ARCH:-i486}
BUILD=${BUILD:-2}

if [ "$ARCH" = "i386" ]; then
  SLKCFLAGS="-O2 -march=i386 -mcpu=i686"
elif [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "s390" ]; then
  SLKCFLAGS="-O2"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
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 {} \;
find . -perm 666 -exec chmod 644 {} \;
find . -perm 600 -exec chmod 644 {} \;
find . -perm 444 -exec chmod 644 {} \;
find . -perm 400 -exec chmod 644 {} \;
find . -perm 440 -exec chmod 644 {} \;
find . -perm 511 -exec chmod 755 {} \;
find . -perm 711 -exec chmod 755 {} \;
find . -perm 555 -exec chmod 755 {} \;
CFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --localstatedir=/var \
  --sysconfdir=/etc \
  --bindir=/usr/sbin \
  --sbindir=/usr/sbin \
  --program-prefix= \
  --program-suffix= \
  --build=$ARCH-slackware-linux

make -j6 || make || exit 1
make install DESTDIR=$PKG || exit 1

# Since the Makefile's install pays no heed to the --bindir settings,
# we'll move things to where they've always been ourselves:
mv $PKG/usr/bin/* $PKG/usr/sbin
rmdir $PKG/usr/bin

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/etc/rc.d
cat $CWD/rc.ntpd > $PKG/etc/rc.d/rc.ntpd.new

mkdir -p $PKG/usr/doc/ntp-$VERSION
cp -a \
  COPYRIGHT 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
( cd $PKG/usr/doc/ntp-$VERSION
  find . -type d -name .deps -exec rm -rf {} \; 2> /dev/null
)
( 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
)
gzip -9 $PKG/usr/man/man?/*.?
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