#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# ntp.SlackBuild
# by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 20-Jun-2004
#
# Copyright 2008, 2009, 2010, 2011, 2012, 2014, 2015, 2017  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
# Add -fPIC to CFLAGS:
case $ARCH in
   arm|aarch64) export SLKCFLAGS="$SLKCFLAGS -fPIC"
                export LIBDIRSUFFIX="" ;;
   *)           export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/ntp-${VERSION}*.tar.!(*sign|*asc|*sig)
cd ntp* || exit 1
slackhousekeeping

# Apply patches:
zcat $CWD/ntp.nano.diff.gz | patch -p1 --verbose || exit 1

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --mandir=/usr/man \
   --enable-ipv6 \
   --with-crypto \
   --enable-ntp-signd \
   --localstatedir=/var \
   --sysconfdir=/etc \
   --bindir=/usr/sbin \
   --sbindir=/usr/sbin \
   --docdir=/usr/doc/ntp-$VERSION \
   --htmldir=/usr/doc/ntp-$VERSION \
   --program-prefix= \
   --program-suffix= \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
make $NUMJOBS || make || failmake

# Install into package framework:
make -i install DESTDIR=$PKG || failinstall
mkdir -vpm755 $PKG/etc/rc.d
install -vpm644 $CWD/rc.ntpd $PKG/etc/rc.d/rc.ntpd.new

# KDE Plasma 5 seems to want ntpdate in /usr/bin unless you have systemd:
mkdir -p $PKG/usr/bin
( cd $PKG/usr/bin
  ln -vsf ../sbin/ntpdate . )

# This should be empty.  Try to remove it, and error out if it's not actually empty:
rmdir $PKG/usr/libexec || exit 1

# Install default conf and keys files:
mkdir -p $PKG/etc
install -vpm644 $CWD/ntp.conf $PKG/etc/ntp.conf.new
install -vpm644 $CWD/ntp.keys $PKG/etc/ntp.keys.new

# Install configs:
# Location for the drift and stats files:
mkdir -p $PKG/var/lib/ntp
# Perms & ownerships are set below:

# This is deprecated and no longer used by anything:
#touch $PKG/etc/ntp/step-tickers

mkdir -p $PKG/etc/logrotate.d
install -vpm644 $CWD/ntp.logrotate $PKG/etc/logrotate.d/ntp.new

# Install docs:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION/{util,ntpdate,html}
mv -fv $PKG/usr/doc/ntp-$VERSION/*.html $PKG/usr/doc/ntp-$VERSION/html || exit 1
cp -fav COPYRIGHT NEWS README* TODO WHERE-TO-START \
      *.y2kfixes clockstuff conf scripts \
      $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav util/README $PKG/usr/doc/$PKGNAM-$VERSION/util
cp -fav ntpdate/README $PKG/usr/doc/$PKGNAM-$VERSION/ntpdate
( cd $PKG/usr/doc/ntp-$VERSION
  find . -name ".deps*" -exec rm -rf "{}" \; 2> /dev/null )
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file


# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh

# Special ownerships & permissions:
chown ntp:ntp $PKG/var/lib/ntp
chmod 0775 $PKG/var/lib/ntp
chown root:ntp $PKG/etc/ntp.keys.new
chmod 640 $PKG/etc/ntp.keys.new

slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
