#!/usr/bin/env bash
# This launcher sets the Debian-specific multiarch tuples as the SNAP_ARCH_TRIPLET environmental variable for other launchers' ease

set \
	-o errexit \
	-o errtrace \
	-o nounset \
	-o pipefail

if ! test -v SNAP_ARCH; then
	printf -- \
		'%s: Error: This launcher requires SNAP_ARCH environmental variable to be set and exported.\n' \
		"$(basename "${BASH_SOURCE[0]}")"
	exit 1
fi

declare \
	SNAP_ARCH_TRIPLET

# Refer:
#
# * Environmental variables - doc - snapcraft.io
#   https://forum.snapcraft.io/t/environmental-variables/7983
# * Multiarch/Tuples - Debian Wiki
#   https://wiki.debian.org/Multiarch/Tuples
#   NOTE: Only consider Linux archs with the `released` status in Debian for now
case "${SNAP_ARCH}" in
	# These are the special cases
	amd64)
		SNAP_ARCH_TRIPLET=x86_64-linux-gnu
	;;
	armel)
		SNAP_ARCH_TRIPLET=arm-linux-gnueabi
	;;
	armhf)
		SNAP_ARCH_TRIPLET=arm-linux-gnueabihf
	;;
	arm64)
		SNAP_ARCH_TRIPLET=aarch64-linux-gnu
	;;
	ppc64el)
		SNAP_ARCH_TRIPLET=powerpc64le-linux-gnu
	;;
	# Consider rest of them not exceptions
	s390x \
	|*)
		SNAP_ARCH_TRIPLET="${SNAP_ARCH}"-linux-gnu
	;;
esac

export \
	SNAP_ARCH_TRIPLET

# Finally run the launching command
exec "${@}"
