#!/bin/bash

# Copyright (c) 2015, 2016, 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

# Script modified to handle the specific needs of the "wsrep" plugin
# which enables a MySQL server to act as a node in Galera Cluster.
# Modifications
# Copyright (c) 2015, 2016, Codership Oy

# Scripts to run by MySQL systemd service
#
# Needed argument: pre | post
#
# pre mode  :  try to perform sanity check for configuration, log, data
# post mode :  ping server until answer is received

get_path () {
	my_print_defaults mysqld | grep "$1" | cut -d= -f2 | tail -n 1
}

# Runs an arbitrary init sql file supplied in $1. Does not require login access
run_init_sql() {
	tmpdir=$(mktemp -d)
	chown mysql:mysql "$tmpdir"
	/usr/sbin/mysqld --user=mysql --init-file="$1" --socket="$tmpdir/mysqld.sock" --pid-file="$tmpdir/mysqld.pid" > /dev/null 2>&1
	result=$?
	rm -rf "$tmpdir"
	return $result
}

get_recover_pos () {
    wsrep_start_position_opt=""

    LOG=$(mktemp --tmpdir=/var/lib/mysql-files/ --suffix=.log)
    chown mysql.mysql $LOG
    /usr/sbin/mysqld --user=mysql --wsrep_recover --log-error=$LOG

    local rp="$(grep 'WSREP: Recovered position:' $LOG | tail -n 1)"
    if [ -z "$rp" ]; then
         local skipped="$(grep WSREP $LOG | grep 'skipping position recovery')"
         if [ -z "$skipped" ]; then
             systemd-cat -t mysql -p err echo "WSREP: Failed to recover position:"
             exit 1
         else
             systemd-cat -t mysql -p info echo "WSREP: Position recovery skipped"
         fi
    else
         local start_pos="$(echo $rp | sed 's/.*WSREP\:\ Recovered\ position://' \
              | sed 's/^[ \t]*//')"
         systemd-cat -t mysql -p info echo "WSREP: Recovered position $start_pos"
         wsrep_start_position_opt="--wsrep_start_position=$start_pos"
    fi
    rm -f $LOG

    if [ -n "$start_pos" ]; then
        systemctl set-environment MYSQLD_RECOVER_START="$wsrep_start_position_opt"
    fi
}

sanity () {
	if [ ! -r /etc/mysql/my.cnf ]; then
                systemd-cat -t mysql -p err \
		        echo "MySQL configuration not found at /etc/mysql/my.cnf. Please install one using update-alternatives."
		exit 1
	fi

	MYSQLRUN=/var/run/mysqld
	MYSQLDATA=/var/lib/mysql
	MYSQLFILES=/var/lib/mysql-files
	MYSQLKEYRING=/var/lib/mysql-keyring
	MYSQLLOG=/var/log/mysql

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

	if [ ! -d ${MYSQLFILES} -a ! -L ${MYSQLFILES} ];
	then
		mkdir ${MYSQLFILES}
		chown mysql:mysql ${MYSQLFILES}
		chmod 770 ${MYSQLFILES}
	fi

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

	if [ ! "$(ls -A ${MYSQLDATA}/mysql)" ];
	then
		SQL=$(mktemp -u ${MYSQLFILES}/XXXXXXXXXX)
		install /dev/null -m0600 -omysql -gmysql "${SQL}"
		cat << EOF > ${SQL}
USE mysql;
INSTALL PLUGIN auth_socket SONAME 'auth_socket.so';
ALTER USER 'root'@'localhost' IDENTIFIED WITH 'auth_socket';
SHUTDOWN;
EOF
		/usr/sbin/mysqld --initialize-insecure --user=mysql > /dev/null
		run_init_sql "$SQL"
		rm -f "$SQL"
        else
                get_recover_pos
	fi

	if [ -x /usr/bin/mysql_ssl_rsa_setup -a ! -e "${MYSQLDATA}/server-key.pem" ];
	then
		/usr/bin/mysql_ssl_rsa_setup --datadir="${MYSQLDATA}" --uid=mysql >/dev/null 2>&1
	fi

	if [ ! -d ${MYSQLLOG} -a ! -L ${MYSQLLOG} ];
	then
		mkdir ${MYSQLLOG}
		chown mysql:adm ${MYSQLLOG}
		chmod 750 ${MYSQLLOG}
		install /dev/null -m0640 -omysql -gadm ${MYSQLLOG}/error.log
	fi

	@DEB_INIT_APPARMOR@
}

clear_recover_pos () {
    systemctl unset-environment MYSQLD_RECOVER_START
}

pinger () {
    server_up=false
    for i in $(seq 1 30); do
        sleep 1
        if mysqladmin ping >/dev/null 2>&1; then
            server_up=true
            break
        fi
    done
    clear_recover_pos
    if [ ! $server_up ]; then
        systemd-cat -t mysql -p err \
            echo "MySQL server not started"
        exit 1
    fi
}

case $1 in
	"pre")  sanity ;;
        "post") pinger ;;
esac
