-#check and ask for database user password
-DBUSER_="-u$DBUSER"
-DBPASS_=""
-[ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
-DBHOST_="-h$DBHOST"
-DBPORT_="-P$DBPORT"
-while ! echo ";" | mysql $DBHOST_ $DBPORT_ $DBUSER_ $DBPASS_ >/dev/null 2>&1
-do
- [ -n "$logintry" ] && echo -e "\nInvalid database credentials!!!. Try again (Ctrl+c to abort)"
- [ -z "$logintry" ] && echo -e "\nProvide database credentials"
-# read -e -p "mysql database name($DBNAME): " KK
-# [ -n "$KK" ] && DBNAME="$KK"
- read -e -p "mysql user($DBUSER): " KK
- [ -n "$KK" ] && DBUSER="$KK" && DBUSER_="-u$DBUSER"
- read -e -s -p "mysql password: " DBPASS
- [ -n "$DBPASS" ] && DBPASS_="-p$DBPASS"
- [ -z "$DBPASS" ] && DBPASS_=""
- logintry="yes"
+DB_VERSION=$1
+
+if [ -n "$DB_VERSION" ] ; then
+ # check it is a number and an allowed one
+ [ "$DB_VERSION" -eq "$DB_VERSION" ] 2>/dev/null ||
+ ! echo "parameter 'version' requires a integer value" >&2 || exit 1
+fi
+
+# Creating temporary file
+TEMPFILE="$(mktemp -q --tmpdir "initdb.XXXXXX")"
+trap 'rm -f "$TEMPFILE"' EXIT
+chmod 0600 "$TEMPFILE"
+DEF_EXTRA_FILE_PARAM="--defaults-extra-file=$TEMPFILE"
+echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE"
+
+if [ -n "${CREATEDB}" ] ; then
+ FIRST_TRY="yes"
+ while ! DB_ERROR=`mysqladmin "$DEF_EXTRA_FILE_PARAM" -s status 2>&1 >/dev/null` ; do
+ # if password is not provided, try silently with $DEFAULT_DBPASS before exit or prompt for credentials
+ [[ -n "$FIRST_TRY" ]] && [[ -z "$DBPASS" ]] && DBPASS="$DEFAULT_DBPASS" &&
+ echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE" &&
+ continue
+ echo "$DB_ERROR"
+ [[ -n "$QUIET_MODE" ]] && echo -e "Invalid admin database credentials!!!" >&2 && exit 1
+ echo -e "Provide database credentials (Ctrl+c to abort):"
+ read -e -p " mysql user($DBUSER): " KK
+ [ -n "$KK" ] && DBUSER="$KK"
+ read -e -s -p " mysql password: " DBPASS
+ echo -e "[client]\n user='${DBUSER}'\n password='$DBPASS'\n host='$DBHOST'\n port='$DBPORT'" > "$TEMPFILE"
+ FIRST_TRY=""