#!/bin/sh
#-------------------------------------------------------------------------
#
# dropdb--
#    destroy a postgres database
#
#    this program runs psql to drop 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/src/bin/scripts/Attic/dropdb,v 1.19 2002/10/18 22:05:36 petere Exp $
#
#-------------------------------------------------------------------------

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

PSQLOPT=
dbname=
forcedel=t

# Check for echo -n vs echo \c

if echo '\c' | grep -s c >/dev/null 2>&1
then
    ECHO_N="echo -n"
    ECHO_C=""
else
    ECHO_N="echo"
    ECHO_C='\c'
fi


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"
		;;
# other options
        --interactive|-i)
		forcedel=f
		;;
	-*)
		echo "$CMDNAME: invalid option: $1" 1>&2
                echo "Try '$CMDNAME --help' for more information." 1>&2
		exit 1
		;;
	 *)
		dbname="$1"
		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 removes a PostgreSQL database."
        echo
	echo "Usage:"
        echo "  $CMDNAME [OPTION]... DBNAME"
        echo
	echo "Options:"
	echo "  -e, --echo                show the query being sent to the backend"
	echo "  -i, --interactive         prompt before deleting anything"
        echo "  -q, --quiet               don't write any messages"
	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 " --help                     show this help, then exit"
	echo
	echo "Report bugs to <pgsql-bugs@postgresql.org>."
	exit 0
fi

if [ -z "$dbname" ]; then
	echo "$CMDNAME: missing required argument database name" 1>&2
        echo "Try '$CMDNAME -?' for help." 1>&2
	exit 1
fi


if [ "$forcedel" = f ]; then
	echo "Database \"$dbname\" will be permanently deleted."
	$ECHO_N "Are you sure? (y/n) "$ECHO_C
	read REPLY

	[ "$?" -eq 1 ] && exit 1
	[ "$REPLY" != "y" -a "$REPLY" != "Y" ] && exit 0
fi


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

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

exit 0
