#!@l_bash@
##
##  dbmail-setup.sh -- DBMail RDBMS Setup Utility
##

#   command line argument sanity check
if [ $# -eq 0 ]; then
    echo "$0:ERROR: invalid command line" 1>&2
    echo "$0:USAGE: $0 install [<database-directory>]" 1>&2
    echo "$0:USAGE: $0 uninstall" 1>&2
    exit 1
fi

#   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_sname=""
    db_suser="@l_rusr@"
    db_spass="@l_rgrp@"
elif [ ".$db_type" = .mysql ]; then
    db_sname="mysql"
    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_sname=`grep "^superuser_database" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
              sed -e 's;^ *superuser_database="\(.*\)".*;\1;'`
    db_suser=`grep "^superuser_username" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
              sed -e 's;^ *superuser_username="\(.*\)".*;\1;'`
    db_spass=`grep "^superuser_password" @l_prefix@/var/postgresql/db/pg_superuser.conf |\
              sed -e 's;^ *superuser_password="\(.*\)".*;\1;'`
fi

#   dispatch operation
cmd="$1"
shift
case "$cmd" in
    install )
        #   create the DBMail database
        if [ $# -gt 0 ]; then
            db_dir="$1"
            shift
        fi
        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 PRIVILEGES ON $db_name.* TO $db_user@localhost IDENTIFIED BY '$db_pass';"
              echo "FLUSH PRIVILEGES;"
            ) | @l_prefix@/bin/mysql --user="$db_suser" --password="$db_spass" "$db_sname"
            @l_prefix@/bin/mysql --user="$db_user" --password="$db_pass" "$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 ENCODING 'UTF8';"
            ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f-
            PGPASSWORD="$db_pass" @l_prefix@/bin/psql -q -U "$db_user" -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" "$db_sname"
        elif [ ".$db_type" = .pgsql ]; then
            ( echo "DROP DATABASE $db_name;"
              echo "DROP TABLESPACE $db_name;"
              echo "DROP ROLE $db_user;"
            ) | PGPASSWORD="$db_spass" @l_prefix@/bin/psql -q -U "$db_suser" -d "$db_sname" -f-
        fi
        ;;
esac

