#!/bin/bash
# -*- mode: shell-script; indent-tabs-mode: t; sh-basic-offset: 8; sh-indentation: 8; sh-indent-for-case-alt: + -*-

script_path=`dirname $0`
script_name=`basename $0`
pbench_bin="`cd ${script_path}/..; /bin/pwd`"

# source the base script
. "$pbench_bin"/base

# This script can list all tools from all groups,
# list tools from a specific group, or list which
# groups contain a specific tool

# Defaults
name=""
group=""
with_options=0

# Process options and arguments

function usage() {
	printf "\n"
	printf "The following options are available (use neither, one, or the other, but not both):\n\n"
	printf -- "\t-n str --name=str, list the perftool groups which str is used\n"
	printf "\n"
	printf -- "\t-g str --group=str, list the tools used in this str\n"
	printf "\n"
	printf -- "\t-o --with-options, list the options with each tool\n"
}

opts=$(getopt -q -o hon:g: --longoptions "help,with-options,name:,group:" -n "getopt.sh" -- "$@");
if [ $? -ne 0 ]; then
	printf -- "\n${*}\n" >&2
	printf -- "${script_name}: you specified an invalid option\n\n" >&2
	usage >&2
	exit 1
fi

eval set -- "$opts";
while true; do
	case "$1" in
	-n|--name)
		shift;
		if [ -n "$1" -a "$group" = "" ]; then
			name="$1"
			shift;
		else
			printf "You cannot specify both group and a name\n"
			exit 1
		fi
		;;
	-g|--group)
		shift;
		if [ -n "$1" -a "$name" = "" ]; then
			group="$1"
			shift;
		else
			printf "You cannot specify both group and a name\n"
			exit 1
		fi
		;;
	-o|--with-options)
		shift;
		with_options=1
		with_options_opt="--with-options" # used if we ssh for remote list
		;;
	-h|--help)
		usage
		exit 0
		;;
	--)
		shift;
		break;
		;;
	esac
done

if [ -d "$pbench_run" ]; then
	pushd "$pbench_run" >/dev/null
	if [ -z $name ]; then
		# list tools in one or all groups
		if [ -z "$group" ]; then
			groups=`/bin/ls -d tools-* 2>/dev/null | sed -e s/^tools-//g`
		else
			groups="$group"
		fi
		for group in $groups; do
			tools=""
			tool_group_dir="tools-$group"
			for this_tool_file in `/bin/ls $tool_group_dir`; do
				if [ "$this_tool_file" == "label" ]; then
					continue;
				fi
				if echo $this_tool_file | grep -q "^remote"; then
                                	remote_host=`echo $this_tool_file | awk -F@ '{print $2}'`
                                	tool=`echo $this_tool_file | awk -F@ '{print $1}'`
                                	remote_tools=`ssh $ssh_opts -n $remote_host ". ${pbench_install_dir}/profile; pbench-list-tools $with_options_opt --group=$group" | sed -e s/"^$group: "//`
					tools="$tools,$remote_host[$remote_tools]"
	                        elif [ -d $tool_group_dir/$this_tool_file ] ;then
					# skip spurious subdirectory of $tool_group_dir
					warn_log "[$script_name]$this_tool_file is a directory in $tool_group_dir; that should not happen. Please consider deleting it."
					continue
	                        elif [ ! -e "$pbench_bin/tool-scripts/$this_tool_file" ] ;then
					# skip spurious file - not a tool.
					warn_log "[$script_name]$this_tool_file does not exist in $pbench_bin/tool-scripts; spurious file perhaps? Please consider deleting it."
					continue
                        	else
					if [ $with_options -eq 1 ]; then
						this_tool_file="$this_tool_file `cat $tool_group_dir/$this_tool_file`"
					fi
					tools="$tools,$this_tool_file"
                        	fi
			done
			tools=`echo $tools | sed -e s/"^,"//`
			# do not print anything for this group if there are no tools
			if [ ! -z "$tools" ]; then
				printf "%s: %s\n" "$group" "$tools"
			fi
		done
	else 
		# list the groups which include this tool
		for group in `/bin/ls -d tools-* 2>/dev/null | sed -e s/^tools-//g`; do
			if /bin/ls tools-$group | grep -q "^$name"; then
				groups="$groups $group"
			fi
		done
		printf "name: $name groups: $groups\n"
	fi
	popd >/dev/null
fi
