--- x2gocreatebase.sh	2009-01-30 02:45:34.000000000 -0500
+++ x2gocreatebase.sh.new	2009-03-25 14:23:41.000000000 -0400
@@ -1,8 +1,117 @@
 #!/bin/bash
 
-su postgres -c "dropdb x2go_sessions"
-su postgres -c "createdb x2go_sessions"
-su postgres -c "echo \"create table sessions(
+if [ $(id -u) -ne 0 ];then
+	echo "$(basename $0) must be run with root privileges."
+	exit 1
+fi
+
+REMOTE=0
+SAVEPASS=0
+ERR=0
+PGPW=
+PGHOME=/var/lib/pgsql/
+
+# Find the database details
+. /etc/x2go/x2godb.conf
+DBNAME=${DBNAME:-x2go_sessions}
+DBUSER=${DBUSER:-postgres}
+SSHPORT=${SSHPORT:-22}
+SCRIPT="/usr/lib/x2go/script/x2gocreatebase.sh"
+
+# If we were not called remotely, we need to obtain the postgres password if necessary
+if [ -z "${1}" ];then 
+	echo
+	echo "If the database administrator requires a password, please enter it."
+	echo "Otherwise, simply press enter."
+	read -s -p "(will not display): " PGPW
+	echo
+fi
+
+# Is this a local or remote database server?
+DBSERVER="$(cat /etc/x2go/sql)"
+if [ "${DBSERVER}" != "local" ]; then
+	echo
+	read -s -p "What is database password for the user, ${DBUSER}? (will not display) " DBPW
+	echo
+	ssh -p ${SSHPORT} ${DBSERVER} "${SCRIPT} ${DBNAME} ${SCHEMA} ${DBUSER} ${DBPW} ${PGPW}"
+	if [ $? -ne 0 ]; then
+		echo "I was not able to create the database on the remote server."
+		exit 2
+	fi
+	TESTS="${DBSERVER}\:\*\:${DBNAME}\:${DBUSER}\:"
+	if [ ! -f /root/.pgpass -o -z "$(grep ${TESTS} /root/.pgpass 2>/dev/null)" ];then
+		# if .pgpass does not exist or it does not already have an entry for this user, add the entry
+		# We must not change an existing password as this routine will not change passwords for 
+		# existing users; it simply the user creation step if the user exists
+		echo "${DBSERVER}:*:${DBNAME}:${DBUSER}:${DBPW}" >> /root/.pgpass
+		chmod 600 /root/.pgpass
+	fi
+	exit 0
+fi
+
+# If we were called remotely, we must use the passed parameters and not the local ones
+if [ -n "${1}" ]; then
+	REMOTE=1
+	DBNAME="${1}"
+	SCHEMA="${2}"
+	DBUSER="${3}"
+	DBPW="${4}"
+	PGPW="${5}"
+	REMOTE=1
+fi
+
+if [ -n "${PGPW}" ];then #postgres uses a password
+	if [ -f ${PGHOME}.pgpass ]; then
+	        su - postgres -c "mv ~/.pgpass ~/.pgpass.x2go"
+                if [ $? != "0" ]; then
+                	echo "Failed to save existing database password file" >&2
+                	exit 1
+                fi
+                SAVEPASS=1
+	fi
+	su - postgres -c "echo -e \"localhost:*:${DBNAME}:postgres:${5}\\\nlocalhost:*:postgres:postgres:${5}\" > ~/.pgpass"
+	if [ ! -f ${PGHOME}.pgpass ]; then
+       		echo "Failed to create database password file" >&2
+	       	ERR=1
+	fi
+	su - postgres -c "chmod 600 ~/.pgpass"
+fi
+
+if [ ${ERR} -eq 0 ];then
+	echo "Creating database in case it does not exist."
+	su - postgres -c "psql -c \"create database \\\"${DBNAME}\\\";\" postgres"
+	ret=$?
+	if [ ${ret} -eq 2 ];then
+		echo "The postres user could not access the database."
+		ERR=2
+	fi
+	if [ ${ret} -eq 1 ];then
+		echo "An error while creating an already extant database is not a problem."
+		echo
+	fi
+fi
+
+if [ ${ERR} -eq 0 ];then
+	echo "Dropping schema in case it already exists."
+	su - postgres -c "psql -c \"drop schema \\\"${SCHEMA}\\\" cascade;\" ${DBNAME}"
+	if [ $? -ne 0 ];then
+		echo "An error while dropping an non-existant schema is not a problem."
+		echo
+	fi
+fi
+
+if [ ${ERR} -eq 0 ];then
+	su - postgres -c "psql -c \"create role \\\"${DBUSER}\\\" with nocreatedb nocreaterole login encrypted password '${DBPW}';\" ${DBNAME}"
+	# We execute the drop schema and create role commands separately because it is acceptable for them to fail
+	# but failing would prevent execution of the other commands if they were all one command
+	if [ $? -ne 0 ];then
+		echo "An error while creating an already existant user is not a problem."
+		echo
+	fi
+	
+	DBCMD="create schema \\\"${SCHEMA}\\\" authorization \\\"${DBUSER}\\\";set search_path to \\\"${SCHEMA}\\\";set role \\\"${DBUSER}\\\";"
+
+	DBCMD="${DBCMD}create table sessions(
 		session_id varchar(500) primary key, 
                 display integer not null, 
 		uname varchar(100) not null, 
@@ -17,26 +126,48 @@
 		sound_port int,
 		fs_port int,
 		unique(display)
-		)\" | psql x2go_sessions"
+		);"
 
-su postgres -c "echo \"create table messages(mess_id varchar(20) primary key, message text)\" | psql x2go_sessions"
+	DBCMD="${DBCMD}create table messages(mess_id varchar(20) primary key, message text);"
 
-su postgres -c "echo \"create table user_messages(
+	DBCMD="${DBCMD}create table user_messages(
                 mess_id varchar(20) not null, 
 		uname varchar(100) not null
-		)\" | psql x2go_sessions"
+		);"
 
-su postgres -c "echo \"create table used_ports(
+	DBCMD="${DBCMD}create table used_ports(
                 server varchar(100) not null,
 		session_id varchar(500) references sessions on delete cascade, 
 		port integer primary key
-		)\" | psql x2go_sessions"
+		);"
 
-su postgres -c "echo \"create table mounts(
+	DBCMD="${DBCMD}create table mounts(
                 session_id varchar(500) references sessions on delete restrict,
 		path varchar(512) not null, 
 		client inet not null, 
 		primary key(path,client)
-		)\" | psql x2go_sessions"
+		);"
+
+	su - postgres -c "psql -c \"${DBCMD}\" ${DBNAME}"
+	if [ $? -ne 0 ];then
+		ERR=3
+	fi
+fi
+
+rm -f ${PGHOME}.pgpass
+if [ $SAVEPASS -eq 1 ]
+# We use SAVEPASS rather than checking for .pgpass.x2go just in case
+# there was not an existing .pgpass but there was an old .pgpass.spm
+then
+        su - postgres -c "mv ~/.pgpass.x2go ~/.pgpass"
+fi
+
+if [ ${ERR} != 0 -a ${REMOTE} == 0 ];then
+	echo "I was not able to create the database."
+fi
 
+# The history file contains user passwords so we delete it
+rm -f ${PGHOME}psql_history
 
+exit ${ERR}
+        

