# Build recipe for sysvinit.
#
# Copyright (c) 2018-2021 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=sysvinit
version=2.98
release=1

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

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

# Remote source(s)
fetch=https://download.savannah.nongnu.org/releases/sysvinit/$tarname

description="
The System V init.

The sysvinit package contains programs for controlling the startup,
running, and shutdown of the system.

System V init was originally written by Miquel van Smoorenburg.
"

homepage=https://savannah.nongnu.org/projects/sysvinit
license=GPLv2+

# Source documentation
docs="COPYING COPYRIGHT README doc/Changelog doc/Propaganda"
docsdir="${docdir}/${program}"

# Limit package name to the program name
full_pkgname=$program

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

    cd "$srcdir"

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

    # Adjust some (rc) script paths for Dragora 3+
    patch -p0 < "${worktree}/patches/sysvinit/sysvinit-adjust_paths.h.diff"

    # Remove several programs installed by other packages
    patch -p1 < "${worktree}/patches/sysvinit/sysvinit-2.98-dragora_consolidated-1.patch"

    make -j${jobs} CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" STATIC="-static"
    make -j${jobs} ROOT="$destdir" install

    strip --strip-unneeded "${destdir}/sbin"/* 2> /dev/null || true

    # Remove empty directories
    rmdir "${destdir}/bin" "${destdir}/usr/bin" || true

    # 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}"/
}

