#!/bin/sh
#
# This file is released under the terms of the Artistic License.
# Please see the file LICENSE, included in this package, for details.
#
# Copyright The DBT-2 Authors
#

if [ -z ${DBT2DBNAME} ]; then
    echo "DBT2DBNAME not defined."
    exit 1
fi

while getopts "l:" OPT; do
	case ${OPT} in
	l)
		PORT=${OPTARG}
		;;
	esac
done

if [ ! "x${PORT}" = "x" ]; then
    PORTOPT="-p ${PORT}"
fi
PSQL="psql -X ${PORTOPT} -At"

DBEXISTS=$(${PSQL} -l | cut -d "|" -f 1 | grep -c "\<${DBT2DBNAME}\>")

if [ "${DBEXISTS}" -ne 1 ]; then
	echo "Creating database..."
	eval "${PSQL} -d template1" <<- EOF
		CREATE DATABASE ${DBT2DBNAME};
	EOF
fi

exit 0
