#!/bin/bash
# -*- mode: shell-script; indent-tabs-mode: t; sh-basic-offset: 8; sh-indentation: 8; tab-width: 8 -*-

script_path="$(dirname ${0})"
script_name="$(basename ${0})"
pbench_bin="$(realpath -e ${script_path}/..)"

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

# This script will post-process all tools that belong to a specific group.
# This is typically called by a benchmark script when some activity needs to
# monitored.

# Defaults
def_group="default"
group="${def_group}"
dir=""

function usage {
	printf "The following are required:\n\n"
	printf -- "\t-g str --group=str, str = a tool group used in a benchmark\n"
	printf -- "\t                          (the default group is '%s')\n\n" "${def_group}"
	printf -- "\t-d str --dir=str, str = a directory where %s\n" "${script_name}"
	printf -- "\t                        will store and process data\n"
}

# Process options and arguments

opts=$(getopt -q -o d:g: --longoptions "dir:,group:" -n "getopt.sh" -- "${@}")
if [[ ${?} -ne 0 ]]; then
	printf "\n%s: you specified an invalid option\n\n" "${script_name}"
	usage >&2
	exit 1
fi
eval set -- "${opts}"
while true; do
	case "${1}" in
	-g|--group)
		shift
		if [[ -n "${1}" ]]; then
			group="${1}"
			shift
		fi
		;;
	-d|--dir)
		shift
		if [[ -n "${1}" ]]; then
			dir="${1}"
			shift
		fi
		;;
	--)
		shift
		break
		;;
	esac
done

if [[ -z "${group}" ]]; then
	printf -- "ERROR: required tool group parameter missing.\n\n" >&2
	usage >&2
	exit 1
fi
if [[ -z "${dir}" ]]; then
	printf -- "ERROR: required directory argument missing.\n\n" >&2
	usage >&2
	exit 1
fi

# This tool group's directory which stores the list of tools and their
# options, etc.
tool_group_dir="$(verify_tool_group "${group}")"
if [[ ${?} -ne 0 || -z "${tool_group_dir}" ]]; then
	exit 1
fi

# The tool group's directory which stores tool output for all hosts.
tool_output_dir="${dir}/tools-${group}"
if [[ ! -d "${tool_output_dir}" ]]; then
	error_log "[${script_name}] expected tool output directory, \"${tool_output_dir}\", does not exist"
	exit 1
fi

let failures=0
for dirent in $(/bin/ls -1 ${tool_group_dir}); do
        if [[ "${dirent}" == "__trigger__" ]]; then
		# Ignore trigger files
		continue
	elif [[ ! -d ${tool_group_dir}/${dirent} ]]; then
		# Skip spurious files of ${tool_group_dir}
		warn_log "[${script_name}] \"${this_tool_file}\" is a file in \"${tool_group_dir}\"; that should not happen. Please consider deleting it."
		continue
	fi
	# FIXME: add support for label applied to the hostname directory.
	host_tool_output_dir="${tool_output_dir}/${dirent}"
	if [[ -d "${host_tool_output_dir}" ]]; then
		for filent in $(/bin/ls -1 ${tool_group_dir}/${dirent}); do
        		if [[ ! -f "${tool_group_dir}/${dirent}/${filent}" ]]; then
				# Ignore unrecognized directories or symlinks
				continue
			fi
        		if [[ "${filent}" == "__label__" ]]; then
				# Ignore label files
				continue
			fi
			if [[ ! -x ${pbench_bin}/tool-scripts/${filent} ]]; then
				# Ignore unrecognized tools
				continue
			fi
			if [[ "${filent}" == "node-exporter" || "${filent}" == "dcgm" || "${filent}" == "pcp" || "${filent}" == "pcp-transient" ]]; then
                                # To be removed when converted to python
                                continue
			fi
			${pbench_bin}/tool-scripts/${filent} --postprocess --dir=${host_tool_output_dir} "${tool_opts[@]}" >> ${host_tool_output_dir}/postprocess.log 2>&1
			if [[ ${?} -ne 0 ]]; then
				cat ${host_tool_output_dir}/postprocess.log >&2
				(( failures++ ))
			fi
		done
	else
		warn_log "[${script_name}] Missing tool output directory, '${host_tool_output_dir}'"
		(( failures++ ))
	fi
done

exit ${failures}
