#!/bin/sh

INSTALL_DIR=`pwd`

if [ -z $1 ]; then
    echo "Usage: $0 new-horde-username new-horde-password"
    exit
fi

if [ -z $2 ]; then
    echo "Usage: $0 new-horde-username new-horde-password"
    exit
fi

username=$1
database=$1
password=$2

su - mysql -c "mysqladmin create $database"
su - mysql -c "mysql -e \"grant all on \\\`$database\\\`.* to '$username'@'localhost' identified by '$password';\""

for script in \
/opt/horde/scripts/sql/horde_datatree.mysql.sql \
/opt/horde/scripts/sql/horde_log.sql \
/opt/horde/scripts/sql/horde_prefs.sql \
/opt/horde/scripts/sql/horde_sessionhandler.mysql.sql \
/opt/horde/scripts/sql/horde_tokens.sql \
/opt/horde/scripts/sql/horde_users.sql \
/opt/horde/scripts/sql/horde_vfs.sql \
/opt/horde/turba/scripts/sql/turba_objects.mysql.sql
do
  sed -e "s/^GRANT \(.*\) TO horde.*/GRANT \1 TO \"${username}\"@localhost;/" $script > /tmp/horde-script.sql
  echo \* RUNNING `basename $script`...
  su - mysql -c"mysql $database < /tmp/horde-script.sql"
  rm -f /tmp/horde-script.sql
done

echo ""
echo "Horde Database Setup Complete."


