#
#  Bash completion, common file for all daemons
#

_zabbix-daemons-runtime()
{
cur=$1

# when main process starts supporting runtime loglevel changing, it should be added here, too
runtime_processes_common=( alerter "configuration\ syncer" discoverer "history\ syncer" housekeeper
 "http\ poller" "icmp\ pinger" "ipmi\ poller" "java\ poller" poller self-monitoring trapper "unreachable\ poller"
 "vmware\ collector" )
runtime_processes_server=( "db\ watchdog" escalator "proxy\ poller" timer  )
runtime_processes_proxy=( "data\ sender" "heartbeat\ sender" )
runtime_processes_single="alerter|configuration\ syncer|housekeeper|self-monitoring|db\ watchdog|escalator|data\ sender|heartbeat\ sender|collector"

case $2 in
	server)
		runtime_processes=( "${runtime_processes_server[@]}" "${runtime_processes_common[@]}" )
		;;
	proxy)
		runtime_processes=( "${runtime_processes_proxy[@]}" "${runtime_processes_common[@]}" )
		;;
	agentd)
		runtime_processes=( collector listener "active\ checks" )
esac

if [[ ${cur} =~ = ]]; then
	if [[ ${cur} =~ [a-z]$ ]]; then
		# no need to parse pids anymore, process name should be completed instead
		# should be extended to offer actual process count numbers

		# there does not seem to be a way to pass an array
		#  (or any other way to pass parameters with spaces) to compgen
		runtime_proc_count=${#runtime_processes[@]}
		current_process=${cur#*=}
		for ((i=0; $i<=$runtime_proc_count; i++)); do
			[[ ${runtime_processes[i]} = "${current_process}"* ]] || {
				unset runtime_processes[i]
			}
		done
		COMPREPLY=( "${runtime_processes[@]}" )
		if [[ ${#COMPREPLY[@]} = 1 ]]; then
			# only one process left in the return array
			[[ ${COMPREPLY[0]} =~ $runtime_processes_single ]] || {
				# this will break if there's ever a process name that is a continuation of a "single" process
				COMPREPLY+=( "${COMPREPLY[0]}", )
			}
		fi
		return 0
	fi
	# if -c is passed, we grab the child pids for the pid from the pidfile. Include directive is not supported
	# if -c is not yet passed, we should find out the default config file path for the used binary, but that is not possible yet
	[[ -f "$config_path" ]] && {
		pid_file=$(awk -F= '/^ *PidFile/ {pidfile=$2} END{print pidfile}' "$config_path")
		[[ -f $pid_file ]] && {
			parent_pid=$(cat "$pid_file")
		}
	}

	[[ $parent_pid ]] && {
		# currently log level changing is not supported for the main process
		# $pids should include $parent_pid once that is fixed
		ps_args=( --ppid $parent_pid )
	} || {
		# if the parent pid was not found, fall back to all processes by name
		# for example, config file might be unreadable for the current user when using sudo
		ps_args=( -C ${3##*/} )
	}

	if [[ ${cur} =~ , ]]; then
		process_part=${cur#*=}
		process_name=${process_part%,*}
		process_name_unescaped=${process_name/\\/}
		store_IFS=$IFS
		IFS=$'\n'
		process_count=$(ps ${ps_args[@]} -o args= | awk -F"[:#[]" '{print $2}' | grep -c "^ $process_name_unescaped $")
		[[ $process_count -gt 0 ]] && {
			# completion proposals will have process name prepended before the number. the only way around
			#  this seems to be set COMP_WORDBREAKS globally, which is not a good idea (it would affect
			#  other completions). setting COMP_WORDBREAKS at any level in a single completion does not seem
			#  to work.
			COMPREPLY=( $(compgen -P "${process_name}," -W "{1..$process_count}" -- ${cur#*,}) )
		}
		IFS=$store_IFS
		return 0
	fi
	pids=$(ps ${ps_args[@]} -o pid=)
	# passing empty string to compgen makes it exit with code 1 and array assignment fails
	pidarray=( $(compgen -W "${pids}" -- ${cur#*=}) )
	[[ ${cur} =~ =$ ]] && {
		COMPREPLY=( "${pidarray[@]}" "${runtime_processes[@]}" )
	} || {
		COMPREPLY=( "${pidarray[@]}" )
	}
	return 0
fi
COMPREPLY=( $(compgen -W "${runtime_options}" -- ${cur}) )
if [[ $runtime_loglevel_options =~ ${COMPREPLY[@]} ]]; then
	compopt -o nospace
	if [[ ${#COMPREPLY[@]} = 1 ]]; then
		# only one of increase/decrease left in the return array
		# quite hackish
		COMPREPLY+=( "${COMPREPLY[0]}"= )
	fi
fi
}
