# Build recipe for scron.
#
# Copyright (c) 2017 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=scron
version=0.4
release=1

# Define a category for the output of the package name
pkgcategory=daemons

tarname=${program}-${version}.tar.gz

# Remote source(s)
fetch=http://dl.2f30.org/releases/$tarname

description="
Clock daemon.

Scron schedules commands to be run at specified dates and times.
"

homepage=http://git.2f30.org/scron
license=MIT/X

# Source documentation
docs="LICENSE README TODO"
docsdir="${docdir}/${program}-${version}"

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    make -j${jobs} \
     CFLAGS="$QICFLAGS -std=c99 -Wall -Wextra -pedantic -D_POSIX_C_SOURCE=200809L -D_BSD_SOURCE" \
     LDFLAGS="$QILDFLAGS -static" \
     PREFIX=/usr \
     MANPREFIX=$mandir \
     DESTDIR="$destdir" install

    # Compress man page and copy the documentation

    lzip -9 "${destdir}/${mandir}/man1/scron.1"
    ln -s scron.1.lz "${destdir}/${mandir}/man1/cron.1.lz"	# Symlink-compatibility.

    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}"

    # Create directories adding the crontab file

    mkdir -p "${destdir}/etc/cron.hourly"  \
             "${destdir}/etc/cron.daily"   \
             "${destdir}/etc/cron.weekly"  \
             "${destdir}/etc/cron.monthly"

    # Incorporate our "run-parts" version for running cron jobs
    cp -p "${worktree}/archive/scron/run-parts" "${destdir}/usr/bin/"
    chmod 755 "${destdir}/usr/bin/run-parts"

    # Add the crontab file
    mkdir -p "${destdir}/etc"
    cp -p "${worktree}/archive/scron/crontab" "${destdir}/etc/"
    chmod 644 "${destdir}/etc/crontab"

    # Install scron perp service

    mkdir -p "${destdir}/etc/perp/crond"

    cp -p "${worktree}/archive/scron/rc.log" \
          "${worktree}/archive/scron/rc.main" \
          "${destdir}/etc/perp/crond/"

    chmod 755 "${destdir}"/etc/perp/crond/rc.*

    # Be an active service by default
    chmod +t "${destdir}/etc/perp/crond"

    # To handle config file(s)
    touch "${destdir}/etc/.graft-config" \
          "${destdir}/etc/perp/crond/.graft-config"
}

