#!/bin/sh

. /usr/local/mgr5/lib/pkgsh/core_pkg_funcs.sh

if [ ! -d /usr/local/mgr5/etc ]; then
	# There is no coremgr dir
	exit 1
fi
mkdir -p /usr/local/mgr5/tmp
if [ -z "${1}" ]; then
	# Empty arg"
	exit 1
fi

TMPCNF=/usr/local/mgr5/tmp/.$$.cnf
CORECNF=/usr/local/mgr5/etc/my.cnf

export PATH="${PATH}:/usr/sbin:/usr/bin:/bin:/sbin"


DLIST=$(check_connect)
if [ $? -eq 0 ]; then
	if ! check_my_cnf ; then
		#Alarm! There is empty paswwd
		create_root_passwd
		create_coremgr_passwd
	fi
fi
DLIST=$(check_core_cnf)
if [ $? -ne 0 ]; then
	DLIST=$(check_connect)
	if [ $? -eq 0 ]; then
		create_coremgr_passwd
	else
		OLDROOTPW=$(reset_root_passwd)
		create_coremgr_passwd
		restore_root_passwd ${OLDROOTPW}
	fi
fi

if echo "${DLIST}" | grep -q -E "(^| )${1}( |$)" ; then
	# Db already exist	
	# nothing do
	echo -n ""
fi


PW=$(create_db ${1} ${2})
ret=$?
echo ${PW}
rm -f ${TMPCNF}
exit $ret
