# Build recipe for perp.
#
# Copyright (c) 2016-2019 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=perp
version=2.07
release=7

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

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

# Remote source(s)
fetch="
 http://rsync.dragora.org/current/sources/$tarname
 http://b0llix.net/perp/distfiles/$tarname
"

description="
A persistent process supervisor and service managment framework.

The perp package provides a set of daemons and utilities to reliably
start, monitor, log, and control a collection of persistent processes.

A \"persistent process\" is any program intended to be long-running,
highly available, and purpose critical.  Also known and often described
as a service, a persistent process normally provides some essential,
on-demand system service.  Programs that serve email, domain name
queries, and http requests are all examples of services that are
normally run as persistent processes.

These are the programs that you want to start at system boot, and to
continue running for as long as the system itself.  These are the
programs you need running in uninterrupted service, day and night,
forever and ever.

perp helps make sure that they do.
"

homepage=http://b0llix.net/perp
license=Custom

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

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

    cd "$srcdir"

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

    # Apply patches in order to refresh perp-2.07
    # See: http://github.com/michaelforney/perp/commits/master
    patch -Np1 -i "${worktree}/patches/perp/perp-patches"

    # Apply patches from Dragora
    patch -Np1 -i "${worktree}/patches/perp/runchoom-use-oom_score_adj"
    patch -Np0 -i "${worktree}/patches/perp/flags-conf.mk.diff"

    make -j${jobs} CC="cc -static" QICFLAGS="$QICFLAGS"
    make strip
    make -j${jobs} DESTDIR="$destdir" install

    # Automatic installation setup
    DESTDIR="$destdir" ./perp/perp-setup /etc/perp

    # Create "the blessed" /var/run/perp directory
    mkdir -p "${destdir}/var/run/perp"
    chmod 700 "${destdir}/var/run/perp"

    # Include our config file for tinylog(8)
    cp -p "${worktree}/archive/perp/etc/tinylog.conf" \
          "${destdir}/etc/"
    chmod 644 "${destdir}/etc/tinylog.conf"

    # Include our custom essential perp init scripts
    cp -p "${worktree}/archive/perp/etc/rc.log-boot" \
          "${destdir}/etc/perp/.boot/rc.log"

    cp -p "${worktree}/archive/perp/etc/rc.perp-boot" \
          "${destdir}/etc/perp/.boot/rc.perp"

    chmod 755 "${destdir}/etc/perp/.boot/rc.log" \
              "${destdir}/etc/perp/.boot/rc.perp"

    # Include profile file
    mkdir -p "${destdir}/etc/profile.d"
    cp -p "${worktree}/archive/perp/etc/profile.d/perp.sh" \
           "${destdir}/etc/profile.d/"
    chmod 644 "${destdir}/etc/profile.d/perp.sh"

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

    # We don't include perp-setup*
    rm -f "${destdir}/usr/sbin/perp-setup" \
          "${destdir}/${mandir}/man8/perp-setup.8"

    # 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

    # Copy documentation
    mkdir -p "${destdir}${docsdir}"
    cp -p $docs "${destdir}${docsdir}"/
}

