# Build recipe for socklog.
#
# 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.

program=socklog
version=2.1.0
release=1

# Set 'outdir' for a nice and well-organized output directory
outdir="${outdir}/${arch}/daemons"

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

# Remote source(s)
fetch=http://smarden.org/socklog/$tarname

description="
System and kernel logging services.

Socklog is a small and secure replacement for syslogd.  Socklog
supports system logging through Unix domain sockets (/dev/log),
UDP sockets (0.0.0.0:514), as well as TCP socket.

Socklog provides a different network logging concept, and also
does log event notification.

Socklog is small, secure, and reliable.
"

homepage=http://smarden.org/socklog
license=BSD

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

# Source directory used by this software
srcdir=admin/${program}-${version}

build()
{
    set -e

    unpack "${tardir}/$tarname"

    cd "$srcdir"

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

    cd src
    patch -p1 < "${worktree}/patches/socklog/socklog-headers"
    patch -p1 < "${worktree}/patches/socklog/socklog-klogpath"
    cd -

    # Pick up our custom CFLAGS, LDFLAGS
    sed -i "s|gcc -O2|cc $QICFLAGS|" src/conf-cc
    sed -i "s|gcc -s|cc $QILDFLAGS -static|" src/conf-ld

    ./package/compile

    # Prepare the package, manually

    mkdir -p "${destdir}/usr/bin" "${destdir}/usr/sbin" \
             "${destdir}/${mandir}/man1" "${destdir}/${mandir}/man8"

    cp -p command/tryto command/uncat \
          "${destdir}/usr/bin"

    cp -p command/socklog-check command/socklog-conf command/socklog \
          "${destdir}/usr/sbin"

    cp -p man/*.1 "${destdir}/${mandir}/man1/"
    cp -p man/*.8 "${destdir}/${mandir}/man8/"

    # This is specific and related to the runit project
    rm -f "${destdir}/usr/sbin/socklog-conf" \
          "${destdir}/${mandir}/man8/socklog-conf.8"

    # Install socklog perp service(s)

    mkdir -p "${destdir}/etc/perp/socklog" "${destdir}/etc/perp/klog"

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

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

    chmod 755 "${destdir}"/etc/perp/socklog/rc.* \
              "${destdir}"/etc/perp/klog/rc.*

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

    # Include pre-configuration file for /var/log
    mkdir -p "${destdir}/var/log"
    (
        cd "${destdir}/var/log"
        umask 000
        tar xf "${worktree}/archive/socklog/socklog-config.tar.gz"
    )

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

    (
        cd "${destdir}/var/log"
        find . -type f | while read -r file
        do
            touch "$(dirname -- "$file")/.graft-config"
        done
    )

    # Compress and copy documentation

    lzip -9 "${destdir}/${mandir}"/man?/*

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

    for file in $docs
    do
        cp -p package/${file} "${destdir}${docsdir}"
    done
}

