# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# ROCK Linux: rock-src/package/michiel/postgresql/stone_mod_pgsql.sh
# ROCK Linux is Copyright (C) 1998 - 2006 Clifford Wolf
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. A copy of the GNU General Public
# License can be found at Documentation/COPYING.
# 
# Many people helped and are helping developing ROCK Linux. Please
# have a look at http://www.rocklinux.org/ and the Documentation/TEAM
# file for details.
# 
# --- ROCK-COPYRIGHT-NOTE-END ---
#
# [MAIN] 75 pgsql PostgreSQL Database Setup

pgsql_start() {
	gui_cmd 'starting postgresql server' "rc postgresql start"
}

pgsql_stop() {
	gui_cmd 'stopping postgresql server' "rc postgresql stop"
}

pgsql_initdb() {
	export `locale`
	locale=""
	encoding=""

	if gui_yesno "Initialize the database with the current system locale? ($LANG)" ; then
		locale=$LANG
	else
		gui_input "Locale to initialize the DB with: " "$locale" "locale"
	fi

	if gui_yesno "Initialize the database with the default encoding for the locale $locale ?" ; then
		encoding=""
	else
		gui_input "Encoding to initialize the DB with (i.e. UNICODE): " "$encoding" "encoding"
	fi

	gui_cmd "Initializing PostgreSQL Database (locale: $locale)" \
			"echo \"D_prefix/bin/initdb --locale=$locale --encoding=$encoding\" | su - postgres"
}

pgsql_create_user() {
	createdb="--no-createdb"
	adduser="--no-adduser"
	gui_input "User to create: " "$username" "username"

	if getent passwd $username > /dev/null 2>&1 ; then
		gui_yesno "should the user be able to create databases?" && createdb="--createdb"
		gui_yesno "should the user be able to add users?" && adduser="--adduser"
		gui_cmd "Creating ProstgreSQL User $username" \
			"echo \"D_prefix/bin/createuser $createdb $adduser $username\" | su - postgres"
	else
		gui_message "$username is not a valid system user!" ;
	fi
}

pgsql_user_menu() {
	function=$1
	umenu="gui_menu pgsql_users 'Please select user:'"
	for uname in `echo "D_prefix/bin/psql -c '\du' template1" | su - postgres | tail -n+4 | head -n-2 \
		      | awk -F'|' '{ print $1 ; }' | tr -d ' '| grep -v postgres`; do
		umenu="$umenu '$uname' '$function $uname'"
	done;
	unset uname function
	eval $umenu
}

pgsql_db_menu() {
	user=$1
	function=$2
	dmenu="gui_menu pgsql_dbs 'Please select database:'"
	for db in `echo "D_prefix/bin/psql -l" | su - postgres | tail -n+4 | head -n-2 | grep " | $user.[ ]*|" \
			|awk -F'|' '{ print $1 ; }' | tr -d ' '| grep -v template` ; do
		dmenu="$dmenu '$db' '$function $db'"
	done;
	unset user function
	eval $dmenu
}

pgsql_drop_user() {
    if [ -z "$1" ] ; then
	pgsql_user_menu pgsql_drop_user
    else 
	uname=$1
	if gui_yesno "are you sure you want to drop the user '$uname' ?" ; then
		gui_cmd "Dropping User $uname" "echo \"D_prefix/bin/dropuser $uname\" | su - postgres"
	else
		gui_message "user deletion aborted"
	fi
	unset uname
    fi
}

pgsql_create_db() {
	if [ -z "$1" -a -z "$uname" ] ; then
		pgsql_user_menu pgsql_create_db ;
		return
	fi
	[ -z "$uname" -a -n "$1" ] && uname=$1

	gui_input "Name of the database to create for user $uname:" "$dbname" "dbname"

	[ -z "$dbname" ] && return;

	[ -z "$encoding" ] && encoding="UNICODE"

	gui_input "Encoding for the database to create:" "$encoding" "encoding"

	[ -z "$encoding" ] && return;

	gui_cmd "Creating database $dbname for user $uname - $encoding" \
		"echo \"D_prefix/bin/createdb -E $encoding $dbname\" | su - $uname"

	unset dbname encoding uname 
}


pgsql_drop_db() {
	if [ -z "$1" -a -z "$uname" ] ; then
		pgsql_user_menu pgsql_drop_db ;
		return
	fi
	[ -z "$uname" -a -n "$1" ] && uname=$1

	if [ -z "$2" -a -z "$dbname" ] ; then
		pgsql_db_menu $uname "pgsql_drop_db $uname ";
		return
	fi
	[ -z "$dbname" -a -n "$2" ] && dbname=$2

	[ -z "$dbname" -o -z "$uname" ] && return;

	if gui_yesno "Are you sure you want to drop $uname's database $dbname?" ; then
		gui_cmd "Dropping database $dbname for user $uname" \
			"echo \"D_prefix/bin/dropdb $dbname\" | su - $uname"
	fi

	unset dbname uname
}

pgsql_gen_menu () {
    eval `echo "D_prefix/bin/initdb --show 2>&1" | su - postgres | grep PGDATA`
    PGPID="`echo "D_prefix/bin/pg_ctl status" | su - postgres | grep -o 'PID: .[^)]*' | tr -d 'PID: '`"

    echo "gui_menu pgsql 'PostgresQL Database Setup'"
    # initialisation is needed once, so we note if it seems done
    echo " 'Initialize database system'"
    echo " 'pgsql_initdb'"
    if [ -f $PGDATA/postgresql.conf ] ; then
	echo " 'note: this seems to be done already' ''"
        echo " '' ''"
	if [ -z "$PGPID" ] ; then
		echo " 'note: postgres needs to be running for more options' ''"
        	echo " '' ''"
		echo " 'Start PostgreSQL Server' 'pgsql_start'"
	else 
		echo " 'Create a user' 'pgsql_create_user'"
		echo " 'Drop a user' 'pgsql_drop_user'"
        	echo " '' ''"
		echo " 'Create a database' 'pgsql_create_db'"
		echo " 'Drop a database' 'pgsql_drop_db'"
        	echo " '' ''"
		echo " 'Stop PostgreSQL Server' 'pgsql_stop'"
	fi
    fi
}

main() {

    while
        eval `pgsql_gen_menu`
    do :  ; done
}

