#!/bin/bash
#
# Slackware build script for lynis (http://www.rootkit.nl/projects/lynis.html)
#
# Copyright 2008 Michelino Chionchio
# Copyright 2008-2025 Corrado Franco
#
# All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version, with the following exception:
# the text of the GPL license may be omitted.

# This program is distributed in the hope that it will be useful, but
# without any warranty; without even the implied warranty of
# merchantability or fitness for a particular purpose. Compiling,
# interpreting, executing or merely reading the text of the program
# may result in lapses of consciousness and/or very being, up to and
# including the end of all existence and the Universe as we know it.
# See the GNU General Public License for more details.

# You may have received a copy of the GNU General Public License along
# with this program (most likely, a file named COPYING).  If not, see
# <http://www.gnu.org/licenses/>.
#
# Initial SlackBuild by Michelino Chionchio
# Rewritten by Corrado Franco

set -eu

cd $(dirname $0) ; CWD=$(pwd)

PKGNAM=lynis
VERSION=3.1.4
BUILD=${BUILD:-1}
TAG=${TAG:-cf}
TMP=${TMP:-/tmp/pkg}
PKG=$TMP/package-$PKGNAM
OUTPUT=${OUTPUT:-/tmp}

ARCH=${ARCH:="noarch"}

DOCFILES="CHANGELOG CONTRIBUT* FAQ INSTALL LICENSE README*"

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME:-""}" ]; then
  echo "$PKGNAM-$VERSION-$ARCH-${BUILD}${TAG}.txz"
  exit 0
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PKGNAM-$VERSION
tar xvf $CWD/$PKGNAM-$VERSION.tar.gz
cd $PKGNAM-$VERSION

chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# Install binary file
install -Dm755 lynis $PKG/usr/bin/lynis

# Install plugins, include and db dirs
mkdir -p $PKG/usr/share/$PKGNAM
cp -r db include plugins $PKG/usr/share/$PKGNAM

# Install config file
install -Dm644 default.prf $PKG/etc/lynis/default.prf.new
install -Dm644 developer.prf $PKG/etc/lynis/developer.prf.new

# Install and compress manpage
install -Dm644 lynis.8 $PKG/usr/man/man8/lynis.8
gzip -9 $PKG/usr/man/man8/lynis.8

# Install bash completion file
install -Dm644 extras/bash_completion.d/lynis $PKG/usr/share/bash-completion/completions/lynis

mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a $DOCFILES $PKG/usr/doc/$PKGNAM-$VERSION || true
cat $CWD/${PKGNAM}.SlackBuild > $PKG/usr/doc/$PKGNAM-$VERSION/${PKGNAM}.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
if [ -e $CWD/slack-required ]; then
  cat $CWD/slack-required | tee $PKG/install/slack-required $PKG/usr/doc/$PKGNAM-$VERSION/slack-required > /dev/null 2>&1
fi
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PKGNAM-$VERSION-$ARCH-$BUILD${TAG}.txz
