# cc_create_db
#
# Create and initialize a new Call Commander database
#
#   (C) Copyright 2003 Fred Gleason <fredg@paravelsystems.com>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as
#   published by the Free Software Foundation; either version 2 of
#   the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public
#   License along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#


echo
echo "Create a new Call Commander database."
echo 

#
# Get the mySQL Administrator Login
#
echo "First, we need an existing mySQL account with rights to create new" 
echo "databases and new users."
echo
read -p "  mySQL account with Admin access [root]: " USER
if [ -z $USER ] ; then
  USER=root
fi
read -s -p "  mySQL Password: " PASSWORD
echo
echo

#
# Get the name of the database to create
#
echo "Next, we need the name of the database to be created."
echo
read -p "  Name of database to create [Commander]: " DATABASE
if [ -z $DATABASE ] ; then
  DATABASE=Commander
fi
echo

#
# Get the application user account
#
echo "Finally, we need to enter the account that Call Commander should use"
echo "to access the database.  If this account does not exist, it will"
echo "be created automatically.  You will also need to select an account"
echo "password.  Be sure to write down these values, as you will need to"
echo "put them into Call Commander's configuration file."
echo
read -p "  Name of mySQL account for application access [ccuser]: " CC_USER
if [ -z $CC_USER ] ; then
  CC_USER=ccuser
fi
read -s -p "  Application Access Password: " CC_PASSWORD
echo

#
# Check that mySQL is running and accessible
#
mysql -s -u $USER -p$PASSWORD -e"use mysql" 2> /dev/null
if [ $? -ne 0 ] ; then
  echo
  echo "  *** Unable to connect to the mySQL server. ***"
  echo "  It's possible that the server is not currently running, or that"
  echo "  the administrator name/password you've supplied are not correct."  
  echo "  Please correct the problem and try again."
  echo
  exit 1
fi

#
# Check for an existing Call Commander database
#
mysql -s -u $USER -p$PASSWORD -e use\ $DATABASE 2> /dev/null
if [ $? -eq 0 ] ; then
  echo
  echo -n "  *** A "
  echo -n $DATABASE
  echo " database already exists! ***"
  echo "  Do you want to overwrite it and create a new one?"
  echo "  [WARNING: overwriting the database will COMPLETELY DESTROY"
  echo "  any existing data!]"
  echo
  read -p "  Overwrite existing database [y/N]: " OVERWRITE
  echo
  case "$OVERWRITE" in
    "y" | "Y")
      mysql -s -u $USER -p$PASSWORD -e drop\ database\ $DATABASE
    ;;
    "n" | "N" | "")
    echo "  Database NOT overwritten."
    echo
    exit 0
    ;;
  esac
fi 

#
# Create the database
#
echo -n "  Creating "
echo -n $DATABASE
echo -n " Database..."
mysql -u $USER -p$PASSWORD -e CREATE\ DATABASE\ IF\ NOT\ EXISTS\ $DATABASE

#
# Create CALLS table
#
mysql -u $USER -p$PASSWORD -e"USE $DATABASE;
      CREATE TABLE IF NOT EXISTS CALLS (
      ID INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
      PHONE CHAR(10),
      SHOWCODE CHAR(4),
      CALL_DATETIME DATETIME,
      NAME CHAR(16),
      AGE INT,
      QUALITY INT,
      CELLPHONE ENUM('N','Y'),
      CITY CHAR(20),
      STATE CHAR(2),
      ZIP CHAR(10),
      STATION CHAR(4),
      GENDER ENUM('M','F'),
      COMMENT CHAR(94),
      RING_TIMER INT DEFAULT 0,
      HOLD_TIMER INT DEFAULT 0,
      AIR_TIMER INT DEFAULT 0,
      INDEX PHONE_IDX (PHONE),
      INDEX SHOWCODE_IDX (SHOWCODE)
)"
mysql -u $USER -p$PASSWORD -e"USE $DATABASE;
      GRANT ALL ON CALLS TO $CC_USER IDENTIFIED BY \"$CC_PASSWORD\""

echo "done."
echo


# End of cc_create_db



