#! /bin/bash

#
### BEGIN INIT INFO
# Provides: mysqld
# Required-Start: $remote_fs $syslog networking procps
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 5
# Default-Stop: S 1 4
# Short-Description: Mysql
# Description: Start all mysqld services
### END INIT INFO

# This exists to prevent Jenkins-based builds from breaking but don't do anything otherwise
test -e /root/no-sql-start && exit 0

# Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
# This file is public domain and comes with NO WARRANTY of any kind

# MySQL daemon start/stop script.

# Usually this is put in /etc/init.d (at least on machines SYSV R4 based
# systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
# When this is done the mysql server will be started when the machine is
# started and shut down when the systems goes down.

# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 90 20
# description: A very fast and reliable SQL database engine.
# If you install MySQL on some other places than /usr/local/dh/mysql/base, then you
# have to do one of the following things for this script to work:
#
# - Run this script from within the MySQL installation directory
# - Create a /etc/my.cnf file with the following information:
#   [mysqld]
#   basedir=<path-to-mysql-installation-directory>
# - Add the above to any other configuration file (for example ~/.my.ini)
#   and copy my_print_defaults to /usr/bin
# - Add the path to the mysql-installation-directory to the basedir variable
#   below.
#
# If you want to affect other MySQL variables, you should make your changes
# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.

PATH=/sbin:/usr/sbin:/bin:/usr/bin

export PATH

mode=$1

#set some variables
. /dh/mysql/etc/envrc

# if the user wants a specific service(s), give it them!
if [ $2 ];
then
  SERVERS="$2 $3 $4 $5 $6 $7 $8 $9"
fi

# this is for mysqladmin -r -i 5
if [[ $1 == "watch" ]] ; 
then
   SERVERS="$2"
fi

# go
for S in $SERVERS
do
 mysqld_bin=/dh/mysql/base/bin/mysqld_safe
 pid_file=/tmp/mysql.$S.pid

# create symlink to binary
if ! `test -L /dh/mysql/$S/mysqld--$S`; 
then
	ln -s /dh/mysql/base/libexec/mysqld /dh/mysql/$S/mysqld--$S
fi

# create error log to work around https://bugs.mysql.com/bug.php?id=84427
if test ! -e /dh/mysql/logs/mysql.$S.err ; 
then
	echo creating error log /dh/mysql/logs/mysql.$S.err
	touch /dh/mysql/logs/mysql.$S.err
	chown 11139.1000 /dh/mysql/logs/mysql.$S.err
	chmod 660 /dh/mysql/logs/mysql.$S.err
fi


case "$mode" in
	'start')
		if test -x $mysqld_bin
		then
			echo " Starting mysqld@$S"
	    	    	/bin/systemctl start mysqld\@$S
	    	else
		    	echo " Can't execute $mysqld_bin"
		    	exit 1
	    	fi
		;;

	'stop')
		if test -s "$pid_file" 
		then
			mysqld_pid=`cat $pid_file`
			echo " Stopping mysqld@$S"
			/bin/systemctl stop mysqld\@$S
			# mysqld should remove the pid_file when it exits, so wait for it.
			sleep 1
			    
			while [ -s $pid_file -a "$flags" != aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ]
			do
				[ -z "$flags" ] && echo "Wait for mysqld to exit\c" || echo ".\c"
				flags=a$flags
				sleep 1
			done
		
			if [ -s $pid_file ] 
			then 
				echo " gave up waiting!"
			elif [ -n "$flags" ] 
			then 
				echo " done"
			fi

	 	else
			echo "No mysqld pid file found. Looked for $pid_file."
		fi
		;;

	'restart')
	  	echo -------- ATTENTION --------
	  	echo Do not use restart.  Use stop and verify that it has exited, then start.  Afterwards do a stat to make sure it responds to queries.
	  	;;
    
	'reallyrestart')
	  	# Stop the service and regardless of whether it was
	  	# running or not, start it again.
	  	$0 stop $S
	  	$0 start $S
	  	;;

	'nothing')
		# Do nothing (used by postinst)
		echo Doing nothing.
		;;
   
	'faststart')
	  	# Quickly start all services
		echo systemctl start mysqld@$S
	  	;;
    
	'faststop')
	  	# Quickly stop all services
	  	echo systemctl stop mysqld@$S
	  	;;

	'status')
		echo -n "$S: "
		systemctl status mysqld@$S
		;;
	'stat')
	  	# http://www.mysql.com/doc/P/r/Privileges_provided.html
	  	echo -n "$S: "
	  	mysqladmin -u root -p`grep 'backup pass' /dh/mysql/$S/my.cnf | awk '{ print $5 }'` -S /dh/mysql/$S/mysql.$S.sock stat
	  	;;
    
	'ext')
	  	# http://www.mysql.com/doc/P/r/Privileges_provided.html
	  	echo mysqladmin $S ext
	  	mysqladmin -u local_admin -S /dh/mysql/$S/mysql.$S.sock ext
	  
		if [ `echo $SERVERS |wc -w ` != 1 ]; then
			echo You should really only do one mysqld_instance at a time.
			echo Doing all or several taxes the server quite a bit.
	  	fi
	  	;;
    
	'proc')
	  	# http://www.mysql.com/doc/S/H/SHOW_PROCESSLIST.html
	  	# http://www.mysql.com/doc/P/r/Privileges_provided.html
	  	echo mysqladmin $S proc
	  	mysqladmin -u local_admin -S /dh/mysql/$S/mysql.$S.sock proc
	  
		if [ `echo $SERVERS |wc -w ` != 1 ]; then
			echo You should really only do one mysqld_instance at a time.
			echo Doing all or several taxes the server quite a bit.
	  	fi
	  	;;

	'kill')
	  	if [ `echo $SERVERS |wc -w ` == 1 ]; then
			echo mysqladmin $S kill $3
			mysqladmin -u local_admin -S /dh/mysql/$S/mysql.$S.sock kill $3
	  	else
			echo There is never any reason to kill across mysqld_instances.
			echo Please choose only one mysqld_instance.
			echo Thank you.
	  	fi
	  	;;
    
	'var')
	  	# echo see http://www.mysql.com/doc/P/r/Privileges_provided.html
	  	echo mysqladmin $S var
	  	mysqladmin -u local_admin -S /dh/mysql/$S/mysql.$S.sock var
	  	;;

	'refresh')
		echo mysqladmin $S refresh
		mysqladmin -u local_admin -S /dh/mysql/$S/mysql.$S.sock refresh
		;;

	'watch')
		echo mysqladmin $S watch
		mysqladmin -r -i 5 -u local_admin -S /dh/mysql/$S/mysql.$S.sock ext
		;;

	'flush-hosts')
		echo mysqladmin $S flush-hosts
		mysqladmin -u local_admin -S /dh/mysql/$S/mysql.$S.sock flush-hosts
		;;

	'flush-logs')
		echo mysql $S flush logs
		mysql -u local_admin -S /dh/mysql/$S/mysql.$S.sock -e 'flush logs;'
		;;

	'flush-general-logs')
		echo mysql $S flush general logs
		mysql -u local_admin -S /dh/mysql/$S/mysql.$S.sock -e 'flush local general logs;'
		;;

	'flush-error-logs')
		echo mysql $S flush error logs
		mysql -u local_admin -S /dh/mysql/$S/mysql.$S.sock -e 'flush local error logs;'
		;;

	'flush-slow-logs')
		echo mysql $S flush slow logs
		mysql -u local_admin -S /dh/mysql/$S/mysql.$S.sock -e 'flush local slow logs;'
		;;

	'flush-status')
		echo mysqladmin $S flush-status
		mysqladmin -u local_admin -S /dh/mysql/$S/mysql.$S.sock flush-status
		;;

	'flush-tables')
		echo mysqladmin $S flush-tables
		mysqladmin -u local_admin -S /dh/mysql/$S/mysql.$S.sock flush-tables
		;;

	'flush-threads')
		echo mysqladmin $S flush-threads
		mysqladmin -u local_admin -S /dh/mysql/$S/mysql.$S.sock flush-threads
		;;

	'flush-privileges')
		echo mysqladmin $S flush-privileges
		mysqladmin -u local_admin -S /dh/mysql/$S/mysql.$S.sock flush-privileges
		;;
	*)
		# usage
		echo "Usage: $0 command [service]"
		echo "command: start stop restart stat status ext proc kill var refresh watch flush-hosts flush-logs flush-general-logs flush-error-logs flush-slow-logs flush-status flush-tables flush-threads flush-privileges"
		exit 1
		;;
esac

done
