#!/bin/sh

PROG=$0

die() {
    echo
    echo "$@"
    exit 1
}

BASEDIR="$(dirname "$(readlink -f "$0")")"
echo "Script Dir: $BASEDIR"
SERVER=
DB=

. "$BASEDIR/dbenv" 

[ -z "$SERVER" ] && die "--server not specified"
[ -z "$DB" ] && die "--database not specified"
[ -z "$USER" ] && die "--user not specified"
[ -z "$PASSWORD" ] && die "--password not specified"


sqlcmd -C -S "$SERVER" -U "$USER" -P "$PASSWORD" -b -e -Q "IF EXISTS(SELECT Name from sys.Databases WHERE name = '$DB') ALTER DATABASE $DB SET SINGLE_USER WITH ROLLBACK IMMEDIATE"
sqlcmd -C -S "$SERVER" -U "$USER" -P "$PASSWORD" -b -e -Q "IF EXISTS(SELECT Name from sys.Databases WHERE name = '$DB') DROP DATABASE $DB"

