#!/bin/bash
#
# (c) Copyright 2011 Cloudera, Inc.
#
# init script for the Cloudera SCM Server.
#
# chkconfig: 2345 90 10
# description: Cloudera SCM Server.

### BEGIN INIT INFO
# Provides: cloudera-scm-server
# Required-Start: $local_fs $network $remote_fs $syslog +cloudera-scm-server-db +mysql +postgresql
# Should-Start:
# Required-Stop:
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: Cloudera SCM Server
# Description: Cloudera SCM Server
### END INIT INFO

# Source function library.
if [ -f /etc/rc.d/init.d/functions ]; then
  . /etc/rc.d/init.d/functions
elif [ -f /lib/lsb/init-functions ]; then
  . /lib/lsb/init-functions
  . /etc/rc.status
  rc_reset
fi

prog="cloudera-scm-server"
start_timeout=5
kill_timeout=5

# Source defaults file.
CMF_DEFAULTS=$(readlink -e $(dirname ${BASH_SOURCE-$0})/../default)
CMF_DEFAULTS=${CMF_DEFAULTS:-/etc/default}

[ -e $CMF_DEFAULTS/$prog ] && . $CMF_DEFAULTS/$prog

# Paths to configuration, binaries, etc
SERVER_SCRIPT=${CMF_SBINDIR:-/usr/sbin}/cmf-server

SERVER_OUT="${CMF_VAR:-/var}/log/$prog/$prog.out"
USER=cloudera-scm
GROUP=cloudera-scm

CMF_SUDO_CMD=${CMF_SUDO_CMD:-"su $USER -s"}

# The binary to pass to checkpid and killproc on SuSE
# depends on whether a CMF_SUDO_CMD was used. If not,
# then the real binary is passed, otherwise it has
# to be the CMF_SUDO_CMD binary.
if [ "$CMF_SUDO_CMD" = " " ]; then
  binary="java"
else
  binary=$(echo $CMF_SUDO_CMD | cut -d ' ' -f 1)
fi

if [ ! -f $SERVER_SCRIPT ]; then
  echo "File not found: $SERVER_SCRIPT"
  exit 1
fi

#pid file
pidfile=${PIDFILE-${CMF_VAR:-/var}/run/$prog.pid}

is_suse=`[ -e /etc/SuSE-release ] && echo 1 || echo 0`

RETVAL=0

local_checkpid() {
  if [ $is_suse -eq 1 ]; then
    pidofproc -p $pidfile $binary > /dev/null
  else
    checkpid `cat $pidfile`
  fi
  RETVAL=$?
  return $RETVAL
}

local_killproc() {
  if [ $is_suse -eq 1 ]; then
    killproc -t $kill_timeout -p $pidfile $binary
  else
    killproc -p $pidfile -d $kill_timeout $prog
  fi
  RETVAL=$?
  return $RETVAL
}

local_check_start() {
  if [ $is_suse -eq 1 ]; then
    local_checkpid && log_success_msg || log_failure_msg
  else
    local_checkpid && echo_success || echo_failure
  fi
  return $RETVAL
}

local_print_stop() {
  if [ $is_suse -eq 1 ]; then
    [ $RETVAL = 0 ] && log_success_msg || log_failure_msg
  else
    # killproc prints on Redhat.
    echo
  fi
}

local_check_success() {
  if [ $is_suse -eq 1 ]; then
    [ $RETVAL = 0 ] && log_success_msg || log_failure_msg
  else
    [ $RETVAL = 0 ] && echo_success || echo_failure
  fi
}

start() {
  # check to see if the server is already running by looking at the pid file
  # and grepping the process table.
  if [ -f $pidfile ] && local_checkpid; then
    echo "$prog is already running"
    exit 0
  fi

  echo -n "Starting $prog: "
  $CMF_SUDO_CMD /bin/bash -c "nohup $SERVER_SCRIPT $CMF_SERVER_ARGS" > $SERVER_OUT 2>&1 </dev/null &
  # pid is technically su's but it will die when the server dies.
  server_pid=$!
  echo $server_pid > $pidfile
  # sleep so the process can make its way to the process table - and out again
  # if it fails at startup - which could take a while.
  sleep $start_timeout
  disown -ar
  # print status information.
  local_check_start
  RETVAL=$?
  echo
  return $RETVAL
}

stop() {
  # check if the process is already stopped by seeing if the pid file exists.
  if [ ! -f $pidfile ]; then
    echo "$prog is already stopped"
    exit 0
  fi
  echo -n $"Stopping $prog: "
  local_killproc
  RETVAL=$?
  local_print_stop
  [ $RETVAL = 0 ] && rm -f ${pidfile}
}

status_fn() {
  if [ $is_suse -eq 1 ]; then
    echo -n "Checking for service $prog"
    local_checkpid
    rc_status -v
  else
    status -p $pidfile $prog
  fi
  RETVAL=$?
}

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

exit $RETVAL
