#!/bin/sh

MYSQL_TYPE=`uname -s`

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

if [ -f /etc/init.d/mysql ]; then
	MYSQLD=/etc/init.d/mysql
elif [ -f /etc/init.d/mysqld ]; then    
	MYSQLD=/etc/init.d/mysqld
elif [ -f /usr/local/etc/rc.d/mysql-server.sh ]; then
	MYSQLD=/usr/local/etc/rc.d/mysql-server.sh
elif [ -f /usr/local/etc/rc.d/mysql-server ]; then
	MYSQLD=/usr/local/etc/rc.d/mysql-server
elif [ -f /etc/rc.d/mysql ]; then
	MYSQLD=/etc/rc.d/mysql
elif [ -f /usr/local/psa/rc.d/mysqld ]; then
	MYSQLD=/usr/local/psa/rc.d/mysqld
elif [ -f /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
	MYSQL_STATUS="$MYSQLD status"
	MD5="md5"
elif [ "$MYSQL_TYPE" = "Darwin" ]; then
	MYSQL_DIR=/usr/local/mysql
	MYSQL_VAR_D=/usr/local/mysql/data
	MYSQL_STATUS="[ -a /tmp/mysql.sock ]"
	MD5="md5"
else
	MYSQL_DIR=/usr
	MYSQL_VAR_D=/var/lib/mysql
	MYSQL_STATUS="$MYSQLD status"
	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
		MYSQL_STATUS="$MYSQLD status"
	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
#if [ -x /opt/mysql-4.1/bin/mysqld_safe ]; then
#  SAFE_MYSQL=/opt/mysql-4.1/bin/mysqld_safe
#fi
MYSQL_CLIENT=$MYSQL_BIN_D/mysql
MYSQL_ADMIN_LOGIN=root
MYSQL_ADMIN_PASSWORD=
MYSQL_NEED_RESTART="false"

$MYSQL_STATUS || $MYSQLD start

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
  # try connect as root with empty password
   #cho_try "connect as root with empty password"
   echo "" | $MYSQL_CLIENT -uroot --password= mysql > /dev/null 2>&1
   myOK=$?
   if [ ! $myOK -eq 0 ]; then
        $MYSQLD stop
        sleep 5
        $SAFE_MYSQL --datadir="${MYSQL_VAR_D}" --skip-grant-tables > /dev/null 2>&1 &
        echo "Waiting for mysql to start"

		if [ -x ${MYSQL_BIN_D}/mysqladmin ]; then
			for  i in 0 1 2 3 4 5; do
				sleep $i
				echo -n '.'
				${MYSQL_BIN_D}/mysqladmin ping > /dev/null 2>&1 && break;
			done
			echo
			if ! ${MYSQL_BIN_D}/mysqladmin ping; then
				echo "Cannot connect to mysql server"
				echo "Run sb_config manually"
				exit 0
			fi
		else
			echo "mysqladmin utility not found"
			sleep 10
		fi

        MYSQL_NEED_RESTART="true"
   fi
fi

/usr/local/bin/sb_config --force_update_wscfg --force_update_db \
						 --merge_config \
						 --db_admin_name=$MYSQL_ADMIN_LOGIN \
						 --db_admin_passwd=$MYSQL_ADMIN_PASSWORD \
						 --sb_db_user_passwd=`date| $MD5 |cut -d' '  -f1`;
sleep 2

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

exit 0
