#!/bin/bash

get_pcount () {
	PSCOUNT=$(ps -ef | grep "/usr/sbin/mysqld" | wc -l)
	echo "${PSCOUNT}"
}

server_stop () {
	PSCOUNT=$(get_pcount)
	COUNT=0
	while :; do
		COUNT=$(( COUNT+1 ))
		echo -n .
		if [ "${PSCOUNT}" -eq 1 ];
		then
			echo
			break
		fi
		if [ "${COUNT}" -gt 15 ];
		then
			echo
			return 1
		fi
		PSCOUNT=$(get_pcount)
		sleep 1
	done
	return 0
}

case "$1" in
	install)

	if [ -z "$2" ];
	then

		set -e

		if [ -x "/etc/init.d/mysql" ];
		then
			invoke-rc.d mysql stop || exit $?
			server_stop
		fi

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

		if ! getent group mysql >/dev/null;
		then
			addgroup --system mysql >/dev/null
		fi

		if ! getent passwd mysql >/dev/null;
		then
			adduser --ingroup mysql --system --disabled-login --no-create-home --home ${MYSQLDATA} --shell /bin/false --gecos "MySQL Server" mysql >/dev/null
		fi

		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 [ ! -d ${MYSQLLOG} -a ! -L ${MYSQLLOG} ];
		then
			mkdir ${MYSQLLOG}
			chown mysql:adm ${MYSQLLOG}
			chmod 750 ${MYSQLLOG}
			touch ${MYSQLLOG}/error.log
			chmod 640 ${MYSQLLOG}/error.log
			chown mysql:adm ${MYSQLLOG}/error.log
		fi

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

		# we do this because we don't provide our own my.cnf
		# but use the one from system mysql-common package
		# so we need to disable options that don't work for 5.7
		MYSQLCONFIG=/etc/mysql/my.cnf
		if [ -f ${MYSQLCONFIG} -a ! -L ${MYSQLCONFIG} ];
		then
			cp -f ${MYSQLCONFIG} /etc/mysql/my.cnf.old
			sed -e '/^key_buffer[^_-]/s/^/#/g' -i ${MYSQLCONFIG}
			sed -e '/^myisam-recover/s/^/#/g' -i ${MYSQLCONFIG}
		fi

		set +e

	fi

	;;

	upgrade)

	set -e

	#DEBHELPER#
	server_stop

	set +e

	;;

	abort-upgrade)

	;;

	*)
        exit 1
        ;;
esac

exit 0
