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

# dcron.SlackBuild
# by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 31-May-2004
#
# Copyright 2009, 2010, 2012, 2016, 2024 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

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

# Apply patches:
#
# Apply patches from git:
zcat $CWD/patches/0002-README-var-spool-cron-crontabs-root.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/patches/0003-CHANGELOG-tweak.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/patches/0006-Fixed-a-bug-whereby-syncs-killed-all-waiting-jobs.patch.gz | patch -p1 --verbose || exit 1
zcat $CWD/patches/0007-Update-main.c.patch.gz | patch -p1 --verbose || exit 1

# Define TMPDIR as /run/cron instead of /tmp:
# This eliminates possibility of stale $TMPDIR/cron.??????/ dirs as
# they won't survive a reboot when they're in a subdir of /run
zcat $CWD/patches/defs.h.TMPDIR.diff.gz | patch -p1 --verbose || exit 1

# Fix problem where user creates /var/spool/cron/crontabs/<user>.new
# using 'crontab -', exits with control-c, and then crontab refuses to
# overwrite the junk file. It would be better if dcron would wipe
# the junk file in the event it were not closed correctly, but oh well.
zcat $CWD/patches/crontab.c.O_EXCL.diff.gz | patch -p1 --verbose || exit 1

CFLAGS+="$SLKCFLAGS" make \
  PREFIX=/usr \
  BINDIR=/usr/bin \
  SBINDIR=/usr/sbin \
  MANDIR=/usr/man \
  CRONTAB_GROUP=root \
  || failmake

mkdir -vpm755 $PKG/usr/{bin,sbin}
install -vpm755 crond $PKG/usr/sbin/crond
install -vpm4711 crontab $PKG/usr/bin/crontab

# Add run-parts script and man page, largely taken from Fedora:
install -vpm755 $CWD/run-parts $PKG/usr/bin/run-parts

mkdir -p $PKG/usr/man/man{1,8}
cat crontab.1 | gzip -9c > $PKG/usr/man/man1/crontab.1.gz
cat crond.8 | gzip -9c > $PKG/usr/man/man8/crond.8.gz
cat $CWD/run-parts.8.gz > $PKG/usr/man/man8/run-parts.8.gz

# Add the init script:
mkdir -vpm755 $PKG/etc/rc.d
install -vpm755 $CWD/rc.crond $PKG/etc/rc.d/rc.crond.new

# Add the default files:
mkdir -p $PKG/etc/default
install -vpm644 $CWD/crond.default $PKG/etc/default/crond.new
install -vpm644 $CWD/run-parts.default $PKG/etc/default/run-parts.new

# Create some other stuff we need
mkdir -vpm755 $PKG/etc/cron.{hourly,daily,weekly,monthly}
mkdir -vpm755 $PKG/var/spool/cron/{crontabs,cronstamps}
zcat $CWD/crontab.root > $PKG/var/spool/cron/crontabs/root.new

# dcron will whine about "unable to scan" this directory, so we'll create it
mkdir -vpm755 $PKG/etc/cron.d

# Docs:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
  CHANGELOG README \
  extra \
  $PKG/usr/doc/$PKGNAM-$VERSION

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
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

# Set special permissions:
chmod 4711 $PKG/usr/bin/crontab
chmod 0751 $PKG/var/spool/cron
chmod 0750 $PKG/var/spool/cron/crontabs $PKG/var/spool/cron/cronstamps
chmod 0600 $PKG/var/spool/cron/crontabs/root.new

slackmp         # run makepkg -l y -c n

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