#! /bin/bash

# the rpm package assumes following parameter values --------------------
if   [ "$LANG" == "ja_JP.eucJP" ] ; then    # Vine Linux etc.
	documentroot=/home/httpd/html
	cfg_lang=ja
	nkfopt=""
elif [ "$LANG" == "ja_JP.UTF-8" ] ; then    # Fedora Core etc.
	documentroot=/var/www/html
	cfg_lang=ja_utf8
	nkfopt="-w"
	echo "check PHP setting if mbstring.http_output = pass"
else
	documentroot=/var/www/html
	unset cfg_lang
fi

# if nkf not exists or too old, use default lang (en)
chknkf=`nkf --version 2>&1 | grep 'Version 2'`
if [ "$chknkf" == "" ] ; then
	unset cfg_lang
fi

## wwwroot=http://$HOSTNAME/moodle
dirroot=$documentroot/moodle
dataroot=/var/lib/moodle

dbname=moodlerpm
dbuser=mdluser
# ---------------------------------------------------------------

rerun_msg(){
	echo ""
	if [ "$cfg_lang" == "" ] ; then
		echo "Setup failed. Rerun $0."
	else
		echo "処理は失敗しました。再度 $0 を実行してください。" | nkf $nkfopt
	fi
}
input_timeout_msg(){
	echo ""
	echo "Input timed out."
}


if [ $UID -ne 0 ]; then
	if [ "$cfg_lang" == "" ] ; then
		echo "This script must be run as root."
	else
		echo "このスクリプトは root 権限で実行してください。" | nkf $nkfopt
	fi
	exit 1
fi

if [ -e $dataroot ]; then
	if [ "$cfg_lang" == "" ] ; then
		echo -n "remove $dataroot. OK? [y/N] : "
	else
		echo -n "$dataroot を削除していいですか? [y/N] : " | nkf $nkfopt
	fi
	read -t 300 ans < /dev/tty
	RETVAL=$?
	if [ $RETVAL -ne 0 ] ; then input_timeout_msg; exit 1;  fi
	if [ \( "$ans" == "y" \) -o \( "$ans" == "Y" \) ]; then
		rm -rf $dataroot
	fi
fi

if [ -e $dirroot ]; then
	if [ "$cfg_lang" == "" ] ; then
		echo -n "remove $dirroot. OK? [y/N] : "
	else
		echo -n "$dirroot を削除していいですか? [y/N] : " | nkf $nkfopt
	fi
	read -t 300 ans < /dev/tty
	RETVAL=$?
	if [ $RETVAL -ne 0 ] ; then input_timeout_msg; exit 1;  fi
	if [ \( "$ans" == "y" \) -o \( "$ans" == "Y" \) ]; then
		rm -rf $dirroot
	fi
fi


# if MySQL is not running, exit.
mysqladmin -u dummy ping > /dev/null 2>&1
RETVAL=$?
if [ $RETVAL -ne 0 ] ; then
	if [ "$cfg_lang" == "" ] ; then
		echo "MySQL is not running.  You need to run MySQL to use moodle."
	else
		echo "MySQLが動いていません。MySQLを動かしてください。" | nkf $nkfopt
	fi
	rerun_msg
	exit 1
fi

# remember MySQL password if any
mysql -u root --exec="" > /dev/null 2>&1
RETVAL=$?
if [ $RETVAL -ne 0 ] ; then
	if [ "$cfg_lang" == "" ] ; then
		echo -n "Input MySQL root password : "
	else
		echo -n "MySQL の root のパスワードを入力してください : " | nkf $nkfopt
	fi
	read -s pas < /dev/tty
	echo ""
fi
# check if the password is right
mysql -u root --exec="" --password="$pas" > /dev/null 2>&1
RETVAL=$?
if [ $RETVAL -ne 0 ] ; then
	if [ "$cfg_lang" == "" ] ; then
		echo "MySQL root password is wrong."
	else
		echo "MySQL root パスワードが違います。" | nkf $nkfopt
	fi
	exit 3
fi

# if database named $dbname exists, drop it.
mysqlshow -u root --password="$pas" $dbname > /dev/null 2>&1
RETVAL=$?
if [ $RETVAL -eq 0 ] ; then
	if [ "$cfg_lang" == "" ] ; then
		echo -n "Erase the moodle data from DB. OK? [y/N] : "
	else
		echo -n "DB から moodle のデータを消去していいですか? [y/N] : " | nkf $nkfopt
	fi
	read -t 300 ans < /dev/tty
	RETVAL=$?
	if [ $RETVAL -ne 0 ] ; then input_timeout_msg;  exit 1;  fi
	if [ \( "$ans" == "y" \) -o \( "$ans" == "Y" \) ]; then
		SQL_POSTUN="revoke ALL ON $dbname.* FROM $dbuser@localhost; drop database $dbname;"
		mysql -u root --exec="$SQL_POSTUN" --password="$pas" > /dev/null 2>&1
		RETVAL=$?
		mysqladmin -u root  --password="$pas" reload 
		if [ "$cfg_lang" == "" ] ; then
			if [ $RETVAL -eq 0 ] ; then
				echo "DB unset completed."
			else
				echo "DB unset error!"
			fi
		else
			if [ $RETVAL -eq 0 ] ; then
				echo "DBからの消去が完了しました。" | nkf $nkfopt
			else
				echo "DBからの消去でエラーが発生しました。" | nkf $nkfopt
			fi
		fi
	else
		echo "Abort."
		exit 1
	fi
else
	echo "DB '$dbname' is not found.  Nothing is done for DB."
fi


