#!/bin/bash
set -e

# We need to override the killing during testing.
if [ "${CI:-false}" = "true" ]; then
  SENDSIG=$(command -v sendsig)
else
  SENDSIG="kill"
fi

kam_pid=$1

check_pid() {
  for p in ${KAM_PIDS} ; do
    if [ "$p" = "${kam_pid}" ] ; then
      return
    fi
  done
  echo "${kam_pid} not found" >&2
  exit 1
}

if [ -z "${kam_pid}" ] ; then
	${CMD} ps
else
  KAM_PIDS=$(${CMD} ps | awk '{ print $1}'|xargs)
  if [ "${kam_pid}" = "all" ] ; then
    for p in ${KAM_PIDS} ; do
      ${SENDSIG} -SIGUSR1 "$p"
      echo "SIGUSR1 sent to $p"
    done
  else
    check_pid
    ${SENDSIG} -SIGUSR1 "${kam_pid}"
    echo "SIGUSR1 sent to ${kam_pid}"
  fi
fi

exit 0
