# Build recipe for at.
#
# Copyright (c) 2018, 2020 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=at
version=3.2.1
release=1

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

tarname=${program}_${version}.orig.tar.gz

# Remote source(s)
fetch=http://software.calhariz.com/at/$tarname

description="
The at package provide delayed job execution and batch processing.

It includes:

at     - queues, examines or deletes jobs for later execution.
atd    - is the daemon that runs jobs queued for later execution.
atq    - lists the user's pending jobs, or all jobs, if superuser.
atrm   - deletes jobs, identified by their job number.
atrun  - runs jobs queued for later execution.
batch  - script that executes commands when system load levels permit. 
"

homepage=http://blog.calhariz.com/tag/at
license=GPLv2+

# The source documentation will be auto-installed
docsdir="${docdir}/${program}-${version}"

# This source does not support parallel jobs
jobs=1

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

    cd "$srcdir"

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

    # Update for hosts based on musl
    cp -f "${worktree}/archive/common/config.guess" config.guess
    cp -f "${worktree}/archive/common/config.sub" config.sub

    # Fix Makefile.in so that the documentation directory is
    # installed in the specified docdir (Thanks to BLFS!)
    sed -i '/docdir/s/=.*/= @docdir@/' Makefile.in

    # Apply a patch (from "Void Linux") to solve build issues
    patch -p0 < "${worktree}/patches/at/flex_isleap.patch"

    ./configure CFLAGS="$QICFLAGS" LDFLAGS="$QILDFLAGS -static" \
    SENDMAIL=/usr/sbin/sendmail \
     $configure_args \
     --mandir=$mandir \
     --docdir=$docsdir \
     --with-jobdir=/var/spool/atd \
     --with-atspool=/var/spool/atd \
     --with-daemon_username=daemon \
     --with-daemon_groupname=daemon \
     --build="$(gcc -dumpmachine)"

    make -j${jobs} V=1
    make -j${jobs} IROOT="$destdir" install

    # Clean up installed file
    rm -f "${destdir}/var/spool/atd/.SEQ"

    # Set permissions for the spool directory
    chmod 770 "${destdir}/var/spool/atd"

    # Install atd perp service(s)

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

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

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

    # THIS SERVICE IS DISABLED BY DEFAULT
    chmod -t "${destdir}/etc/perp/atd"

    # Manage (dot) new files via graft(1)
    touch "${destdir}/etc/.graft-config" \
          "${destdir}/etc/perp/atd/.graft-config"

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi
}

