#!/bin/sh

export LANG=C
umask 0022 || :

if [ -f /tmp/SB_CONFIGURE_VERBOSE ] || [ ! -z "$SB_CONFIGURE_VERBOSE" ]; then
	set -vx
fi
MYSQL_TYPE=`uname -s`
MYSQL_HOME=$1

[ -z "$MYSQL_HOME" ] && exit 1

# detect MySQL startup script
MYSQLD=/etc/rc.d/init.d/mysqld

if [ -x /etc/init.d/mysql ]; then
	MYSQLD=/etc/init.d/mysql
elif [ -x /etc/init.d/mysqld ]; then
	MYSQLD=/etc/init.d/mysqld
elif [ -x /usr/local/etc/rc.d/mysql-server.sh ]; then
	MYSQLD=/usr/local/etc/rc.d/mysql-server.sh
elif [ -x /usr/local/etc/rc.d/mysql-server ]; then
	MYSQLD=/usr/local/etc/rc.d/mysql-server
elif [ -x /etc/rc.d/mysql ]; then
	MYSQLD=/etc/rc.d/mysql
elif [ -x /usr/local/psa/rc.d/mysqld ]; then
	MYSQLD=/usr/local/psa/rc.d/mysqld
elif [ -x /Library/StartupItems/MySQLCOM/MySQLCOM ]; then
	MYSQLD=/Library/StartupItems/MySQLCOM/MySQLCOM
fi

# detect platform specific vars
if [ "$MYSQL_TYPE" = "FreeBSD" ]; then
	MYSQL_DIR=/usr/local
	MYSQL_VAR_D=/var/db/mysql
	MD5="md5"
elif [ "$MYSQL_TYPE" = "Darwin" ]; then
	MYSQL_DIR=/usr/local/mysql
	MYSQL_VAR_D=/usr/local/mysql/data
	MD5="md5"
else
	MYSQL_DIR=/usr
	MYSQL_VAR_D=/var/lib/mysql
	MD5="md5sum"
fi

if [ "$MYSQL_TYPE" = "FreeBSD" -o "$MYSQL_TYPE" = "Darwin" ]; then
	if [ -f /etc/psa/.psa.shadow ]; then	
		MYSQL_DIR=/usr/local/psa/mysql
		MYSQL_VAR_D=/usr/local/psa/mysql/var
	fi
fi

MYSQL_BIN_D=${MYSQL_DIR}/bin

SAFE_MYSQL=$MYSQL_BIN_D/mysqld_safe
if [ -x $MYSQL_BIN_D/safe_mysqld ]; then
	SAFE_MYSQL=$MYSQL_BIN_D/safe_mysqld
fi

MYSQL_CLIENT=$MYSQL_BIN_D/mysql
MYSQL_ADMIN_LOGIN=root
MYSQL_ADMIN_PASSWORD=
MYSQL_NEED_RESTART="false"

MYSQL_PING="perl ${MYSQL_HOME}/utils/mysql_ping.pl"
MYSQL_CLIENT=$MYSQL_PING

# Try to connect to mysql server with the specified credintials
mysql_try_connect() {
	local user=$1
	local password=$2
	mysql_ec=`$MYSQL_CLIENT "${user}" "${password}" 2> /dev/null`
	[ "$mysql_ec" -gt 0 ] && return 1
	return 0
}

# Check if mysql is alive
mysql_ping() {
	mysql_ec=`$MYSQL_PING 2> /dev/null`
	[ "$mysql_ec" -eq 0 -o "$mysql_ec" -eq 1045 -o "$mysql_ec" -eq 1044 ] && return 0
	return 1
}

# Wait untill mysql server is online (similar to mysqladmin ping)
mysql_wait_up() {
	local count=1
	while true; do
		mysql_ping ;
		[ $? -eq 0 ] && break;
		echo -n '.'
		sleep $count
		# Is (( )) POSIX-compliant? 
		count=$(($count+1))
		[ "$count" -gt "$1" ] && break; 
	done

	echo 
	mysql_ping 
	[ $? -eq 0 ] && return 0;
	return 1;
}

mysql_wait_down() {
	local count=1
	while true; do
		mysql_ping ;
		[ $? -gt 0 ] && break;
		echo -n '.'
		sleep $count
		count=$(($count+1))
		[ "$count" -gt "$1" ] && break; 
	done
	echo 
	mysql_ping ;
	[ $? -gt 0 ] && return 0
	return 1;
}

if ! mysql_ping; then
	echo "Waiting for mysql to start"
	$MYSQLD start
 	mysql_wait_up 6 || exit 1;
fi

if [ -f /etc/psa/.psa.shadow ]; then
	MYSQL_ADMIN_LOGIN=admin
	MYSQL_ADMIN_PASSWORD=`cat /etc/psa/.psa.shadow`
elif [ -f /root/.my.cnf ]; then
	MYSQL_ADMIN_PASSWORD=`cat /root/.my.cnf | grep password | awk -F= '{print $2;}'`
else
	MYSQL_ADMIN_LOGIN=root
	MYSQL_ADMIN_PASSWORD=""

	if ! mysql_try_connect ${MYSQL_ADMIN_LOGIN} ${MYSQL_ADMIN_PASSWORD} ; then
		$MYSQLD stop
		mysql_wait_down 6;

		$SAFE_MYSQL --datadir="${MYSQL_VAR_D}" --skip-grant-tables > /dev/null 2>&1 &
		MYSQL_NEED_RESTART="true"
		echo "Waiting for mysql to start"

		mysql_wait_up 6;
	fi
fi

if ! mysql_try_connect "$MYSQL_ADMIN_LOGIN" "$MYSQL_ADMIN_PASSWORD"; then
	echo "Cannot connect to mysql server. SiteBuilder configuration will exit now."
	echo "Make sure mysql server is installed and running."
	echo "If root password is not empty put it in /root/.my.cnf"
	echo "After finishing the installation run sb_config manually."
	exit 1
fi

export MYSQL_NEED_RESTART

MYSQL_SB_PASSWORD=$(date | $MD5 | cut -d' '  -f1)

echo "Running Sitebuilder configuration utility"
(/usr/local/bin/sb_config \
	--force_update_wscfg --force_update_db --merge_config \
	--update_modules=ImageGallery,Blogger \
	--db_admin_name=${MYSQL_ADMIN_LOGIN} \
	--db_admin_passwd=${MYSQL_ADMIN_PASSWORD} \
	--sb_db_user_passwd=${MYSQL_SB_PASSWORD})
ec=$?

sleep 2

(if [ "$MYSQL_NEED_RESTART" = "true" ]; then
	killall -TERM mysqld safe_mysqld > /dev/null 2>&1 || true
	sleep 5
	killall -9 mysqld safe_mysqld > /dev/null 2>&1 || true
	sleep 5
	$MYSQLD start
fi)

exit $ec

