#!/bin/bash

# chkconfig: 2345 90 60
# description: Karoo Bridge Daemon.
# processname: karoo
# pidfile: /var/run/karoo.pid

### BEGIN INIT INFO
# Provides: sahara
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: start and stop Sahara Karoo Bridge watchdog
# Description: Sahara Karoo Bridge watchdog
### END INIT INFO

INIT_DIR=/etc/karoo.conf.d/init

REDIS_START="${INIT_DIR}/redisd start"
REDIS_STOP="${INIT_DIR}/redisd stop"
REDIS_STATUS="${INIT_DIR}/redisd status"

VBRIDGE_START="${INIT_DIR}/videobridged start"
VBRIDGE_STOP="${INIT_DIR}/videobridged stop"
VBRIDGE_STATUS="${INIT_DIR}/videobridged status"

SECHURA_START="${INIT_DIR}/sechurad_node start"
SECHURA_STOP="${INIT_DIR}/sechurad_node stop"
SECHURA_STATUS="${INIT_DIR}/sechurad_node status"

SAHARA_START="${INIT_DIR}/saharad start"
SAHARA_STOP="${INIT_DIR}/saharad stop"
SAHARA_STATUS="${INIT_DIR}/saharad status"
SAHARA_RESTART="${INIT_DIR}/saharad restart"

MONIT_START="${INIT_DIR}/monitd start"
MONIT_STOP="${INIT_DIR}/monitd stop"
MONIT_STATUS="${INIT_DIR}/monitd status"

is_root_user()
{
  if [ "$(id -u)" != "0" ]; then
    return 1
  else
    return 0
  fi
}

do_sechura_stop()
{
  ${SECHURA_STOP} 0
  ${SECHURA_STOP} 1
  ${SECHURA_STOP} 2
  ${SECHURA_STOP} 3
  ${SECHURA_STOP} 4
}

do_stop()
{
  ${MONIT_STOP}
  ${SAHARA_STOP}
  ${REDIS_STOP}
  #${VBRIDGE_STOP}
  do_sechura_stop
}

do_stop_no_redis()
{
  ${MONIT_STOP}
  ${SAHARA_STOP}
  #${VBRIDGE_STOP}
  do_sechura_stop
}

do_sechura_start()
{
  if ! ${SECHURA_START} 0; then
    do_stop
    exit 1
  fi

  if ! ${SECHURA_START} 1; then
    do_stop
    exit 1
  fi

  if ! ${SECHURA_START} 2; then
    do_stop
    exit 1
  fi
  
  if ! ${SECHURA_START} 3; then
    do_stop
    exit 1
  fi

  if ! ${SECHURA_START} 4; then
    do_stop
    exit 1
  fi
}

do_start()
{
  if ! ${REDIS_START}; then
    do_stop
    exit 1
  fi

  do_sechura_start

  if ! ${SAHARA_START}; then
    do_stop
    exit 1
  fi

  #if ! ${VBRIDGE_START}; then
  #  do_stop
  #  exit 1
  #fi

  if ! ${MONIT_START}; then
    do_stop
    exit 1
  fi
}

do_status()
{
  ${MONIT_STATUS}
  ${SAHARA_STATUS}
  ${REDIS_STATUS}
  ${SECHURA_STATUS} 0
  ${SECHURA_STATUS} 1
  ${SECHURA_STATUS} 2
  ${SECHURA_STATUS} 3
  ${SECHURA_STATUS} 4
  #${VBRIDGE_STATUS}
}

if ! is_root_user; then
  echo "You need to run this service as root."
  exit 1
fi

case "$1" in
  start)
    do_start
    ;;
  stop)
    do_stop
    ;;
  stop-no-redis)
    do_stop_no_redis
    ;;
  restart-media)
    if ! ${SAHARA_STATUS}; then
      echo "Do a restart-hard instead"
      exit 1
    fi
    ${MONIT_STOP}
    do_sechura_stop
    do_sechura_start
    ${MONIT_START}
    ;;
  restart-redis)
    ${REDIS_STOP}
    ${REDIS_START}
  ;;
  restart-hard)
    do_stop
    do_start
    ;;
  restart)
    if ! ${SAHARA_RESTART}; then
      echo "Do a restart-hard instead"
    fi
    ;;
  status)
    do_status
  ;;
*)
echo "Usage: $0 (start|stop|stop-no-redis|restart|restart-media|restart-redis|restart-hard|status)"
esac
exit 0