Fix bug when installing vim_db based on ovim.py version
[osm/openvim.git] / database_utils / install-db-server.sh
index e393395..5a9b9ab 100755 (executable)
@@ -13,6 +13,7 @@ function usage(){
     echo -e "     -h --help:  show this help"
     echo -e "     --forcedb:  reinstall vim_db DB, deleting previous database if exists and creating a new one"
     echo -e "     --no-install-packages: <deprecate> use this option to skip updating and installing the requires packages. This avoid wasting time if you are sure requires packages are present e.g. because of a previous installation"
+    echo -e "     --unistall: Delete DB, by default vim_db"
 
 
 }
@@ -22,7 +23,7 @@ function _create_db(){
     #################################################################
     #####               CREATE DATABASE                         #####
     #################################################################'
-    echo -e "\nCreating temporary file form MYSQL installation and initialization"
+    echo -e "\nCreating temporary file for MYSQL installation and initialization"
     TEMPFILE="$(mktemp -q --tmpdir "installopenvim.XXXXXX")"
     trap 'rm -f "$TEMPFILE"' EXIT
     chmod 0600 "$TEMPFILE"
@@ -34,8 +35,8 @@ function _create_db(){
             [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
             mysqladmin  --defaults-extra-file=$TEMPFILE -s drop ${DB_NAME} $DBDELETEPARAM || ! echo "Could not delete ${DB_NAME} database" || exit 1
             mysqladmin  --defaults-extra-file=$TEMPFILE -s create ${DB_NAME} || ! echo "1 Error creating ${DB_NAME} database" || exit 1
-            echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';"   | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "2 Failed while creating user vim at database"
-            echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO $DB_USER@'localhost';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "3 Failed while creating user vim at database" || exit 1
+            echo "CREATE USER $DB_USER@'localhost' IDENTIFIED BY '$DB_PASS';"   | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "2 Failed while creating user ${DB_USER}"
+            echo "GRANT ALL PRIVILEGES ON ${DB_NAME}.* TO $DB_USER@'localhost';" | mysql --defaults-extra-file=$TEMPFILE -s || ! echo "3 Failed while granting privileges to user ${DB_USER} at database ${DB_NAME}" || exit 1
             echo " Database '${DB_NAME}' created, user $DB_USER password '$DB_PASS'"
         else
             echo "Database exists. Use option '--forcedb' to force the deletion of the existing one" && exit 1
@@ -59,6 +60,16 @@ function _init_db(){
     su $SUDO_USER -c "${DIRNAME}/init_vim_db.sh -u $DB_USER -p $DB_PASS -d ${DB_NAME}" || ! echo "Failed while initializing database" || exit 1
 }
 
+function _uninstall_db(){
+echo '
+    #################################################################
+    #####        DELETE DATABASE                                #####
+    #################################################################'
+    DBDELETEPARAM=""
+    [[ -n $QUIET_MODE ]] && DBDELETEPARAM="-f"
+    mysqladmin  --defaults-extra-file=$TEMPFILE -s drop ${DB_NAME} $DBDELETEPARAM || ! echo "Could not delete ${DB_NAME} database" || exit 1
+
+}
 function db_exists() {
     RESULT=`mysqlshow --defaults-extra-file="$2" | grep -v Wildcard | grep -o $1`
     if [ "$RESULT" == "$1" ]; then
@@ -77,6 +88,7 @@ DBPASSWD_PARAM=""
 QUIET_MODE=""
 FORCEDB=""
 NO_PACKAGES=""
+UNINSTALL=""
 while getopts ":U:P:d:u:p:hiq-:" o; do
     case "${o}" in
         U)
@@ -107,6 +119,7 @@ while getopts ":U:P:d:u:p:hiq-:" o; do
             [ "${OPTARG}" == "forcedb" ] && FORCEDB="y" && continue
             [ "${OPTARG}" == "quiet" ] && export QUIET_MODE=yes && export DEBIAN_FRONTEND=noninteractive && continue
             [ "${OPTARG}" == "no-install-packages" ] && export NO_PACKAGES=yes && continue
+            [ "${OPTARG}" == "uninstall" ] &&  UNINSTALL="y" && continue
             echo -e "Invalid option: '--$OPTARG'\nTry $0 --help for more information" >&2
             exit 1
             ;;
@@ -158,9 +171,17 @@ then
     done
 fi
 
+if [[ ! -z "$UNINSTALL" ]]
+then
+    _uninstall_db
+    exit
+fi
+
+
 if [[ -z "$NO_PACKAGES" ]]
 then
     _create_db
     _init_db
 fi
 
+