#!/bin/bash
# delete_example script
#
# Stops the database and eliminates the desired Example Database
#
# This Vertica Scripts is bound to the Terms and Conditions referred to in the Vertica Script EULA Agreement (http://www.vertica.com/termsofuse)
#
echo "WARNING - WARNING - WARNING - WARNING"
echo "-------------------------------------"
echo "This operation is irreversible"
echo
echo "You are about to delete the $1 database"
echo
read -p "Do you wish to continue ? (YES) :" answer
if [ "$answer" != "YES" ]
then
  exit 1
fi
#
check_file_exists(){
  local message
  if [ -z "$1" ]
  then
    echo "-Parameter #1 is zero length.-"  # Or no parameter passed.
    exit 1 
  fi
  if [ -z "$2" ]
  then
    message="File"
  else
    message=$2
  fi

  if [ ! -f $1 ]
  then
    echo "$message $1 does not exist"
    exit 1
  fi
}

check_file_executable(){
  local message
  if [ -z "$1" ]
  then
    echo "-Parameter #1 is zero length.-"  # Or no parameter passed.
    exit 1
  fi
  if [ -z "$2" ]
  then
    message="File"
  else
    message=$2
  fi

  if [ ! -x $1 ]
  then
    echo "$message $1 does not have execute permission"
    exit 1
  fi
}

check_dir_exists(){
  local message
  if [ -z "$1" ]
  then
    echo "-Parameter #1 is zero length.-"  # Or no parameter passed.
    exit 1
  fi
  if [ -z "$2" ]
  then
    message="Directory"
  else
    message=$2
  fi

  if [ ! -d $1 ]
  then
    echo "$message $1 does not exist"
    exit 1
  fi
}

usage(){
  echo "Script to delete vertica example database"
  echo "Usage: $progname schema_example"
  echo "where schema_example is the following:"
  echo "  [ $examples ]"
}

ARGS=1
progname=`basename $0`
examples='VMart'
if [ $# -ne "$ARGS" ]
then
  usage
  exit 1
fi

echo "$progname $1"
vertica_install_dir=/opt/vertica
vertica_example_dir=$vertica_install_dir/examples
vertica_example_log=$vertica_example_dir/log/ExampleDelete.txt
example=''
for i in $examples; do if [ `echo $i|awk '{print tolower($0)}'` = `echo $1|awk '{print tolower($0)}'` ]; then  example=$i;example_lower=`echo $i|awk '{print tolower($0)}'`; fi; done
if [ -z "$example" ]
then
  echo "Wrong parameter was provided"
  echo "Allowed parameters:" $examples
  usage
  exit 1
fi

# Check if we have permisions and all scripts to do the job
check_file_exists ${vertica_install_dir}/bin/adminTools "adminTools file"
check_file_executable ${vertica_install_dir}/bin/adminTools "adminTools file"

# Check if database exists
created=''
active_dbs=`${vertica_install_dir}/bin/adminTools -t show_active_db`
for i in $active_dbs; do if [ "$i" = "$example" ]; then  created=$i; fi; done
if [ -z "$created" ]
then
  echo "Database $example does not exist"
  exit 1
fi

# Create the log directory if necessary
if [ ! -d $vertica_example_dir/log ]
then
   mkdir -p $vertica_example_dir/log
fi


# Stop database
# echo ${vertica_install_dir}/bin/adminTools -t stop_db -d $example
echo "Stopping the $example example database"
${vertica_install_dir}/bin/adminTools -t stop_db -d $example >> ${vertica_example_log}

# Stop database
#echo ${vertica_install_dir}/bin/adminTools -t drop_db -d $example
echo "Dropping the database"
${vertica_install_dir}/bin/adminTools -t drop_db -d $example >> ${vertica_example_log}

# Check if database still exists
active_dbs=`${vertica_install_dir}/bin/adminTools -t show_active_db`
for i in $active_dbs; do if [ "$i" = "$example" ]; then  echo "Database $example still exists"; exit 1; fi; done

#Finish
# echo "$progname $1 finished."
echo "Example deleted"
echo
echo "Log is located in $vertica_example_log"
