#!/bin/bash
#
# 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.
#
# Hue web server
#
# chkconfig: 345 90 10
# description: Hue web server
# pidfile: /var/run/hue/supervisor.pid
#
### BEGIN INIT INFO
# Provides:          hue_webserver
# Required-Start:    $syslog $remote_fs
# Should-Start:
# Required-Stop:     $syslog $remote_fs
# Should-Stop:
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: Hue web server
### END INIT INFO

source /lib/lsb/init-functions


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


ERROR_PROGRAM_NOT_INSTALLED=5
ERROR_PROGRAM_NOT_CONFIGURED=6


RETVAL=0
LOCKFILE=/var/lock/subsys/hue_webserver
LOGDIR=/var/log/hue  # Log directory to use
PIDFILE=/var/run/hue/supervisor.pid
DAEMON=/usr/lib/hue/build/env/bin/supervisor # Introduce the server's location here
DAEMON_OPTS="-p $PIDFILE -l $LOGDIR -d"
DESC="Hue web server"
USER=hue
HUE_SHUTDOWN_TIMEOUT=15

export PYTHON_EGG_CACHE=/tmp/.hue-python-eggs

hue_start() {
  [ -x $DAEMON ] || exit $ERROR_PROGRAM_NOT_INSTALLED
  log_success_msg "Starting $DESC: "

  for dir in $(dirname $PIDFILE) $LOGDIR ${PYTHON_EGG_CACHE}
  do
    mkdir -p $dir
    chown -R $USER $dir
  done

  PATH=/usr/lib/hue/build/env/bin:$PATH start_daemon -u $USER $DAEMON $DAEMON_OPTS
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch $LOCKFILE
  return $RETVAL
}

hue_stop() {
  if [ ! -e $PIDFILE ]; then
    log_success_msg "Hue is not running"
    return 0
  fi

  log_success_msg "Stopping $DESC: "

  HUE_PID=`cat $PIDFILE 2>/dev/null`
  if [ -n "$HUE_PID" ]; then
    kill -TERM ${HUE_PID} &>/dev/null
    for i in `seq 1 ${HUE_SHUTDOWN_TIMEOUT}` ; do
      kill -0 ${HUE_PID} &>/dev/null || break
      sleep 1
    done
    kill -KILL ${HUE_PID} &>/dev/null
  fi
  echo
  rm -f $LOCKFILE $PIDFILE
  RETVAL=0
  return $RETVAL
}

hue_restart() {
  hue_stop
  hue_start
}


checkstatus(){
  pid=`cat "$PIDFILE" 2>/dev/null`
  if [ "$pid" = '' ]; then
    # The pidfile probably does not exist or is empty.
    if [ -e $LOCKFILE ]; then
      log_failure_msg "$DESC is dead and lock file exists"
      RETVAL=$STATUS_DEAD_AND_LOCK
    else
      log_failure_msg "$DESC is not running"
      RETVAL=$STATUS_NOT_RUNNING
    fi
    return $RETVAL
  fi

  set -- $pid
  pid="$1"
  ps -fp $pid | grep $pid | grep -i hue > /dev/null 2>&1
  status=$?

  if [ "$status" = 0 ]; then
    log_success_msg "$DESC is running"
    RETVAL=$STATUS_RUNNING
  elif [ -e $LOCKFILE ]; then
    log_failure_msg "$DESC is dead and lock file exists"
    RETVAL=$STATUS_DEAD_AND_LOCK
  else
    # pidfile exists, that's how we ended up here (by checking the
    # the pid's status through ps
    log_failure_msg "$DESC is dead and pid file exists"
    RETVAL=$STATUS_DEAD
  fi
return $RETVAL
}

hue_condrestart(){
  [ -e $LOCKFILE ] && restart || :
}

check_for_root() {
  if [ $(id -ur) -ne 0 ]; then
    echo 'Error: root user required'
    echo
    exit 1
  fi
}

service() {
  case "$1" in
    start)
      check_for_root
      hue_start
      ;;
    stop)
      check_for_root
      hue_stop
      ;;
    status)
      checkstatus
      ;;
    restart)
      check_for_root
      hue_restart
      ;;
    condrestart|try-restart)
      check_for_root
      hue_condrestart
      ;;
    *)
      echo $"Usage: $0 {start|stop|status|restart|try-restart|condrestart}"
      exit 1
  esac
}

service "$1"

exit $RETVAL
