#!/bin/sh
#
# @@@ START COPYRIGHT @@@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.
#
# @@@ END COPYRIGHT @@@
# trafodion
# chkconfig: 06 96 04       
# description:  stops trafodion  only
#
#
### BEGIN INIT INFO
# Provides: trafodion
# Required-Start:
# Required-Stop:
# Default-Start: 
# Default-Stop: 0 6
# Short-Description: stops trafodion node 
# Description: stops trafodion node if it is up
### END INIT INFO

# Source function library.
. /etc/init.d/functions

if [ ! -f /etc/trafodion/trafodion_config ]; then
   exit 1
fi
. /etc/trafodion/trafodion_config
if [ -z $TRAF_HOME ]; then
   exit 1
fi
TRAF_USER=${TRAF_USER:-"trafodion"}
TRAF_SUDO_CMD=${TRAF_SUDO_CMD:-"su ${TRAF_USER} --login "}
TRAF_SHELL_CMD=${TRAF_SHELL_CMD:-sqshell}
TRAF_OUT=${TRAF_VAR:-/var}/log/trafodion/${TRAF_SHELL_CMD}.log
TRAF_NODE_NAME=`uname -n`


node_start() {
echo "trafodion start not implemented"
}

node_stop() {
nid=`${TRAF_SUDO_CMD} -c "${TRAF_HOME}/sql/scripts/${TRAF_SHELL_CMD} -c node info | grep -B1 ${TRAF_NODE_NAME} | grep Up | head -1 | cut -d ' ' -f 2"`
if [ $? != 0 ]; then 
   echo -n `date` >> ${TRAF_OUT} 2>&1 </dev/null 
   echo " trafodion on ${TRAF_NODE_NAME} is already stopped" >> ${TRAF_OUT} 2>&1 </dev/null 
   echo "trafodion on ${TRAF_NODE_NAME} is already stopped"
   return 1
fi
if [ "$nid" == "" ]; then
   echo -n `date` >> ${TRAF_OUT} 2>&1 </dev/null 
   echo " trafodion on ${TRAF_NODE_NAME} is already stopped" >> ${TRAF_OUT} 2>&1 </dev/null 
   echo "trafodion on node ${TRAF_NODE_NAME} is already stopped"
else
   echo "Stopping trafodion node $nid on ${TRAF_NODE_NAME}"
   echo -n `date` >> ${TRAF_OUT} 2>&1 </dev/null 
   echo " Stopping trafodion node $nid on ${TRAF_NODE_NAME}" >> ${TRAF_OUT} 2>&1 </dev/null 
   ${TRAF_SUDO_CMD} /bin/bash -c "${TRAF_HOME}/sql/scripts/${TRAF_SHELL_CMD} -c down $nid" >> ${TRAF_OUT}  2>&1 </dev/null &
fi
return 0
}

node_status() {
nid=`${TRAF_SUDO_CMD} -c "${TRAF_HOME}/sql/scripts/${TRAF_SHELL_CMD} -c node info | grep -B1 ${TRAF_NODE_NAME} | grep Up | head -1 | cut -d ' ' -f 2"`
if [ $? != 0 ]; then 
   echo "trafodion on node $nid is not yet started"
   return 0
fi
if [ "$nid" == "" ]; then
   echo "trafodion on node ${TRAF_NODE_NAME} is not yet started"
else
   echo "trafodion on node ${TRAF_NODE_NAME} is started as logical node $nid"
fi
return 0
}


stop() {
node_stop
}

status() {
node_status
}

restart() {
stop
start
}

case "$1" in
  start)
    start
    RETVAL=$?
    ;;
  stop)
    stop
    RETVAL=$?
    ;;
  status)
    status
    RETVAL=$?
    ;;
  restart)
    restart
    RETVAL=$?
    ;;
  *)
    echo $"Usage: $prog {start|stop|restart|status}"
    RETVAL=3
esac

exit $RETVAL

