#!/bin/bash
# chkconfig: 2345 20 80
# description: NetApp 7-Mode Transition Tool Service.
#
# Source function library.
. /etc/init.d/functions

INSTALL_DIR=
BIN_DIR=$INSTALL_DIR/bin

PID=$(ps -A u | grep "com.netapp.transition.server.ApiServerMain" | grep -v "grep" | sed 's/ \+/ /g'| cut -d' ' -f2)

start() {
    if [ x$PID == "x" ]; then
        echo "Starting the transition-service (NetApp 7-Mode Transition Tool server)."
        nohup $BIN_DIR/transition-server > /dev/null 2>&1&
        
        ### Create the lock file ###
        touch /var/lock/subsys/7MTT
        success $"transition-service server startup"
        echo
    else
        echo "The transition-service (NetApp 7-Mode Transition Tool server) is already running. PID=$PID."
    fi
}

stop() {
    if [ x$PID == "x" ]; then
        echo "The transition-service (NetApp 7-Mode Transition Tool server) is not running."
        echo
    else
        echo "Stopping the transition-service (NetApp 7-Mode Transition Tool server). PID=$PID"
        kill -9  $PID
        while [ $? == 0 ]
        do
            sleep 1
            kill -9  $PID > /dev/null 2>&1
        done
        
        ### Now, delete the lock file ###
        rm -f /var/lock/subsys/7MTT
        PID=""
        echo    
    fi
}

status() {
    if [ x$PID == "x" ]; then
        echo "The transition-service (NetApp 7-Mode Transition Tool server) is not running."
    else
        echo "The transition-service (NetApp 7-Mode Transition Tool server) is running. PID=$PID."
    fi
}

usage()
{
    echo "Linux service: transition-service (NetApp 7-Mode Transition Tool server)"
    echo "usage: service transition-service [start | stop | restart | status]";
    exit 1
}

#Step:0 Command line args parsing
case "$#" in
1)
  if [ x$1 = "xstart" ]
  then
    start
  elif [ x$1 = "xstop" ]
  then
    stop
  elif [ x$1 = "xrestart" ]
  then
    stop
    start
  elif [ x$1 = "xstatus" ]
  then
    status
  else
    usage
    exit 1
  fi
  ;;
*)
  usage
  exit 1
  ;; 
esac
