#!/bin/sh

# File: refdbd-init-pgsql.sh - performs the part of the post-install setup
# of refdbd which may have to be run from a special PostgreSQL user account
# the script is not supposed to be called standalone, it is called from
# refdb-init.sh instead

# Markus Hoenicka <mhoenicka@users.sourceforge.net> 2006-06-04

# parameters:
# $1: replace_maindb: if true, we have to drop the existing main database
# $2: dbadmin_name: the database superuser name used to authenticate against
#                   PostgreSQL

# check for correct number of parameters
if [ $# -lt 2 ]; then
    echo "Not enough parameters. Do not use this script directly."
    echo "It is invoked by the refdb-init.sh script"
    exit 1
fi

echo "When asked for a password, provide the database superuser password"
if [ "$1" = "y" ]; then
# we have to drop the database and to remove the refdbuser group
    if dropdb -U $2 -W refdb; then
	echo "Could not remove existing main database"
#	exit 1
    fi
    if psql -U $2 -W -c "DROP GROUP refdbuser" template1; then
	echo "Could not remove existing database user group"
#	exit 1
    fi
fi

if createdb -U $2 -W -E UNICODE refdb \
    && psql -U $2 -W refdb < /usr/share/refdb/sql/refdb.dump.pgsql; then
    echo "Could not create main database"
#    exit 1
fi

exit 0


