#!/bin/bash
#
# Copyright (C) SUSE LINUX GmbH 2018, all rights reserved.
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation; either version 2.1 of the License, or
# (at your option) version 3.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
# License for more details.

RAPIDO_DIR="$(realpath -e ${0%/*})"

declare -A short_help

short_help["teardown-network"]="Teardown network for VMs"
rapido_teardown_network()
{
	${RAPIDO_DIR}/tools/br_tap_teardown.sh "$@"
}

short_help["setup-network"]="Setup network for VMs"
rapido_setup_network()
{
	${RAPIDO_DIR}/tools/br_tap_setup.sh "$@"
}

# vm is an alias for boot
rapido_vm()
{
	${RAPIDO_DIR}/vm.sh
}

short_help["boot"]="Boot previously prepared test"
rapido_boot()
{
	${RAPIDO_DIR}/vm.sh
}

cut_usage()
{
		local progname="$(basename $0)"
		cat << EOF
Usage: $progname cut [-B] [-f file] [-x cmd] testname

-B:		cut testcase image only, don't boot it
-f <file>:	run script file within the VM upon boot
-x <cmd>:	run command string within the VM upon boot
<testname>:	testcase name. See '$progname list' for options
EOF
}

short_help["cut"]="Prepare testcase and boot it"
rapido_cut()
{
	local boot_img="yes"
	local option=""
	local post_autorun_files=()
	local cleanup="rm "
	local t cut_script
	while getopts "Bf:x:" option; do
		case $option in
		B)
			unset boot_img
			;;
		f)
			# existence checked via _rt_require_dracut_args()
			post_autorun_files+=($(realpath "$OPTARG"))
			;;
		x)
			t="$(mktemp --tmpdir rapido_post_autorun.XXXXXXXXXX)" \
				|| exit 1
			printf "%s" "$OPTARG" > "$t" || exit 1
			post_autorun_files+=("$t")
			cleanup="$cleanup \"$t\""
			;;
		*)
			echo "Invalid cut parameter"
			cut_usage
			exit 1
			;;
		esac
	done
	[ -f "$t" ] && trap "$cleanup" 0 1 2 3 15

	# shift away any processed params, so we're left with testname
	shift $(($OPTIND - 1))
	local testname=$1

	if [ $# -ne 1 -o -z "$testname" -o x"$testname" = "xhelp" ]; then
		cut_usage
		exit 1
	fi

	pushd "$RAPIDO_DIR" > /dev/null
	cut_script="cut/${testname//-/_}.sh"

	if [[ ! -x $cut_script ]]; then
		[[ -f $cut_script ]] \
		  && echo "$cut_script lacks execute permission." \
		  || echo "$testname not found. See \"rapido list\"."
		popd > /dev/null
		exit
	fi

	./$cut_script "${post_autorun_files[@]}"
	local cut_status=$?
	popd > /dev/null
	[ $cut_status -ne 0 ] && exit $cut_status
	[ -n "$boot_img" ] || exit 0
	"${RAPIDO_DIR}/vm.sh"
}

short_help["list"]="List testcases"
rapido_list()
{
	local t

	pushd "$RAPIDO_DIR" > /dev/null

	shopt -s nullglob
	for t in cut/*.sh; do
		[ -x "$t" ] || continue
		t="${t%.sh}"
		t="${t//_/-}"
		echo "${t#cut/}"
	done
	popd > /dev/null
}

list_commands()
{
	local command
	for command in $(declare -F); do
		[[ $command =~ rapido_([a-zA-Z_]*) ]] || continue
		command="${BASH_REMATCH[1]}"
		command="${command//_/-}"
		if [ -n "${short_help[$command]}" ]; then
			echo -e "\t$command - ${short_help[$command]}"
		fi
	done
}

short_help["help"]="Show help"
rapido_help()
{
	echo "Usage: $(basename $0) SUBCOMMAND [ARGUMENTS]"
	echo
	echo "The available subcommands are:"
	list_commands
}

if [ "$#" = "0" ]; then
	subcmd_func=rapido_help
else
	subcmd_func=rapido_${1//-/_}
	shift
fi
if ! declare -f $subcmd_func > /dev/null; then
	rapido_help
	exit 1
fi

$subcmd_func "$@"
