#!/bin/sh
#
# Main script that calls other scripts for different factory testing activities
#
# Not setting -e, because there's specific error handing in script

# Usage info
show_help()
{
	# Show help
	echo "List of available commands:"
	ls /usr/share/factory-tool/scripts/ | grep '\.sh$' | sed 's/^/\t/' | sed 's/\.sh$//'
	echo -e "\nFor specific help use:\n\tfactory_tool <COMMAND> help"
}

if [ $# -eq 0 ]; then
	echo "Error: no command provided"
	echo
	show_help
	exit 1;
fi

COMMAND="$1"

if [ "$COMMAND" = "help" ]; then
	show_help
	exit 0
elif [ -f /usr/share/factory-tool/scripts/"$COMMAND".sh ]; then
	# Execute command
	shift
	/usr/share/factory-tool/scripts/"$COMMAND".sh "$@"
	# Print error, if subcommand failed
	EXIT_STATUS=$?
	if [ $EXIT_STATUS -ne 0 ]; then
		echo "Error: executing command $COMMAND failed"
	fi
	exit $EXIT_STATUS
else
	# Command not found
	echo "Error: unknown command $COMMAND"
	echo
	show_help
	exit 1
fi
