#! /bin/sh
# 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.

### BEGIN INIT INFO
# Provides:		solr-server
# Required-Start:	$remote_fs $syslog
# Required-Stop:	$remote_fs $syslog
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	Apache Solr as a Service
### END INIT INFO

STATUS_RUNNING=0
STATUS_DEAD=1
STATUS_DEAD_AND_LOCK=2
STATUS_NOT_RUNNING=3

SERVICE_DESC="Apache Solr server"
SOLR_INSTALL_DIR="/usr/odp/current/solr"

if [ ! -d "$SOLR_INSTALL_DIR" ]; then
  echo "$SOLR_INSTALL_DIR not found! Please check the SOLR_INSTALL_DIR setting in your $0 script."
  exit 1
fi

# Path to an include file that defines environment specific settings to override default
# variables used by the bin/solr script. It's highly recommended to define this script so
# that you can keep the Solr binary files separated from live files (pid, logs, index data, etc)
# see bin/solr.in.sh for an example
SOLR_ENV="/etc/default/solr"

if [ ! -f "$SOLR_ENV" ]; then
  echo "$SOLR_ENV not found! Please check the SOLR_ENV setting in your $0 script."
  exit 1
fi
. $SOLR_ENV
[ "$SOLR_HOME" = "" ] && SOLR_HOME="/var/lib/solr"
[ "$SOLR_PORT" = "" ] && SOLR_PORT="8983"
[ "$SOLR_PID_DIR" = "" ] && SOLR_PID_DIR="/var/run/solr"
SOLR_PID_FILE="$SOLR_PID_DIR/solr-$SOLR_PORT.pid"

. /lib/lsb/init-functions

# Specify the user to run Solr as; if not set, then Solr will run as root.
# Running Solr as root is not recommended for production environments
RUNAS="solr"

# verify the specified run as user exists
runas_uid="`id -u "$RUNAS"`"
if [ $? -ne 0 ]; then
  echo "User $RUNAS not found! Please create the $RUNAS user before running this script."
  exit 1
fi

check_status()
{
  pidofproc -p $SOLR_PID_FILE $SOLR_INSTALL_DIR/bin/solrd > /dev/null
  return $?
}

start_server()
{
  /bin/su -s /bin/bash -c \
    "$SOLR_INSTALL_DIR/bin/solrd start --config $SOLR_ENV" $RUNAS 2>&1 > /dev/null || return 2
}

stop_server()
{
  /bin/su -s /bin/bash -c \
    "$SOLR_INSTALL_DIR/bin/solrd stop --config $SOLR_ENV" $RUNAS 2>&1 > /dev/null || return 2
}

export PATH="${PATH:+$PATH:}/usr/odp/current/solr/bin:/usr/sbin:/sbin"

case "$1" in
  start)
    check_status > /dev/null 2>&1
    status=$?
    if [ $status = 0 ]; then
      log_success_msg "${SERVICE_DESC} is already running"
      exit 0
    fi
    start_server
    case "$?" in
      0|1) log_success_msg "Starting ${SERVICE_DESC} in cloud mode" ;;
      2) log_failure_msg "Starting ${SERVICE_DESC} in cloud mode" ;;
      *) log_failure_msg "Starting ${SERVICE_DESC} in cloud mode" ;;
    esac
  ;;
  stop)
    check_status > /dev/null 2>&1
    status=$?
    if [ $status != 0 ]; then
      log_success_msg "${SERVICE_DESC} is not running"
      exit 1
    fi

    stop_server
    case "$?" in
      0|1) log_success_msg "Stopping ${SERVICE_DESC}" ;;
      2) log_failure_msg "Stopping ${SERVICE_DESC}" ;;
      *) log_failure_msg "Stopping ${SERVICE_DESC}" ;;
    esac
  ;;

  restart)
    check_status > /dev/null 2>&1
    status=$?
    # stop running service
    if [ $status = 0 ]; then
      stop_server
      if [ $? != 0 ]; then
        log_failure_msg "Restarting ${SERVICE_DESC}"
        exit 1
      fi
    fi
    # start service
    start_server
    case "$?" in
      0|1) log_success_msg "Restarting ${SERVICE_DESC}" ;;
      2) log_failure_msg "Restarting ${SERVICE_DESC}" ;;
      *) log_failure_msg "Restarting ${SERVICE_DESC}" ;;
    esac
  ;;

  try-restart)
    check_status > /dev/null 2>&1
    status=$?
    if [ $status != 0 ]; then
      log_failure_msg "${SERVICE_DESC} is not running"
      exit 1
    fi
    stop_server
    if [ $status != 0 ]; then
      log_failure_msg "Restarting ${SERVICE_DESC}"
      exit 1
    fi
    # start service
    start_server
    case "$?" in
      0|1) log_success_msg "Restarting ${SERVICE_DESC}" ;;
      2) log_failure_msg "Restarting ${SERVICE_DESC}" ;;
      *) log_failure_msg "Restarting ${SERVICE_DESC}" ;;
    esac
  ;;

  status)
    pidofproc -p $SOLR_PID_FILE $SOLR_INSTALL_DIR/bin/solrd > /dev/null
	status=$?
    if [ $status = 0 ]; then
      log_success_msg "${SERVICE_DESC} is running"
	else
	  log_success_msg "${SERVICE_DESC} is stopped"
    fi
	exit $status
  ;;

  init)
    check_status > /dev/null 2>&1
    status=$?
    if [ $status = 0 ]; then
      log_failure_msg "${SERVICE_DESC} is running"
      exit 1
    fi
    rm -r ${SOLR_HOME}/*
    /usr/bin/solrctl init --force
  ;;

  *)
    log_action_msg "Usage: /etc/init.d/solr-server {start|stop|restart|try-restart|status|init}" || true
    exit 1
  ;;
esac

exit 0
