#!/bin/bash

EXPECTED_ARGS=1
E_BADARGS=65
MYSQL=`which mysql`
CONFDIR=/var/opi/etc

if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Usage: $0 config"
  exit $E_BADARGS
fi

if [ -e "$CONFDIR/$1.conf" ]
then
  . $CONFDIR/$1.conf
else
  echo "Missing configuration"
  exit $E_BADARGS
fi

if [ -z ${_DBC_DBNAME_+x} ]
then
  echo "Missing dbname"
  exit $E_BADARGS
fi

if [ -z ${_DBC_DBUSER_+x} ]
then
  echo "Missing dbuser"
  exit $E_BADARGS
fi

Q0="GRANT USAGE ON *.* TO '$_DBC_DBUSER_'@'localhost';" 
Q1="DROP DATABASE IF EXISTS $_DBC_DBNAME_;"
Q2="DROP USER '$_DBC_DBUSER_'@'localhost';"
Q3="FLUSH PRIVILEGES;"
SQL="${Q0}${Q1}${Q2}${Q3}"
 
if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Usage: $0 dbname"
  exit $E_BADARGS
fi
 
$MYSQL -uroot -e "$SQL"
