#!/bin/sh
##
##  dbmail-setup.sh -- Sympa RDBMS Setup Utility
##

#   package configuration
db_type="@db_type@"
db_script="@l_prefix@/share/dbmail/dbmail-setup.sql"

#   determine database details
#   FIXME: bail out if extracted values are empty or don't make sense
db_dir="@l_prefix@/var/dbmail/db"
db_name=`grep "^db"   @l_prefix@/etc/dbmail/dbmail.conf | sed -e 's;^[^=]*= *;;'`
db_user=`grep "^user" @l_prefix@/etc/dbmail/dbmail.conf | sed -e 's;^[^=]*= *;;'`
db_pass=`grep "^pass" @l_prefix@/etc/dbmail/dbmail.conf | sed -e 's;^[^=]*= *;;'`

#   determine RDBMS-specific details
if [ ".$db_type" = .sqlite ]; then
    db_suser="@l_rusr@"
    db_spass="@l_rgrp@"
elif [ ".$db_type" = .mysql ]; then
    db_suser=`grep "^user" @l_prefix@/etc/mysql/my.pwd |\
              sed -e 's;^user[^=]*= *;;' -e 's; *$;;'`
    db_spass=`grep "^password" @l_prefix@/etc/mysql/my.pwd |\
              sed -e 's;^password[^=]*= *;;' -e 's; *$;;'`
elif [ ".$db_type" = .pgsql ]; then
    db_suser="postgresql"
    db_spass="" # has to be interactive
fi

#   dispatch operation
cmd="${1:-"install"}"
case "$cmd" in
    install )
        #   create the DBMail database
        if [ ".$db_type" = .sqlite ]; then
            ( umask 077
              @l_prefix@/bin/sqlite3 $db_name <$db_script
              chown $db_suser:$db_spass $db_name
              chmod 640 $db_name
            ) || exit $?
        elif [ ".$db_type" = .mysql ]; then
            #   FIXME: MySQL doesn't allow easy relocation of tablespaces AFAIK --rse
            @l_prefix@/bin/mysqladmin --user="$db_suser" --password="$db_spass" create "$db_name"
            ( echo "GRANT ALL ON $db_name.* TO $db_user@localhost IDENTIFIED BY '$db_pass';"
              echo "FLUSH PRIVILEGES;"
            ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" mysql
            @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_name" <$db_script
        elif [ ".$db_type" = .pgsql ]; then
            ( echo "CREATE ROLE $db_user LOGIN ENCRYPTED PASSWORD '$db_pass' NOCREATEDB NOCREATEUSER;"
              echo "CREATE TABLESPACE $db_name OWNER $db_user LOCATION '$db_dir';"
              echo "CREATE DATABASE $db_name OWNER $db_user TABLESPACE $db_name;"
            ) | @l_prefix@/bin/psql -U "$db_suser" -d template1 -f-
            @l_prefix@/bin/psql -U "$db_suser" -d "$db_name" -f- <$db_script
        fi
        ;;
    uninstall )
        #   remove the DBMail database
        if [ ".$db_type" = .sqlite ]; then
            rm -f $db_name >/dev/null 2>&1 || true
        elif [ ".$db_type" = .mysql ]; then
            ( echo "DROP DATABASE $db_name;"
            ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" mysql
        elif [ ".$db_type" = .pgsql ]; then
            ( echo "DROP DATABASE $db_name;"
              echo "DROP TABLESPACE $db_name;"
              echo "DROP ROLE $db_user;"
            ) | @l_prefix@/bin/psql -U "$db_suser" -d template1 -f-
        fi
        ;;
esac

