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

# qemu.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright 2007, 2008, 2009, 2010, 2012, 2016, 2017, 2019, 2020, 2021  Eric Hameleers, Eindhoven, Netherlands
# Copyright 2026  Patrick J. Volkerding, Sebeka, Minnesota, USA
# 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.

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

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

# Detect whether we're building for /patches:
# This function sets the variable 'SLACKPATCHING'
slack_findpkgstore_is_stablerelease

# Temporary build locations:
shm_tmp # Use /dev/shm if >8GB RAM is available & not mounted 'noexec'
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD
export PKG2=$TMP/package-qemu-guest-agent
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD
rm -rf $PKG2 ; mkdir -p $PKG2

# Allow user to specify a specific set of supported audio backends:
AUDIO_DRV=${AUDIO_DRV:-"sdl,alsa,oss,pa,pipewire"}

# Build with SPICE protocol support by default:
USE_SPICE_PROTOCOL=${USE_SPICE_PROTOCOL:---enable-spice-protocol}

# Build with SPICE support by default:
USE_SPICE=${USE_SPICE:---enable-spice}

# Build with libslirp support by default:
USE_SLIRP=${USE_SLIRP:---enable-slirp}

# Build with usbredir support by default:
USE_USBREDIR=${USE_USBREDIR:---enable-usb-redir}

# Build with VNC support by default:
USE_VNC=${USE_VNC:---enable-vnc --enable-vnc-sasl --enable-vnc-jpeg}

# Default 32-bit targets:
TARGETS32=${TARGETS32:-i386-softmmu,i386-linux-user,arm-softmmu,arm-linux-user,armeb-linux-user,riscv32-softmmu,riscv32-linux-user,sparc-softmmu,sparc-linux-user}

# Default 64-bit targets:
TARGETS64=${TARGETS64:-x86_64-softmmu,x86_64-linux-user,aarch64-softmmu,aarch64-linux-user,loongarch64-softmmu,loongarch64-linux-user,riscv64-softmmu,riscv64-linux-user,sparc64-softmmu,sparc64-linux-user}

# Unless TARGET_LIST was provided by the user, set the target list based
# on 32 or 64 bit arch:
if [ "$(getconf LONG_BIT)" = "64" ]; then
   # Set default target list for 64-bit.
   TARGET_LIST=${TARGET_LIST:-"$TARGETS64,$TARGETS32"}
 else
   # Set default target list for 32-bit.
   TARGET_LIST=${TARGET_LIST:-"$TARGETS32"}
fi

case $ARCH in
   aarch64)   KVMARCH="aarch64";;
   *)         export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig*)
cd $PKGNAM-*/ || failextract
slackhousekeeping

# Refresh libtool to support latest architectures:
slackupdatelibtool

# Configure:
slack_autotoolsprep
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
unshare -n ./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --sysconfdir=/etc \
   --localstatedir=/var \
   --docdir=/usr/doc/$PKGNAM-$VERSION \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --enable-gtk \
   --enable-malloc=jemalloc \
   --enable-kvm \
   --enable-linux-io-uring \
   --enable-nettle \
   --enable-sdl \
   --enable-system \
   --enable-vde \
   --enable-virtfs \
   --target-list=${TARGET_LIST} \
   --audio-drv-list=${AUDIO_DRV} \
   $USE_SPICE_PROTOCOL \
   $USE_SPICE \
   $USE_SLIRP \
   $USE_USBREDIR \
   $USE_VNC \
   --host=${SLK_ARCH_HOST} \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
make $NUMJOBS || make || failmake

# Install into package framework:
make install DESTDIR=$PKG || failinstall

# Create "qemu-kvm" symlink. some 3rd party frontends look for it:
[ ! -z $KVMARCH ] && ln -s qemu-system-$KVMARCH $PKG/usr/bin/qemu-kvm

# We might as well install the guest agest boot script here also, just to make
# the QEMU package complete. Someone might install it in a guest when they
# really only needed the guest agent, and that should be fine.
mkdir -p $PKG/etc/rc.d
install -vpm644 $CWD/rc.qemu-ga $PKG/etc/rc.d/rc.qemu-ga.new

# Add documentation:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
  COPYING* Changelog LICENSE* MAINTAINERS* README* VERSION* \
  $PKG/usr/doc/$PKGNAM-$VERSION
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall        # strip all .a archives and all ELFs
#slackstriprpaths    # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i      # compress man & info pages and delete usr/info/dir
slackslack           # set standard Slackware file/dir permissions and ownerships
slackdesc            # install slack-desc and doinst.sh
slackhlinks     # search for any hard links

# Build the "qemu" package:
slackmp              # run makepkg -l y -c n

# Build the "qemu-guest-agent" package:
( cd $PKG2
  # Install the binary:
  mkdir -p $PKG2/usr/bin
  cp -a $PKG/usr/bin/qemu-ga $PKG2/usr/bin/qemu-ga
  # Install helper script which gets called when the host issues a 'fsfreeze':
  mkdir -p $PKG2/etc/qemu/fsfreeze-hook.d
  install -vpm755 scripts/qemu-guest-agent/fsfreeze-hook $PKG2/etc/qemu/fsfreeze-hook
  # Install a boot script:
  mkdir -p $PKG2/etc/rc.d
  install -vpm644 $CWD/rc.qemu-ga $PKG2/etc/rc.d/rc.qemu-ga.new
  # Add a documentation directory (at least the licenses):
  mkdir -p $PKG2/usr/doc/${PKGNAM}-guest-agent-$VERSION
  cp -a \
    COPYING* LICENSE* \
    $PKG2/usr/doc/${PKGNAM}-guest-agent-$VERSION

  # Package metadata:
  mkdir -p $PKG2/install
  install -vpm644 $CWD/qemu-guest-agent.doinst.sh $PKG2/install/doinst.sh
  install -vpm644 $CWD/qemu-guest-agent.slack-desc $PKG2/install/slack-desc

  PKGNAM=qemu-guest-agent # so that slackmp wipes the old package, not the primary qemu package
  # and now override the package name with the sub package:
  SLACKPACKAGE=qemu-guest-agent-$VERSION-$PKGARCH-$BUILD.txz slackmp )
