#!/usr/bin/env sh

set -e

mysql=@MYSQL@
mysql_admin=@MYSQL_ADMIN@
mysql_admin_passwd=@MYSQL_ADMIN_PASSWD@
mysql_host=@MYSQL_HOST@
db_name=@TANGO_DB_NAME@

if test "x$mysql_admin" = "x"; then
	user_switch="";
else
	user_switch="-u$x$mysql_admin";
fi

if test "x$mysql_admin_passwd" = "x"; then
   passwd_switch="";
else
   passwd_switch="-p$mysql_admin_passwd"
fi

if test "x$mysql_host" = "x"; then
  host_switch="";
else
  host_switch="-h$mysql_host";
fi

connect="$mysql $user_switch $passwd_switch $host_switch $db_name"

if `echo quit | $connect >/dev/null 2>&1`; then
   if test "x$mysql_host" = "x"; then
      echo "The $db_name database is already defined on this host" >&2
   else
      echo "The $db_name database is already defined on $mysql_host" >&2
   fi
   echo "Please run " >&2
   echo "$connect < ./update_db.sql" >&2
   echo "manually to force a db update" >&2

   exit 1
else
  $mysql $user_switch $passwd_switch $host_switch < ./create_db.sql > /dev/null

  exit 0
fi
