#!/bin/sh
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Zygmunt Krynicki
set -eu

GARDEN_QEMU_SYSTEM=s390x
GARDEN_COMPONENT=qemu-"$GARDEN_QEMU_SYSTEM"

# Create a symbolic link that makes
# /snap/image-garden/current/components/qemu-aarch64 a valid path, matching
# build-time configuration of qemu.
ln -nfs ../../components/"$SNAP_REVISION"/"$GARDEN_COMPONENT" "$SNAP"/components/"$GARDEN_COMPONENT"

# If the component is not installed, fail with an error message.
if [ ! -d "$SNAP"/../components/"$SNAP_REVISION"/"$GARDEN_COMPONENT" ]; then
	echo "install $GARDEN_COMPONENT component to use $GARDEN_QEMU_SYSTEM emulation" >&2
	exit 1
fi

# Execute qemu-system-$GARDEN_QEMU_SYSTEM through the symbolic link created above.
exec "$SNAP"/components/"$GARDEN_COMPONENT"/bin/qemu-system-$GARDEN_QEMU_SYSTEM "$@"
