#!/bin/sh
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Zygmunt Krynicki
set -eu
V="$(snapctl get spread-variant)"
case "$V" in
'')
	if [ ! -x "$SNAP"/../components/"$SNAP_REVISION"/spread/spread ]; then
		if ! echo "$SNAP_REVISION" | grep -E -q 'x[0-9]+'; then
			echo "Installing component with spread..." >&2
			exec snapctl install +spread >&2
		fi
	fi
	exit 0
	;;
[a-z][a-z0-9]*)
	if [ ! -x "$SNAP"/../components/"$SNAP_REVISION"/spread-"$V"/spread ]; then
		if ! echo "$SNAP_REVISION" | grep -E -q 'x[0-9]+'; then
			echo "Installing component with spread variant (spread-$V)..." >&2
			exec snapctl install +spread-"$V" >&2
		fi
	fi
	;;
*)
	echo "spread-variant may only use ASCII alphanumerical characters or dashes" >&2
	exit 1
	;;
esac
