#!/bin/sh
#-------------------------------------------------------------------------
#
# createdb--
#    create a postgres database
#
#    This program runs psql with the "-c" option to create
#    the requested database.
#
# Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
# Portions Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
#    $Header: /cvsroot/pgsql-server/src/bin/scripts/Attic/createdb,v 1.28 2002/10/18 22:05:36 petere Exp $
#
#-------------------------------------------------------------------------

CMDNAME=`basename "$0"`
PATHNAME=`echo "$0" | sed "s,$CMDNAME\$,,"`

MB=
TEMPLATE=
PSQLOPT=
dbname=
dbowner=
dbcomment=
dbpath=

while [ "$#" -gt 0 ]
do
    case "$1" in
	--help|-\?)
		usage=t
                break
		;;
# options passed on to psql
	--host|-h)
		PSQLOPT="$PSQLOPT -h $2"
		shift;;
        -h*)
                PSQLOPT="$PSQLOPT $1"
                ;;
        --host=*)
                PSQLOPT="$PSQLOPT -h `echo \"$1\" | sed 's/^--host=//'`"
                ;;
	--port|-p)
		PSQLOPT="$PSQLOPT -p $2"
		shift;;
        -p*)
                PSQLOPT="$PSQLOPT $1"
                ;;
        --port=*)
                PSQLOPT="$PSQLOPT -p `echo \"$1\" | sed 's/^--port=//'`"
                ;;
	--username|-U)
		PSQLOPT="$PSQLOPT -U $2"
		shift;;
        -U*)
                PSQLOPT="$PSQLOPT $1"
                ;;
        --username=*)
                PSQLOPT="$PSQLOPT -U `echo \"$1\" | sed 's/^--username=//'`"
                ;;
	--password|-W)
		PSQLOPT="$PSQLOPT -W"
		;;
	--echo|-e)
		PSQLOPT="$PSQLOPT -e"
		;;
	--quiet|-q)
		PSQLOPT="$PSQLOPT -o /dev/null"
		;;
# options converted into SQL command
	--owner|-O)
		dbowner="$2"
		shift;;
        -O*)
                dbowner=`echo "$1" | sed 's/^-O//'`
                ;;
        --owner=*)
                dbowner=`echo "$1" | sed 's/^--owner=//'`
                ;;
	--location|-D)
		dbpath="$2"
		shift;;
        -D*)
                dbpath=`echo "$1" | sed 's/^-D//'`
                ;;
        --location=*)
                dbpath=`echo "$1" | sed 's/^--location=//'`
                ;;
	--template|-T)
		TEMPLATE="$2"
		shift;;
        -T*)
                TEMPLATE=`echo "$1" | sed 's/^-T//'`
                ;;
        --template=*)
                TEMPLATE=`echo "$1" | sed 's/^--template=//'`
                ;;
	--encoding|-E)
		MB="$2"
		shift;;
        -E*)
                MB=`echo "$1" | sed 's/^-E//'`
                ;;
        --encoding=*)
                MB=`echo "$1" | sed 's/^--encoding=//'`
                ;;
	-*)
		echo "$CMDNAME: invalid option: $1" 1>&2
                echo "Try '$CMDNAME --help' for more information." 1>&2
		exit 1
		;;
	*)
		dbname="$1"
		if [ "$2" ]
		then
			shift
			dbcomment="$1"
		fi
		if [ "$#" -ne 1 ]; then
			echo "$CMDNAME: invalid option: $2" 1>&2
	                echo "Try '$CMDNAME --help' for more information." 1>&2
			exit 1
		fi
		;;
    esac
    shift
done

if [ "$usage" ]; then
        echo "$CMDNAME creates a PostgreSQL database."
        echo
	echo "Usage:"
        echo "  $CMDNAME [OPTION]... [DBNAME] [DESCRIPTION]"
        echo
	echo "Options:"
	echo "  -D, --location=PATH       alternative place to store the database"
	echo "  -E, --encoding=ENCODING   encoding for the database"
	echo "  -O, --owner=OWNER         database user to own the new database"
	echo "  -T, --template=TEMPLATE   template database to copy"
	echo "  -e, --echo                show the query being sent to the backend"
        echo "  -q, --quiet               don't write any messages"
	echo "  --help                    show this help, then exit"
	echo
	echo "Connection options:"
	echo "  -h, --host=HOSTNAME       database server host"
	echo "  -p, --port=PORT           database server port"
	echo "  -U, --username=USERNAME   user name to connect as"
	echo "  -W, --password            prompt for password"
        echo
        echo "By default, a database with the same name as the current user is created."
	echo
	echo "Report bugs to <pgsql-bugs@postgresql.org>."
	exit 0
fi


if [ -n "$MB" ]
then
        mbcode=`${PATHNAME}pg_encoding "$MB"`
        if [ -z "$mbcode" ]
	then
		echo "$CMDNAME: \"$MB\" is not a valid encoding name" 1>&2
		exit 1
	fi
fi

if [ -z "$dbname" ]; then
        if [ "$PGDATABASE" ]; then
                dbname="$PGDATABASE"
        elif [ "$PGUSER" ]; then
                dbname="$PGUSER"
        else
                dbname=`${PATHNAME}pg_id -u -n`
        fi
        [ "$?" -ne 0 ] && exit 1
fi


# escape the quotes
dbpath=`echo "$dbpath" | sed "s/'/\\\\\'/g"`
dbname=`echo "$dbname" | sed 's/\"/\\\"/g'`
TEMPLATE=`echo "$TEMPLATE" | sed 's/\"/\"\"/g'`

withstring=
[ "$dbowner" ] &&    withstring="$withstring OWNER = \"$dbowner\""
[ "$dbpath" ] &&     withstring="$withstring LOCATION = '$dbpath'"
[ "$MB" ] &&         withstring="$withstring ENCODING = '$MB'"
[ "$TEMPLATE" ] &&   withstring="$withstring TEMPLATE = \"$TEMPLATE\""
[ "$withstring" ] && withstring=" WITH$withstring"

${PATHNAME}psql $PSQLOPT -d template1 -c "SET autocommit TO 'on';CREATE DATABASE \"$dbname\"$withstring"
if [ "$?" -ne 0 ]; then
	echo "$CMDNAME: database creation failed" 1>&2
	exit 1
fi

# Insert comment as well, if requested
[ -z "$dbcomment" ] && exit 0

dbcomment=`echo "$dbcomment" | sed "s/'/\\\\\'/g"`

${PATHNAME}psql $PSQLOPT -d "$dbname" -c "SET autocommit TO 'on';COMMENT ON DATABASE \"$dbname\" IS '$dbcomment'"
if [ "$?" -ne 0 ]; then
	echo "$CMDNAME: comment creation failed (database was created)" 1>&2
	exit 1
fi

exit 0
