#!/bin/bash
#
# Slackware build script for clamav
#
# Copyright (c) 2006,2007 Eric Hameleers <alien@slackware.com>
# Copyright (c) 2011-2018 Matteo Bernardini <ponce@slackbuilds.org>
# Copyright 2015-2025 Corrado Franco (https://corradofranco.it)
# 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/>.
#
# Latest version of this SlackBuild at https://github.com/conraid/SlackBuilds

# Based on https://slackbuilds.org/slackbuilds/14.2/system/clamav/clamav.SlackBuild

set -euo pipefail

# You must have clamav user and group to run this script
#
#  groupadd -g 210 clamav
#  useradd -u 210 -d /dev/null -s /bin/false -c "Clam AntiVirus" -g clamav clamav
#

if ! grep ^clamav: /etc/passwd 2>&1 > /dev/null; then
  echo "You must have a clamav user to run this script"
  exit 1
elif ! grep ^clamav: /etc/group 2>&1 > /dev/null; then
  echo "You must have a clamav group to run this script"
  exit 1
fi

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

PKGNAM=clamav
SRCVERSION=1.5.1
VERSION="${SRCVERSION//-/_}"
BUILD=${BUILD:-1}
TAG=${TAG:-cf}
TMP=${TMP:-/tmp/pkg}
PKG=$TMP/package-$PKGNAM
OUTPUT=${OUTPUT:-/tmp}
COUNTRY=${COUNTRY:-it}

DOCFILES="AUTHORS BUGS COPYING* ChangeLog FAQ INSTALL NEWS README UPGRADE docs/*.pdf docs/html examples"

if [ -z ${ARCH:=""} ]; then
  case "$( uname -m )" in
  i?86) ARCH=i686 ;;
  arm*) ARCH=arm ;;
  *) ARCH=$( uname -m ) ;;
  esac
fi

# 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

if [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=pentium4 -mtune=generic"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -march=x86-64 -mtune=generic -fPIC -pipe -fomit-frame-pointer"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

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

chown -R root:root .
find -L . \
  \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 -o -perm 511 \) \
  -exec chmod 755 {} \; -o \
  \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
  -exec chmod 644 {} \;

# Apply patches
if [ -e $CWD/patches ]; then
  for PATCH in $CWD/patches/*.patch; do
    patch -p1 -i $PATCH
  done
fi

# Specify the desired mirror in the update config file
# http://www.iana.org/cctld/cctld-whois.htm
sed -i "s/^\#DatabaseMirror.*/DatabaseMirror db.${COUNTRY}.clamav.net/" \
  etc/freshclam.conf.sample

sed -i \
  -e "s/^Example/#Example/" \
  -e "s/^\#LogSyslog/LogSyslog/" \
  -e "s/^\#LogFacility/LogFacility/" \
  -e "s|^\#PidFile.*|PidFile /run/clamav/freshclam.pid|" \
  -e "s|^\#UpdateLogFile.*|UpdateLogFile /var/log/clamav/freshclam.log|" \
  -e "s/^\#AllowSupplementaryGroups.*/AllowSupplementaryGroups yes/" \
  -e "s/^\#DatabaseOwner.*/DatabaseOwner clamav/" \
  -e "s|^\#NotifyClamd.*|NotifyClamd /etc/clamd.conf|" \
  etc/freshclam.conf.sample

sed -i \
  -e "s/^Example/#Example/" \
  -e "s/^\#LogSyslog/LogSyslog/" \
  -e "s/^\#LogFacility/LogFacility/" \
  -e "s|^\#LogFile\ .*|LogFile /var/log/clamav/clamd.log|" \
  -e "s|^\#PidFile.*|PidFile /run/clamav/clamd.pid|" \
  -e "s|^\#LocalSocket\ .*|LocalSocket /run/clamav/clamd.socket|" \
  -e "s/^\#LocalSocketGroup.*/LocalSocketGroup clamav/" \
  -e "s/^\#LocalSocketMode/LocalSocketMode/" \
  -e "s/^\#FixStaleSocket/FixStaleSocket/" \
  -e "s/^\#User.*/User clamav/" \
  -e "s/^\#AllowSupplementaryGroups.*/AllowSupplementaryGroups yes/" \
  -e "s/^\#ExitOnOOM/ExitOnOOM/" \
  etc/clamd.conf.sample

mkdir -p build
cd build
cmake -Wno-dev -GNinja \
  -DCMAKE_C_FLAGS_RELEASE:STRING="$SLKCFLAGS -DNDEBUG" \
  -DCMAKE_CXX_FLAGS_RELEASE:STRING="$SLKCFLAGS -DNDEBUG" \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_INSTALL_PREFIX=/usr \
  -DCMAKE_INSTALL_SYSCONFDIR=/etc \
  -DAPP_CONFIG_DIRECTORY=/etc \
  -DCMAKE_INSTALL_DOCDIR=/usr/doc/$PKGNAM-$VERSION \
  -DCMAKE_INSTALL_MANDIR=/usr/man \
  -DENABLE_ALL_THE_WARNINGS=OFF \
  -DENABLE_APP=ON \
  -DENABLE_CLAMONACC=ON \
  -DENABLE_DEBUG=OFF \
  -DENABLE_DOXYGEN=OFF \
  -DENABLE_EXAMPLES=OFF \
  -DENABLE_EXPERIMENTAL=ON \
  -DENABLE_EXTERNAL_MSPACK=ON \
  -DENABLE_FRESHCLAM_DNS_FIX=OFF \
  -DENABLE_FRESHCLAM_NO_CACHE=OFF \
  -DENABLE_FUZZ=OFF \
  -DENABLE_LIBCLAMAV_ONLY=OFF \
  -DENABLE_MAN_PAGES=ON \
  -DENABLE_MILTER=ON \
  -DENABLE_SHARED_LIB=ON \
  -DENABLE_STATIC_LIB=OFF \
  -DENABLE_STRN_INTERNAL=OFF \
  -DENABLE_SYSTEMD=OFF \
  -DENABLE_TESTS=OFF \
  -DENABLE_UNRAR=ON \
  -DENABLE_WERROR=OFF \
  ..

ninja
DESTDIR=$PKG ninja install
cd ..

# Don't ship .la files:
rm -f $PKG/{,usr/}lib${LIBDIRSUFFIX}/*.la

find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF | cut -f 1 -d : | xargs -r strip --strip-unneeded 2> /dev/null || true
find $PKG | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs -r strip -g 2> /dev/null || true

if [ -d $PKG/usr/man ]; then
  find $PKG/usr/man -type f -exec chmod 644 {} \; -exec gzip -9 {} \;
  for i in $( find $PKG/usr/man -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
fi

# Prepare the config files:
rename .sample .new $PKG/etc/*.sample

# Install rc script and logrotate entry
install -Dm755 $CWD/files/rc.clamav $PKG/etc/rc.d/rc.clamav.new
install -Dm644 $CWD/files/logrotate.clamav $PKG/etc/logrotate.d/clamav.new

# Create directories and files for pid, socket, db, and log.
mkdir -p $PKG/var/{lib,log}/clamav/
mkdir -p $PKG/run/clamav/
chown clamav:clamav $PKG/var/{lib,log}/clamav/
chown clamav:clamav $PKG/run/clamav/
touch $PKG/var/log/clamav/{clamd,freshclam}.log.new

# Fixup some ownership and permissions issues
chown -R root:root $PKG
chmod -R o-w $PKG
chmod 0770 $PKG/var/lib/clamav
touch $PKG/var/lib/clamav/main.cvd $PKG/var/lib/clamav/daily.cvd
chmod 0660 $PKG/var/lib/clamav/*

# Create log files in such a way that they won't clobber existing ones
touch $PKG/var/log/clamav/clamd.log.new \
  $PKG/var/log/clamav/freshclam.log.new
chmod 660 $PKG/var/log/clamav/clamd.log.new \
  $PKG/var/log/clamav/freshclam.log.new

chown -R clamav:clamav $PKG/var/lib/clamav $PKG/var/log/clamav \
  $PKG/run/clamav

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
cat $CWD/doinst.sh > $PKG/install/doinst.sh

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