#!/bin/sh
##
##  drupal-setup
##

#   determine MySQL administrator username/password
username=`grep "^user" @l_prefix@/etc/mysql/my.pwd |\
          sed -e 's;^user[^=]*= *;;' -e 's; *$;;'`
password=`grep "^password" @l_prefix@/etc/mysql/my.pwd |\
          sed -e 's;^password[^=]*= *;;' -e 's; *$;;'`

#   create the MySQL database for Drupal
@l_prefix@/bin/mysqladmin --user=$username --password=$password create drupal
@l_prefix@/bin/mysql --user=$username --password=$password mysql <<EOF
GRANT ALL ON drupal.* TO drupal@localhost IDENTIFIED BY 'drupal';
FLUSH PRIVILEGES;
EOF

#   create the default Drupal database schema
@l_prefix@/bin/mysql --user=$username --password=$password drupal \
    <@l_prefix@/share/drupal/database/database.mysql

#   import Drupal modules extra database schema (requires manual sorting)
for m in  \
    bookmarks/bookmarks.mysql \
    daily/daily.mysql \
    event/event.mysql \
    freelinking/freelinking.mysql \
    nodewords/nodewords.mysql \
    shoutbox/shoutbox.mysql \
    subscriptions/subscriptions.mysql \
    taxonomy_block/taxonomy_block.mysql \
    trackback/trackback.mysql \
    wordfilter/wordfilter.mysql \
; do
    @l_prefix@/bin/mysql --user=$username --password=$password drupal \
                         <@l_prefix@/share/drupal/modules/extra/$m \
    || echo "$0:ERROR: setup $m failed"
done

#  configure /etc/drupal/default/settings.php accordingly
@l_prefix@/lib/openpkg/shtool subst \
    -e '/^\$db_url =/s;mysql://username:password@localhost/databasename;mysql://drupal:drupal@localhost/drupal;' \
    @l_prefix@/etc/drupal/sites/default/settings.php

