# Patch rc.mysqlrouter start/stop script to make it working by emulating LSB
# Copyright (C) 2017 Georgi D. Sotirov <gdsotirov@dir.bg>
#
# 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; either version 2
# of the License, or (at your option) any later version.
#
# 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 Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# $Id: rc.mysqlrouter.diff,v 1.5 2017/02/24 14:11:55 gsotirov Exp $
#
33c33,167
< . /lib/lsb/init-functions
---
> # Emulate LSB (Linux Standard Base) commands and functions
> function log_daemon_msg() { echo -n $1; }
> function log_success_msg() { echo $1; }
> function log_failure_msg() { echo $1; }
> function log_end_msg() { if [ $1 -eq 0 ]; then echo " Done."; else echo " Filure."; fi }
> function log_warning_msg() { echo -e "\nWarning: $1"; }
> function start_stop_daemon() {
>   local retval=0
>   local START_STOP="" PID_FILE="" CMD_LINE="" PROC_NAME="" USER_NAME=""
>   local RTRY_CNT=1 FG_TEST=0 FG_QUIET=0 SH_BKG=1 FG_MAKE_PID=1
>   local CMD_OPTS=""
>   # see http://man7.org/linux/man-pages/man8/start-stop-daemon.8.html
>   while [[ $# -gt 1 ]]; do
>     case "$1" in
>       # commands
>       -S|--start       ) START_STOP="start"    ;;
>       -K|--stop        ) START_STOP="stop"     ;;
>       # matching options
>       -p|--pidfile     ) PID_FILE="$2" ; shift ;;
>       -x|--exec        ) CMD_LINE="$2" ; shift ;;
>       -n|--name        ) PROC_NAME="$2"; shift ;; # not implemented
>       -u|--user        ) USER_NAME="$2"; shift ;; # not implemented
>       # generic options
>       -R=*|--retry=*   ) RTRY_CNT="${1#*=}";   ;; # not implemented
>       -t|--test        ) FG_TEST=1             ;;
>       -q|--quiet       ) FG_QUIET=1            ;;
>       -c|--chuid       ) USER_NAME="$2"; shift ;;
>       -b|--background  ) SH_BKG=1              ;;
>       -m|--make-pidfile) FG_MAKE_PID=1         ;;
>       --               ) shift; CMD_OPTS="$@"; break ;;
>       # ignore and report all other options as errors
>       *) log_failure_msg "Error: Unregognized or unimplemented option: '$1'" ;;
>    esac
>    shift
>   done
> 
>   if [ "$START_STOP" == "start" ]; then
>     status_of_proc -p $PID_FILE $CMD_LINE "$PROC_NAME" >/dev/null 2>&1
>     # return if process already running
>     if [ $? -eq 0 ]; then
>       return 1;
>     fi
>     if [ $FG_QUIET -ne 1 ]; then
>       echo -n " ($CMD_LINE $CMD_OPTS) "
>     fi
>     if [ $FG_TEST -eq 0 ]; then
>       if [ "$USER_NAME" != "" ]; then
>         /bin/su -p -s /bin/sh $USER_NAME -c "$CMD_LINE $CMD_OPTS & echo \$! > $PID_FILE" >/dev/null 2>&1
>         sleep 1
>         kill -0 `cat $PID_FILE` >/dev/null 2>&1
>         retval=$?
>       else
>         $CMD_LINE $CMD_OPTS >/dev/null 2>&1 &
>         sleep 1
>         kill -0 $! >/dev/null 2>&1
>         retval=$?
>         if [ $retval -eq 0 ]; then
>           echo $! > $PID_FILE
>         fi
>       fi
>     else
>       retval=0
>     fi
>   elif [ "$START_STOP" == "stop" ]; then
>     if [ -e $PID_FILE ]; then
>       if [ $FG_QUIET != 1 ]; then
>         echo -n " (kill $(cat $PID_FILE)) "
>       fi
>       kill `cat $PID_FILE` >/dev/null 2>&1
>       retval=$?
>     else
>       killall $PROC_NAME >/dev/null 2>&1
>       retval=$?
>     fi
>     rm -f $PID_FILE
>   else
>     log_failure_msg "Error: No command given!"
>   fi
> 
>   if [ $retval -ne 0 ]; then
>     retval=2
>   fi
> 
>   return $retval
> }
> function status_of_proc() {
>   # see http://serverfault.com/questions/643006/what-is-status-of-proc-and-how-do-i-call-it
>   local pidfile daemon name status OPTIND;
>   pidfile=;
>   OPTIND=1;
> 
>   while getopts p: opt; do
>     case "$opt" in 
>       p) pidfile="$OPTARG" ;;
>     esac
>   done
> 
>   shift $(($OPTIND - 1));
> 
>   daemon="$1";
>   name="$2";
>   status="0";
> 
>   # see http://www.linux-tutorial.info/modules.php?name=ManPage&sec=8&manpage=pidofproc
>   #pidofproc $pidfile $daemon > /dev/null || status="$?";
>   if [ -e $pidfile ]; then
>     if [ -r $pidfile ]; then
>       proc_pid=$(cat $pidfile)
>       kill -0 $proc_pid >/dev/null 2>&1
>       status=$?
>     else
>       status="4"
>     fi
>   else
>     proc_pid=$(pidof `basename $daemon`)
>     if [ "$proc_pid" != "" ]; then
>       status="0"
>     else
>       status="1"
>     fi
>   fi
> 
>   if [ "$status" = 0 ]; then
>     log_success_msg "$name is running ($proc_pid).";
>     return 0;
>   else
>     if [ "$status" = 4 ]; then
>       log_failure_msg "could not access PID file '$pidfile' for $name";
>       return $status;
>     else
>       log_failure_msg "$name is not running.";
>       return $status;
>     fi;
>   fi
> }
49c183
<   log_daemon_msg "Starting MySQL Router"
---
>   log_daemon_msg "Starting ${NAME}:"
56c190
<   start="start-stop-daemon --start --pidfile $PID --exec $DAEMON"
---
>   start="start_stop_daemon --start --pidfile $PID --exec $DAEMON"
62c196
<           chown mysql:adm ${RUNTIMEDIR}
---
>           chown mysql:mysql ${RUNTIMEDIR}
69c203
<           chown mysql:adm ${LOGDIR}
---
>           chown mysql:mysql ${LOGDIR}
75c209
<   chown mysql:adm ${LOGFILE}
---
>   chown mysql:mysql ${LOGFILE}
78c212
<   /lib/init/apparmor-profile-load usr.sbin.mysqlrouter
---
>   #/lib/init/apparmor-profile-load usr.sbin.mysqlrouter
79a214
>   if [ $retval -eq 0 ]; then
80a216
>   fi
82c218
<   case $? in
---
>   case $retval in
97c233
<   log_daemon_msg "Stopping $NAME"
---
>   log_daemon_msg "Stopping $NAME:"
104c240
<   start-stop-daemon --stop --quiet --retry=$STOP_RETRY --pidfile $PID --name $PROCNAME
---
>   start_stop_daemon --stop --quiet --retry=$STOP_RETRY --pidfile $PID --name $PROCNAME
138c274
<     >2& echo "Usage: /etc/init.d/mysqlrouter {start|stop|status|restart|force-reload}"
---
>     >2& echo "Usage: $0 {start|stop|status|restart|force-reload}"
