#!/bin/sh
### BEGIN INIT INFO
# Provides:          sonar
# Required-Start:    $local_fs $remote_fs $network $time $named
# Required-Stop:     $local_fs $remote_fs $network $time $named
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Sonar
# Description:       Open platform to manage code quality
### END INIT INFO

NAME=sonar
SCRIPTNAME=/etc/init.d/$NAME

if [ `id -u` -ne 0 ]; then
  echo "The $NAME init script can only be run as root"
  exit 1
fi

MACHINE_TYPE=`getconf LONG_BIT`
if [ "${MACHINE_TYPE}" = "64" ]; then
  JSW=/opt/sonar/bin/linux-x86-64/sonar.sh
else
  JSW=/opt/sonar/bin/linux-x86-32/sonar.sh
fi

check_rc()
{
  if [ "$1" -ne 0 ]; then
    exit "$1"
  fi
}

do_start()
{
  su sonar -c "$JSW start"
  check_rc "$?"
}

do_stop()
{
  su sonar -c "$JSW stop"
  check_rc "$?"
}

do_status()
{
  su sonar -c "$JSW status"
  check_rc "$?"
}

do_restart()
{
  su sonar -c "$JSW restart"
  check_rc "$?"
}

case "$1" in
  start)
    do_start
    ;;
  stop)
    do_stop
    ;;
  status)
    do_status
    ;;
  restart|force-reload)
    do_restart
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
    exit 3
    ;;
esac

exit 0
