#!/bin/sh
#-------------------------------------------------------------------------
#
# createuser--
#    Utility for creating a user in the PostgreSQL database
#
# Copyright (c) 1994, Regents of the University of California
#
#
# IDENTIFICATION
#    $Header: /cvsroot/pgsql/src/bin/scripts/Attic/createuser,v 1.4 1999/12/16 20:10:02 momjian Exp $
#
# Note - this should NOT be setuid.
#
#-------------------------------------------------------------------------

CMDNAME=`basename $0`

NewUser=
SysID=
CanAddUser=
CanCreateDb=
PwPrompt=
Password=
PSQLOPT=

# 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
		;;
# 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=//'`
                ;;
# Note: These two specify the user to connect as (like in psql),
#       not the user you're creating.
	--user|--username|-U)
		PSQLOPT="$PSQLOPT -U '$2'"
		shift;;
        -U*)
                PSQLOPT="$PSQLOPT $1"
                ;;
        --user=*)
                PSQLOPT="$PSQLOPT -U "`echo $1 | sed 's/^--user=//'`
                ;;
        --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	
        --createdb|-d)
		CanCreateDb=t
		;;
        --no-createdb|-D)
		CanCreateDb=f
		;;
        --adduser|-a)
		CanAddUser=t
		;;
        --no-adduser|-A)
		CanAddUser=f
		;;
        --sysid|-i)
                SysID=$2
                shift;;
        --sysid=*)
                SysID=`echo $1 | sed 's/^--sysid=//'`
                ;;
        -i*)
                SysID=`echo $1 | sed 's/^-i//'`
                ;;
	--pwprompt|--pw|-P)
		PwPrompt=t
		;;
	-*)
		echo "$CMDNAME: Unrecognized option: $1. Try -? for help."
		exit 1
		;;
         *)
		NewUser=$1
		;;
    esac
    shift;
done

if [ "$usage" ]; then	
	echo ""
	echo "Usage: $CMDNAME [options] [username]"
	echo ""
	echo "    -h HOSTNAME, --host=HOSTNAME     "
	echo "    -p PORT,     --port=PORT         "
	echo "    -d,          --createdb          "
	echo "    -D,          --no-createdb       "
	echo "    -a,          --adduser           "
	echo "    -A,          --no-adduser        "
	echo "    -i SYSID,    --sysid=SYSID       "     
	echo "    -P,          --pwprompt          "
	echo "    -U USERNAME, --username=USERNAME (for connect to db)"
	echo "    -W,          --password          (for connect to db)"
	echo "    -e,          --echo              "
        echo "    -q,          --quiet             "   
	echo "    -?,          --help              "
	echo ""
	exit 1
fi

if [ "$SysID" ]; then
        if [ "$SysID" != "`echo $SysID | sed 's/[^0-9]//g'`" ]; then
                echo "$CMDNAME: User sysid must be a positive number."
                exit 1
        fi
fi

# Get missing user attributes

if [ -z "$NewUser" ]; then
	$ECHO_N "Enter name of user to add: "$ECHO_C
	read NewUser
	[ $? -ne 0 ] && exit 1
fi

if [ "$PwPrompt" ]; then
	$ECHO_N "Enter password for user \"$NewUser\": "$ECHO_C
	read Password
fi

if [ -z "$CanCreateDb" ]; then
	$ECHO_N "Shall the new user be allowed to create databases? (y/n) "$ECHO_C
	read REPLY
	[ $? -ne 0 ] && exit 1
	if [ $REPLY = "y" -o $REPLY = "Y" ]; then
		CanCreateDb=t
	else
		CanCreateDb=f
	fi
fi

if [ -z "$CanAddUser" ]; then
	$ECHO_N "Shall the new user be allowed to create more new users? (y/n) "$ECHO_C
	read REPLY
	[ $? -ne 0 ] && exit 1
	if [ $REPLY = "y" -o $REPLY = "Y" ]; then
		CanAddUser=t
	else
		CanAddUser=f
	fi
fi


#
# build SQL command
#
NewUser=`echo $NewUser | sed 's/\"/\\\"/g'`
Password=`echo $Password | sed 's/\"/\\\"/g'`

QUERY="CREATE USER \"$NewUser\""

SUBQUERY=
[ "$SysID" ] &&    SUBQUERY="$SUBQUERY SYSID $SysID"
[ "$Password" ] && SUBQUERY="$SUBQUERY PASSWORD \"$Password\""
[ "$SUBQUERY" ] &&        QUERY="$QUERY WITH $SUBQUERY"

[ "$CanCreateDb" = t ] && QUERY="$QUERY CREATEDB"
[ "$CanCreateDb" = f ] && QUERY="$QUERY NOCREATEDB"
[ "$CanAddUser" = t ] &&  QUERY="$QUERY CREATEUSER"
[ "$CanAddUser" = f ] &&  QUERY="$QUERY NOCREATEUSER"

psql $PSQLOPT -d template1 -c "$QUERY"
if [ $? -ne 0 ]; then
	echo "$CMDNAME: Creation of user \"$NewUser\" failed."
	exit 1
fi
		
exit 0
