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

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

# Copyright 2009, 2010  Andrew Brouwers <abrouwers@gmail.com>
# Copyright 2013-2022  Edward W. Koenig, Vancouver, USA <kingbeowulf@linuxgalaxy.org>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''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 AUTHOR 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.

# 20230701 bkw: Modified by SlackBuilds.org, BUILD=2:
# - add doinst to update desktop database and icon cache.
# Note: sbopkglint <= 0.9.0 complains about ELF binaries found in
# /usr/share/qemu (intended for the target systems). This is an error
# in sbopkglint, and will be fixed (no need to change anything here).

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

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

KVMGROUP=${KVMGROUP:-users}
TARGETS=${TARGETS:-aarch64-softmmu}
VNC_ENABLE=${VNC_ENABLE:-yes}
AUDIODRIVERS=${AUDIODRIVERS:-pa,alsa,oss,sdl}
BRIDGE_HELPER_SETUID=${BRIDGE_HELPER_SETUID:-no}
SLIRP=${SLIRP:-=no}


# 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

# Needed to build man pages if built after plain "su"
export PATH=$PATH:/usr/share/texmf/bin

# Autodetect liburing support
uring="dis" ; if pkg-config --exists liburing ; then uring="en" ; fi

# Remove double CFLAGS
sed -i "s|^\ \ CFLAGS=\"-O2\ |  CFLAGS=\"|" configure

# Enable only x86* and arm emulation for qemu: if you want to build
# all the targets available use TARGETS=all
if [ "$TARGETS" = "all" ]; then
  targets=""
else
  targets="--target-list=$TARGETS"
fi

# To omit VNC capability, use VNC_ENABLE=no
if [ "$VNC_ENABLE" = "yes" ]; then
  with_vnc="--enable-vnc --enable-vnc-sasl --enable-vnc-jpeg"
else
  with_vnc="--disable-vnc"
fi

# To enable user mode networking use SLIRP=yes
if [ "$SLIRP" = "yes" ]; then
  with_slirp="--enable-slirp"
else
  with_slirp=""
fi

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --docdir=/usr/doc/$PKGNAM-$VERSION \
  --enable-gtk \
  --enable-system \
  --enable-kvm \
  --disable-debug-info \
  --enable-virtfs \
  --enable-sdl \
  --enable-nettle \
  --${uring}able-linux-io-uring \
  --audio-drv-list=${AUDIODRIVERS} \
  $with_slirp \
  $with_vnc \
  $targets || failconfig

make V=1 $NUMJOBS || make V=1 || failmake
make install DESTDIR=$PKG || failinstall

# Remove empty /var
rm -rf $PKG/var

# install some udev rules for /dev/kvm
# Note: no udev rules for usb pasthrough when running qemu as non-root on linux hosts. See Section 2.11.2
# https://qemu.weilnetz.de/doc/qemu-doc.html
mkdir -p $PKG/lib/udev/rules.d/
sed "s/@GROUP@/$KVMGROUP/" $CWD/65-kvm.rules \
  > $PKG/lib/udev/rules.d/65-kvm.rules

# create a symlink for qemu-kvm to support aqemu and such frontends
# even though qemu now has a gtk gui.
[ ! -z $KVMARCH ] && ln -s qemu-system-$KVMARCH $PKG/usr/bin/qemu-kvm

# set qemu-bridge-helper setuid if requested
if [ "$BRIDGE_HELPER_SETUID" = "yes" ]; then
  chmod u+s $PKG/usr/libexec/qemu-bridge-helper
fi

# Add a documentation directory:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -favv \
   MAINTAINERS COPYING* LICENSE README* \
   $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
slackgzpages -i # compress man & info pages and delete usr/info/dir
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
