#!/bin/sh
#
# create a new domprodtest database
#
# Usage: make-daq-db [-h <dbhost>] [-u <db user>] [-p <db password>]

DBNAME=domprodtest

##########################################
#
# get arguments

HOST=
PASS=
USER=

while [ $# -gt 0 ]; do
  arg="$1"
  case "$arg" in
  -h*)
    HOST=`echo "$arg" | sed 's/^-h=*//'`
    if [ -z "$HOST" ]; then
      shift
      HOST="$1"
    fi
    ;;
  -p*)
    PASS=`echo "$arg" | sed 's/^-p=*//'`
    if [ -z "$PASS" ]; then
      shift
      PASS="$1"
    fi
    ;;
  -u*)
    USER=`echo "$arg" | sed 's/^-u=*//'`
    if [ -z "$USER" ]; then
      shift
      USER="$1"
    fi
    ;;
  *)
    echo "$0: Unknown option '$arg'" >&2
    exit 1
    ;;
  esac
  shift
done

##########################################
#
# Finish argument initialization

USERARG=
HOSTARG=
PASSARG=

# if no username was specified, assume it's 'root'
#
if [ -z "$USER" ]; then
  USER="root"
fi
USERARG="--user=$USER"

# if no password argument was specified, have MySQL prompt for one
#
if [ -z "$PASS" ]; then
  PASSARG="-p"
else
  PASSARG="--password=$PASS"
fi

# use local host name if none was supplied
#
if [ -z "$HOST" ]; then
  HOST=`hostname`
fi
HOSTARG="--host=$HOST"

##########################################
#
# find MySQL tools

for i in /usr/bin /usr/local/bin /opt/local/bin /usr/local/mysql/bin; do
  if [ -f "$i/mysql" ]; then
    MYSQL="$i/mysql"
  fi
done
if [ -z "$MYSQL" ]; then
  echo "$0: MySQL query tool 'mysql' was not found" >&2
fi

##########################################
#
# Get test username/password

cat <<EOF
The DAQ software connects to the MySQL database via a database-specific
login.  This is not a system login, but internal to the database.
This username and password information will be stored in a configuration
file which will be read by the DAQ software and used to automatically
connect to the database.
EOF

dfltUser='tester'
echo -n "Database username: [$dfltUser] "
read TSTUSER
if [ -z "$TSTUSER" ]; then
  TSTUSER="$dfltUser"
fi

while [ -z "$TSTPASS" ]; do
  echo -n "Database password: "
  read TSTPASS
  if [ -z "$TSTPASS" ]; then
    echo "Please specify a password"
  fi
done

##########################################
#
# Create database

if "$MYSQL" $HOSTARG $USERARG $PASSARG $DBNAME </dev/null >/dev/null 2>&1; then
  echo "$0: Database $DBNAME already exists" >&2
  echo "$0: Please remove it before proceeding" >&2
  exit 1
fi

echo "create database $DBNAME;" |
 "$MYSQL" $HOSTARG $USERARG $PASSARG >/dev/null 2>&1

if "$MYSQL" $HOSTARG $USERARG $PASSARG $DBNAME </dev/null >/dev/null 2>&1; then
  : #created DB
else
  echo "$0: Couldn't create database '$DBNAME'" >&2
  echo "$0: Please check -h(ost), -u(ser), and -p(assword) options" >&2
  exit 1
fi

##########################################
#
# Set up tester access

perms="all"
(echo "grant $perms on $DBNAME.*";
 echo " to '$TSTUSER'@localhost identified by '$TSTPASS';"
 echo "grant $perms on $DBNAME.*";
 echo " to '$TSTUSER'@'%' identified by '$TSTPASS';") |
"$MYSQL" $HOSTARG $USERARG $PASSARG $DBNAME </dev/null >/dev/null 2>&1

##########################################
#
# Create properties file

DBDRIVER="org.gjt.mm.mysql.Driver"
DBURL="jdbc:mysql://$HOST/$DBNAME"

PROPS="domprodtest.properties"
if [ -f "$PROPS" ]; then
  mv "$PROPS" "$PROPS.old"
fi

echo "database.domprodtest.driver: $DBDRIVER" > "$PROPS"
echo "database.domprodtest.url: $DBURL" >> "$PROPS"
echo "database.domprodtest.user: $TSTUSER" >> "$PROPS"
echo "database.domprodtest.password: $TSTPASS" >> "$PROPS"

echo "Properties file created as $PROPS"

exit 0
