#!/bin/bash
#
# Copyright 2018 Confluent Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

TARGET=`ps ax | egrep -i "$1" | grep java | grep -v grep | awk '{print $1}'`
if [ "x$TARGET" = "x" ]; then
  >&2 echo "No running instance found."
  exit 1
fi

kill $TARGET
for i in `seq 20`; do
  sleep 0.25
  ps ax | egrep -i "$1" | grep "$TARGET" > /dev/null
  if [ $? -ne 0 ]; then
    exit 0
  fi
done

>&2 echo "Tried to kill $TARGET but never saw it die"
exit 1
