Description: Since SQL installations and upgrade are handled by dbconfig and
 these scripts provided by upstream are simply processed and installed for
 dbconfig, the USE statement they include needs to be dropped. dbconfig keeps
 track of what database the user uses, but we need keep ability to run this
 script manually in maneer similiarry to upstream.
Bug: http://bugs.bacula.org/view.php?id=1901
Author: Jan Hauke Rahm <jhr@debian.org>
Author: Alexander Golovko <alexandro@ankalagon.ru>
Reviewed-by: Alexander Golovko <alexandro@ankalagon.ru>
Last-Update: 2012-07-03

diff --git a/src/cats/make_mysql_tables.in b/src/cats/make_mysql_tables.in
--- a/src/cats/make_mysql_tables.in
+++ b/src/cats/make_mysql_tables.in
@@ -10,8 +10,7 @@ bindir=@MYSQL_BINDIR@
 PATH="$bindir:$PATH"
 db_name=${db_name:-@db_name@}
 
-if mysql $* -f <<END-OF-DATA
-USE ${db_name};
+if mysql -D ${db_name} $* -f <<END-OF-DATA
 --
 -- Note, we use BLOB rather than TEXT because in MySQL,
 --  BLOBs are identical to TEXT except that BLOB is case
diff --git a/src/cats/update_mysql_tables.in b/src/cats/update_mysql_tables.in
--- a/src/cats/update_mysql_tables.in
+++ b/src/cats/update_mysql_tables.in
@@ -8,11 +8,11 @@ echo " which is needed to convert from Bacula Community version 5.0.x to 5.2.x"
 echo " "
 bindir=@MYSQL_BINDIR@
 PATH="$bindir:$PATH"
-db_name=@db_name@
+db_name=${db_name:-@db_name@}
 
-mysql $* -D ${db_name} -e "select VersionId from Version\G" >/tmp/$$
+mysql -D ${db_name} $* -e "select VersionId from Version\G" >/tmp/$$
 DBVERSION=`sed -n -e 's/^VersionId: \(.*\)$/\1/p' /tmp/$$`
-if [ $DBVERSION != 12 ] ; then
+if [ "$DBVERSION" != 12 ] ; then
    echo " "
    echo "The existing database is version $DBVERSION !!"
    echo "This script can only update an existing version 12 database to version 14."
@@ -21,9 +21,7 @@ if [ $DBVERSION != 12 ] ; then
    exit 1
 fi
 
-if mysql $* -f <<END-OF-DATA
-USE ${db_name};
-
+if mysql -D ${db_name} $* -f <<END-OF-DATA
 CREATE TABLE RestoreObject (
    RestoreObjectId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
    ObjectName BLOB NOT NULL,
diff --git a/updatedb/update_mysql_tables_10_to_11.in b/updatedb/update_mysql_tables_10_to_11.in
--- a/updatedb/update_mysql_tables_10_to_11.in
+++ b/updatedb/update_mysql_tables_10_to_11.in
@@ -10,9 +10,7 @@ bindir=@MYSQL_BINDIR@
 PATH="$bindir:$PATH"
 db_name=${db_name:-@db_name@}
 
-if mysql $* -f <<END-OF-DATA
-USE ${db_name};
-
+if mysql -D ${db_name} $* -f <<END-OF-DATA
 -- Fix bad index on Media table
 DROP INDEX inx8 ON Media;
 CREATE UNIQUE INDEX inx8 ON Media (VolumeName(128));
diff --git a/updatedb/update_mysql_tables_11_to_12.in b/updatedb/update_mysql_tables_11_to_12.in
--- a/updatedb/update_mysql_tables_11_to_12.in
+++ b/updatedb/update_mysql_tables_11_to_12.in
@@ -10,9 +10,9 @@ bindir=@MYSQL_BINDIR@
 PATH="$bindir:$PATH"
 db_name=${db_name:-@db_name@}
 
-mysql $* -D ${db_name} -e "select VersionId from Version\G" >/tmp/$$
+mysql -D ${db_name} $* -e "select VersionId from Version\G" >/tmp/$$
 DBVERSION=`sed -n -e 's/^VersionId: \(.*\)$/\1/p' /tmp/$$`
-if [ $DBVERSION != 11 ] ; then
+if [ "$DBVERSION" != 11 ] ; then
    echo " "
    echo "The existing database is version $DBVERSION !!"
    echo "This script can only update an existing version 11 database to version 12."
@@ -21,9 +21,7 @@ if [ $DBVERSION != 11 ] ; then
    exit 1
 fi
 
-if mysql $* -f <<END-OF-DATA
-USE ${db_name};
-
+if mysql -D ${db_name} $* -f <<END-OF-DATA
 ALTER TABLE JobMedia DROP Stripe ;
 ALTER TABLE JobMedia DROP Copy ;
 ALTER TABLE Job ADD COLUMN HasCache tinyint default 0 after HasBase;
diff --git a/updatedb/update_mysql_tables_12_to_14.in b/updatedb/update_mysql_tables_12_to_14.in
--- a/updatedb/update_mysql_tables_12_to_14.in
+++ b/updatedb/update_mysql_tables_12_to_14.in
@@ -8,9 +8,9 @@ echo " which is needed to convert from Bacula Community version 5.0.x to 5.2.x"
 echo " "
 bindir=@MYSQL_BINDIR@
 PATH="$bindir:$PATH"
-db_name=@db_name@
+db_name=${db_name:-@db_name@}
 
-mysql $* -D ${db_name} -e "select VersionId from Version\G" >/tmp/$$
+mysql -D ${db_name} $* -e "select VersionId from Version\G" >/tmp/$$
 DBVERSION=`sed -n -e 's/^VersionId: \(.*\)$/\1/p' /tmp/$$`
 if [ $DBVERSION != 12 ] ; then
    echo " "
@@ -21,9 +21,7 @@ if [ $DBVERSION != 12 ] ; then
    exit 1
 fi
 
-if mysql $* -f <<END-OF-DATA
-USE ${db_name};
-
+if mysql -D ${db_name} $* -f <<END-OF-DATA
 CREATE TABLE RestoreObject (
    RestoreObjectId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
    ObjectName BLOB NOT NULL,
