#!/usr/bin/bash

# SHLINT:yes
# Copyright (c) 2020 by NetApp, Inc.
# All Rights Reserved.

#
# This scripts stops the embedded KMIP server
#

readonly GREP="/usr/bin/grep"
readonly KILLALL="/usr/bin/killall"
readonly PS="/bin/ps"
readonly SPMCTL="/usr/sbin/spmctl"

ARG=$1

PWD=`pwd`

function stop_server
{
    local retVal=0
    local i=0

    ${SPMCTL} -s -h kmip_server
    ${KILLALL} kmip_server
    sleep 3

    while [ $i -lt 10 ]
    do
      echo "Checking if server killed"
      ${PS} ax | ${GREP} kmip_server | ${GREP} -v grep
      if [[ $? -eq 0 ]]; then
          ${SPMCTL} -s -h kmip_server
          ${KILLALL} kmip_server
          i=`expr $i + 1`
          sleep 1
      else
         echo "Server is killed"
         break
      fi
    done

    return $retVal
}

stop_server
if [ $? -ne 0 ]; then
    exit $?
fi

exit 0
