#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# Slackware build script for libvirt
# Modified by Brenton Earl <el0226@slackware.com> for Slackware ARM

# Written by Michal Bialozor <bialyy@o2.pl>
# Maintained by Robby Workman <rworkman@slackbuilds.org>
# Copyright 2023 Brenton Earl Grantsville, Utah, USA 
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * Neither the name of the <organization> nor the
#       names of its contributors may be used to endorse or promote products
#       derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
tar xvvf $PORTCWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig|*sha256)
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

VIRTUSER=${VIRTUSER:-root}
VIRTGROUP=${VIRTGROUP:-users}

# Some optional dependencies checks
audit="dis" ; if pkg-config --exists audit ; then audit="en" ; fi
bash_completion="" ; if pkg-config --exists bash-completion ; then
  bash_completion="-Dbash_completion=enabled -Dbash_completion_dir=/usr/share/bash-completion/completions"
fi
libiscsi="dis" ; if pkg-config --exists libiscsi ; then libiscsi="en" ; fi

# we have sysctld files in /etc/sysctl.d
sed -i "s|prefix / 'lib' / 'sysctl.d'|sysconfdir / 'sysctl.d'|" src/remote/meson.build

patch -p1 < $PORTCWD/use-virtgroup-in-polkit-rules.diff
sed -i -e "s,@VIRTGROUP@,$VIRTGROUP,g" src/remote/libvirtd.rules

# Since 5.10: "configure: error: Build directory must be different from source directory"
mkdir -p build
cd build
  CFLAGS="$SLKCFLAGS" \
  CXXFLAGS="$SLKCFLAGS" \
  meson setup .. \
    --buildtype=release \
    --infodir=/usr/info \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --localstatedir=/var \
    --mandir=/usr/man \
    --prefix=/usr \
    --sysconfdir=/etc \
    -Dqemu_group=$VIRTGROUP \
    -Dqemu_user=$VIRTUSER \
    $bash_completion \
    -Daudit=${audit}abled \
    -Dlibiscsi=${libiscsi}abled \
    -Dopenwsman=disabled \
    -Dapparmor=disabled \
    -Dselinux=disabled \
    -Dwireshark_dissector=disabled \
    -Ddriver_bhyve=disabled \
    -Ddriver_hyperv=disabled \
    -Ddriver_libxl=disabled \
    -Ddriver_vz=disabled \
    -Dsecdriver_apparmor=disabled \
    -Dsecdriver_selinux=disabled \
    -Dstorage_vstorage=disabled \
    -Ddtrace=disabled \
    -Dstrip=true \
    -Dinit_script=none || failmake
  "${NINJA:=ninja}"
  DESTDIR=$PKG $NINJA install || failinstall
cd ..

# we don't have augeas
rm -fr $PKG/usr/share/augeas

# this tree is recreated when starting the daemon
rm -fR $PKG/var/run

# add an rc.libvirt to start/stop/restart the daemon
install -D -m 0755 $PORTCWD/rc.libvirt $PKG/etc/rc.d/rc.libvirt.new

# we use $VIRTGROUP as our virtualization group, fix auth permissions, and
# consider the fact that by default we got no certs
sed -i \
  -e "s|^\#unix_sock_group\ =\ \"libvirt\"|unix_sock_group = \"$VIRTGROUP\"|" \
  -e "s|^\#unix_sock_rw_perms\ =\ \"0770\"|unix_sock_rw_perms = \"0770\"|" \
  -e "s|^\#auth_unix_ro.*|auth_unix_ro = \"none\"|" \
  -e "s|^\#auth_unix_rw.*|auth_unix_rw = \"none\"|" \
  -e "s|^\#listen_tls|listen_tls|" \
  $PKG/etc/libvirt/libvirtd.conf

# still, we use $VIRTGROUP as our virtualization group
sed -i \
  -e "s|^\#group\ =\ \"root\"|group = \"$VIRTGROUP\"|" \
  $PKG/etc/libvirt/qemu.conf

# disable seccomp support or else VMs won't start with new libvirt/qemu combo
# 20220212 bkw: this file doesn't exist if qemu wasn't installed. and
# since qemu isn't in REQUIRES, it's optional, so the build shouldn't fail.
if [ -e $PKG/etc/libvirt/qemu.conf ]; then
  sed -i  "s|^\#seccomp_sandbox = 1|seccomp_sandbox = 0|" \
    $PKG/etc/libvirt/qemu.conf
fi

mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
mv $PKG/usr/share/doc/$PKGNAM/* $PKG/usr/doc/$PKGNAM-$VERSION/
rm -fR $PKG/usr/share/doc
cp -a \
  *.rst COPYING* examples/polkit/* \
  $PKG/usr/doc/$PKGNAM-$VERSION

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
#slackstriprpaths     # strip rpaths
slackgzpages -i # compress man & info pages and delete usr/info/dir

# Apply generic Slackware packaging policies:
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n
# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
