#!/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 - postgres -c"
psql template1 << \"EOF\"
create database \"$username\";
create user \"$username\";
alter user \"$username\" with password '$password';
EOF
"

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

echo ""
echo "Horde Database Setup Complete."


