#!/bin/bash
#
# File:        mysql_moodle
# Description: Program to manage moodle database in mysql-server
# Author:      Fco Javier Lopez Perez <lopez_frajav@gva.es>
# Copyright:   (C) 2008 Fco Javier Lopez Perez <lopez_frajav@gva.es>
# 
# 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 St, Fifth Floor, Boston MA 02110-1301 USA

set -e

MYSQL_DATABASE_NAME="moodle"
MYSQL_DBUSER_NAME="moodle"
#MYSQL_DBUSER_PASSWD="$(pwgen --capitalize --numerals 7 1)"
MYSQL_DBUSER_PASSWD="moodle"
MYSQL_PASSWD_FILE="/root/.my.cnf"
MYSQL_PASSWD=""
CONFIG_DIR="/etc/moodle/"
CONFIG_PHP="$CONFIG_DIR/config.php"
CONFIG_PHP_OLD="$CONFIG_PHP.old"
#CONFIG_PHP_MYSQL="$CONFIG_DIR/$CONFIG_PHP"
#CONFIG_PHP_MYSQL_OLD="$CONFIG_MYSQL_PHP.old"

do_show_usage(){

	CMD="$(basename "$0")"
	echo -e "Usage: $CMD {create|delete|check_database|check_tables|sync_passwd}\n\n\
$CMD create : Creates moodle database in mysql server\n\
$CMD delete : Deletes moodle database in mysql server\n\
$CMD check_database : if exit status equal zero then the database exists\n\
$CMD check_tables   : if there are tables in database then exit status zero\n\
$CMD sync_passwd   : use when old config.php and new config.php does not contain equal moodle password\n"
	
	exit 1
}

do_exit_messages(){

	case "$1" in
		1)
			echo "You must be root"
			exit 1
		;;
		2)
			echo "Mysql configuration file for root missing"
			exit 1
		;;
		3)
			echo "No password in mysql configuration file"
			exit 1
		;;
		4)
			echo "Mysql is not running"
			exit 1
		;;
		5)
			echo "Moodle database already exists"
			exit 1
		;;
		6)
			echo "Moodle database created"
			exit 0
		;;
		7)
			echo "Error creating moodle database"
			exit 1
		;;
		8)
			echo "Moodle database deleted"
			exit 0
		;;
		9)
			echo "Error deleting moodle database"
			exit 1
		;;
		10)
			echo "Moodle database does not exist"
			exit 1
		;;
	esac

}

do_check_root() {

	local uid=$(id -u)

	[ $uid -ne 0 ] && return 1

	return 0

}

do_check_mycnf(){

	[ -f "$MYSQL_PASSWD_FILE" ] || return 1
	
	return 0

}

do_get_mysqlpasswd(){

	MYSQL_PASSWD=`sed -ne "/^\[mysql\]/,/^\[/s%^password[^=]\+=[[:space:]]\+%%p" "$MYSQL_PASSWD_FILE"`

 	[ -z "$MYSQL_PASSWD" ] && return 1
   
	return 0

} 

do_check_mysql_alive(){

	local ret
	ret=0
	
	 mysqladmin ping > /dev/null || ret=$?

	[ "$ret" -ne "0" ] && return 1
	
	return 0

}

do_check_moodle_database(){

	local ret
	ret=0
	echo "show databases;" | mysql | grep -q "${MYSQL_DATABASE_NAME}" >/dev/null || ret=$?

	[ $ret -ne "0" ] && return 1

	return 0

}

do_sync_moodle_passwd(){

	TMP_SQL="$(mktemp /tmp/mysql_moodle.XXX)"

	cat > "$TMP_SQL" <<EOF
GRANT ALL PRIVILEGES ON $MYSQL_DATABASE_NAME.* TO $MYSQL_DBUSER_NAME@localhost IDENTIFIED BY '$MYSQL_DBUSER_PASSWD';
GRANT ALL PRIVILEGES ON $MYSQL_DATABASE_NAME.* TO $MYSQL_DBUSER_NAME@'%' IDENTIFIED BY '$MYSQL_DBUSER_PASSWD';
FLUSH PRIVILEGES;
EOF

	local ret 
	ret=0
	
	mysql < "$TMP_SQL"  || ret=$?
	
	rm -f "$TMP_SQL"
	
	[ $ret -ne "0" ] && return 1

	return 0

}

do_create_moodle_database(){

	TMP_SQL="$(mktemp /tmp/mysql_moodle.XXX)"

	cat > "$TMP_SQL" <<EOF
CREATE DATABASE $MYSQL_DATABASE_NAME;
DELETE FROM mysql.user WHERE User='$MYSQL_DBUSER_NAME' and Host='localhost';
DELETE FROM mysql.user WHERE User='$MYSQL_DBUSER_NAME' and Host='%';
GRANT ALL PRIVILEGES ON $MYSQL_DATABASE_NAME.* TO $MYSQL_DBUSER_NAME@localhost IDENTIFIED BY '$MYSQL_DBUSER_PASSWD';
CREATE USER "$MYSQL_DBUSER_NAME" IDENTIFIED BY '$MYSQL_DBUSER_PASSWD';
FLUSH PRIVILEGES;
EOF

	local ret 
	ret=0
	
	mysql < "$TMP_SQL"  || ret=$?
	
	rm -f "$TMP_SQL"
	
	[ $ret -ne "0" ] && return 1

   return 0

}

do_check_for_tables(){

	local rc
	rc=0
	NUMTABLES="$(echo "use $MYSQL_DATABASE_NAME; show tables;" | mysql| wc -l)" || rc=$?
	if [ "$rc" -eq 0 ] ; then
		if [ "$NUMTABLES" -gt 1 ] ; then
			return 0
		else
			return 1
		fi
	else
		return 1
	fi

}

do_delete_moodle_database(){

   TMP_SQL="$(mktemp /tmp/mysql_moodle.XXX)"

   cat > "$TMP_SQL" <<EOF
DROP DATABASE $MYSQL_DATABASE_NAME;
DROP USER "$MYSQL_DBUSER_NAME";
DELETE FROM mysql.user WHERE User='$MYSQL_DBUSER_NAME' and Host='localhost';
FLUSH PRIVILEGES;
EOF

   local ret
   ret=0

   mysql < "$TMP_SQL"  || ret=$?

   rm -f "$TMP_SQL"
   
   [ $ret -ne "0" ] && return 1

   return 0

}


#Main

ACTION="$1"

do_check_root || do_exit_messages "1"

do_check_mycnf || do_exit_messages "2"

do_get_mysqlpasswd || do_exit_messages "3"

do_check_mysql_alive || do_exit_messages "4"

case "$ACTION" in
	create)
		if do_check_moodle_database ; then
			do_exit_messages "5"
		else
			if do_create_moodle_database ; then
				do_exit_messages "6"
			else
				do_exit_messages "7"
			fi
		fi
	;;
	delete)
		if do_check_moodle_database ; then
			if do_delete_moodle_database ; then
				do_exit_messages "8"
			else
				do_exit_messages "9"
			fi 
		else
			do_exit_messages "10"
		fi
	;;
	check_database)
		if do_check_moodle_database ; then
			exit 0
		else
			exit 1
		fi	
	;;
    check_tables)
        if do_check_moodle_database ; then
			if do_check_for_tables ; then
				exit 0
			else
				exit 1
			fi
        else
            exit 1
        fi
    ;;
	sync_passwd)
		if do_check_moodle_database ; then
			if do_sync_moodle_passwd ; then
				exit 0
			else
				exit 1
			fi
		fi
	;;
	*)
		do_show_usage
	;;
esac

exit 0
~               

