#! /bin/bash
#
# mysqlrouter	This shell script takes care of starting and stopping
#		the Percona MySQL Router
#
# chkconfig: 2345 66 34
# description: Percona MySQL Router
# processname: mysqlrouter
# config: /etc/mysqlrouter/mysqlrouter.conf
# pidfile: /var/run/mysqlrouter/mysqlrouter.pid
#
# Copyright (c) 2015, 2018, 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, version 2.0,
# as published by the Free Software Foundation.
#
# This program is also distributed with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation.  The authors of MySQL hereby grant you an additional
# permission to link the program and your derivative works with the
# separately licensed software that they have included with MySQL.
#
# 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, version 2.0, 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>
#

# Source function library
. /etc/rc.d/init.d/functions

# Source networking configuration
. /etc/sysconfig/network

exec=/usr/bin/mysqlrouter
prog=mysqlrouter
piddir=/var/run/mysqlrouter
pidfile=/var/run/mysqlrouter/mysqlrouter.pid
logdir=/var/log/mysqlrouter
logfile=$logdir/mysqlrouter.log
lockfile=/var/lock/subsys/$prog
datadir=/var/lib/mysqlrouter

start () {
    [ -d $piddir ] || [ -L $piddir ] || install -d -m 0750 -o mysqlrouter -g mysqlrouter ${piddir}
    [ -d $logdir ] || [ -L $logdir ] || install -d -m 0750 -o mysqlrouter -g mysqlrouter ${logdir}
    [ -e $logfile ] || [ -L $logfile ] || install /dev/null -m 0750 -o mysqlrouter -g mysqlrouter ${logfile}
    export ROUTER_PID=$pidfile
    [ -d $datadir ] || mkdir -p $datadir
    chown mysqlrouter:mysqlrouter $datadir
    daemon $exec --user=mysqlrouter -c /etc/mysqlrouter/mysqlrouter.conf >/dev/null 2>&1 &
    ret=$?
    if [ $ret -eq "0" ]; then
	action $"Starting $prog: " /bin/true
	touch /var/lock/subsys/$prog
    else
	action $"Starting $prog: " /bin/false
    fi
    return $ret
}

stop () {
    [ -f /var/lock/subsys/$prog ] || return 0
    killproc mysqlrouter >/dev/null 2>&1
    ret=$?
    if [ $ret -eq "0" ]; then
	rm -f $pidfile
	rm -f /var/lock/subsys/$prog
	action $"Stopping $prog: " /bin/true
    else
	action $"Stopping $prog: " /bin/false
    fi
}

restart ()  {
    stop
    start
}

condrestart () {
    [ -e /var/lock/subsys/$prog ] && restart || return 0
}

case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    status)
	status -p "$pidfile" $prog
	;;
    restart)
	restart
	;;
    condrestart|try-restart)
	condrestart
	;;
    reload)
	exit 3
	;;
    force-reload)
	restart
	;;
    *)
	echo $"Usage: $0 {start|stop|status|condrestart|try-restart|reload|force-reload}"
	exit 2
esac

exit $?
