#!/bin/sh

### BEGIN INIT INFO
# Provides:          mysqlrouter
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start / Stop MySQL Router
# Description:       This service script facilitates startup and shutdown of
#                    MySQL Router.
### END INIT INFO

# Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

#
# Maintainer: MySQL Release Engineering <mysql-build@oss.oracle.com>
#

. /lib/lsb/init-functions

DESC="Start / Stop MySQL Router"
DAEMON=/usr/sbin/mysqlrouter
DAEMON_OPTIONS=""
PROCNAME=mysqlrouter
NAME="MySQL Router"
RUNTIMEDIR=/var/run/mysqlrouter/
PID=${RUNTIMEDIR}/mysqlrouter.pid
STOP_RETRY=3
LOGDIR=/var/log/mysqlrouter
LOGFILE=${LOGDIR}/mysqlrouter.log

do_start() {

  local retval=0
  log_daemon_msg "Starting MySQL Router"

  if [ ! -x $DAEMON ]; then
    log_end_msg 1
    return 0
  fi

  start="start-stop-daemon --start --pidfile $PID --exec $DAEMON"
  $start --test -u mysql 1>/dev/null || retval=1

  if [ ! -d ${RUNTIMEDIR} -a ! -L ${RUNTIMEDIR} ];
  then
          mkdir ${RUNTIMEDIR}
          chown mysql:adm ${RUNTIMEDIR}
          chmod 750 ${RUNTIMEDIR}
  fi

  if [ ! -d ${LOGDIR} -a ! -L ${LOGDIR} ];
  then
          mkdir ${LOGDIR}
          chown mysql:adm ${LOGDIR}
          chmod 750 ${LOGDIR}
  fi
  touch ${LOGFILE}

  # Ensure the log file is writable by the own
  chown mysql:adm ${LOGFILE}
  chmod 640 ${LOGFILE}

  /lib/init/apparmor-profile-load usr.sbin.mysqlrouter

  $start -m -b --chuid mysql -- $DAEMON_OPTIONS 2>/dev/null || retval=2

  case $? in
    0) log_end_msg 0 ;;
    1)
      log_warning_msg "already running"
      log_end_msg 0
      ;;
    2) log_end_msg 1 ;;  # failure
  esac

  return $retval
}

do_stop() {
  local retval

  log_daemon_msg "Stopping $NAME"

  if [ ! -x $DAEMON ]; then
    log_end_msg 1
    return 0
  fi

  start-stop-daemon --stop --quiet --retry=$STOP_RETRY --pidfile $PID --name $PROCNAME
  retval=$?

  case $retval in
    0|1)
      log_end_msg 0
      ;;
    2) log_end_msg 1 ;;
  esac

  return $retval
}

do_status() {
  status_of_proc -p $PID $DAEMON "$NAME"
}

case "$1" in
  start)
    do_start
    ;;
  status)
    do_status
    ;;
  restart|force-reload)
    do_stop
    sleep 1
    do_start
    ;;
  stop)
    do_stop
    sleep 1
    ;;
  *)
    >2& echo "Usage: /etc/init.d/mysqlrouter {start|stop|status|restart|force-reload}"
    exit 3
esac   
        


