--- x2gopgwrapper_local	2009-03-25 02:28:00.000000000 -0400
+++ x2gopgwrapper_local.new	2009-03-25 13:51:46.000000000 -0400
@@ -2,36 +2,41 @@
 cd ~
 #use only with sudo !!
 
+. /etc/x2go/x2godb.conf
 UNAME=$SUDO_USER
+DBNAME=${DBNAME:-x2go_sessions}
+if [ -n "${SCHEMA}" ];then # SCHEMA is being used
+        SCHEMA="${SCHEMA}."
+fi
 
 case "$1" in
 
 getdisplays)
-  echo "select '|'||display||'|' from sessions;"|psql -t x2go_sessions
+  echo "select '|'||display||'|' from \"${SCHEMA}\"sessions;"|psql -t ${DBNAME}
   ;;
 
 getports)
-  echo "select '|'||port||'|' from used_ports;"|psql -t x2go_sessions
+  echo "select '|'||port||'|' from \"${SCHEMA}\"used_ports;"|psql -t ${DBNAME}
   ;;
 
 getservers)
-  echo "select server,count(*) from sessions where status != 'F' group by server;"|psql -t x2go_sessions
+  echo "select server,count(*) from \"${SCHEMA}\"sessions where status != 'F' group by server;"|psql -t ${DBNAME}
   ;;
 
 listsessions)
   echo "select agent_pid, session_id, display, server, status,\
    to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,\
    sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,\
-   to_char(now()-init_time,'SSSS'),fs_port from  sessions  \
-   where status !='F' and server='$2' and uname='$UNAME' order by status desc;"|psql -t x2go_sessions
+   to_char(now()-init_time,'SSSS'),fs_port from  \"${SCHEMA}\"sessions  \
+   where status !='F' and server='$2' and uname='$UNAME' order by status desc;"|psql -t ${DBNAME}
   ;;
 
 listsessions_all)
   echo "select agent_pid, session_id, display, server, status,\
    to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,\
    sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,\
-   to_char(now()-init_time,'SSSS'),fs_port  from  sessions  \
-   where status !='F' and uname='$UNAME' order by status desc;"|psql -t x2go_sessions
+   to_char(now()-init_time,'SSSS'),fs_port  from  \"${SCHEMA}\"sessions  \
+   where status !='F' and uname='$UNAME' order by status desc;"|psql -t ${DBNAME}
   ;;
 
 listsessionsroot)
@@ -43,8 +48,8 @@
   echo "select agent_pid, session_id, display, server, status,\
    to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,\
    sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,\
-   to_char(now()-init_time,'SSSS'),fs_port  from  sessions  \
-   where server='$2'  order by status desc;"|psql -t x2go_sessions
+   to_char(now()-init_time,'SSSS'),fs_port  from  \"${SCHEMA}\"sessions  \
+   where server='$2'  order by status desc;"|psql -t ${DBNAME}
   ;;
 
 listsessionsroot_all)
@@ -56,8 +61,8 @@
   echo "select agent_pid, session_id, display, server, status,\
    to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,\
    sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,\
-   to_char(now()-init_time,'SSSS'),fs_port  from  sessions  \
-   order by status desc;"|psql -t x2go_sessions
+   to_char(now()-init_time,'SSSS'),fs_port  from  \"${SCHEMA}\"sessions  \
+   order by status desc;"|psql -t ${DBNAME}
   ;;
 
 listsusp)
@@ -67,7 +72,7 @@
     exit  
   fi
   echo "select session_id, display, uname, server,extract( day from now()-last_time)*24*60+extract(hour from now()-last_time)*60+extract(minute from now()-last_time)\
-   from sessions where server='$2' and status='S';"|psql -t x2go_sessions
+   from \"${SCHEMA}\"sessions where server='$2' and status='S';"|psql -t ${DBNAME}
   ;;
 
 listallrunning)
@@ -79,8 +84,8 @@
   echo "select agent_pid, session_id, display, server, status,\
    to_char(init_time,'DD.MM.YY*HH24:MI:SS'),cookie,client,gr_port,\
    sound_port,to_char(last_time,'DD.MM.YY*HH24:MI:SS'),uname,\
-   to_char(now()-init_time,'SSSS'),fs_port  from  sessions  \
-   where status='R';"|psql -t x2go_sessions
+   to_char(now()-init_time,'SSSS'),fs_port  from  \"${SCHEMA}\"sessions  \
+   where status='R';"|psql -t ${DBNAME}
   ;;
 
 listmails)
@@ -90,9 +95,9 @@
     exit  
   fi
   echo "select user_messages.mess_id,sessions.session_id,\
-      sessions.uname,sessions.display from sessions,user_messages,\
-      messages where sessions.uname=user_messages.uname and sessions.status!='F'\
-      and messages.mess_id=user_messages.mess_id and sessions.server='$2';"|psql -t x2go_sessions
+      sessions.uname,sessions.display from \"${SCHEMA}\"sessions,\"${SCHEMA}\"user_messages,\
+      \"${SCHEMA}\"messages where sessions.uname=user_messages.uname and sessions.status!='F'\
+      and messages.mess_id=user_messages.mess_id and sessions.server='$2';"|psql -t ${DBNAME}
   ;;
 
 getmail)
@@ -101,7 +106,7 @@
     echo "$UNAME, You have not permission to do this job!"
     exit  
   fi
-  echo "select message from messages where mess_id='$2';"|psql -t x2go_sessions
+  echo "select message from \"${SCHEMA}\"messages where mess_id='$2';"|psql -t ${DBNAME}
   ;;
 
 rmmail)
@@ -110,7 +115,7 @@
     echo "$UNAME, You have not permission to do this job!"
     exit  
   fi
-  echo "delete from user_messages where mess_id='$2' and uname='$3';"|psql -t x2go_sessions
+  echo "delete from \"${SCHEMA}\"user_messages where mess_id='$2' and uname='$3';"|psql -t ${DBNAME}
   ;;
 
 rmsessionsroot)
@@ -119,52 +124,52 @@
     echo "$UNAME, You have not permission to do this job!"
     exit  
   fi
-  echo "delete from  sessions  \
-   where session_id='$2';"|psql -t x2go_sessions
+  echo "delete from  \"${SCHEMA}\"sessions  \
+   where session_id='$2';"|psql -t ${DBNAME}
   ;;
 
 getagent)
-  echo "select agent_pid from sessions  where session_id = '$2';"|psql -t x2go_sessions
+  echo "select agent_pid from \"${SCHEMA}\"sessions  where session_id = '$2';"|psql -t ${DBNAME}
   ;;
 
 getdisplay)
-  echo "select display from sessions  where session_id = '$2';"|psql -t x2go_sessions
+  echo "select display from \"${SCHEMA}\"sessions  where session_id = '$2';"|psql -t ${DBNAME}
   ;;
 
 changestatus)
-  echo "update sessions set last_time=now(),status='$2' where session_id = '$3' and uname='$UNAME';"|psql -t x2go_sessions
+  echo "update \"${SCHEMA}\"sessions set last_time=now(),status='$2' where session_id = '$3' and uname='$UNAME';"|psql -t ${DBNAME}
   ;;
 
 resume)
-  echo "update sessions set last_time=now(),status='R',client='$2' where session_id = '$3' and uname='$UNAME';"|psql -t x2go_sessions
+  echo "update \"${SCHEMA}\"sessions set last_time=now(),status='R',client='$2' where session_id = '$3' and uname='$UNAME';"|psql -t ${DBNAME}
   ;;
 
 insertsession)
-  echo "insert into sessions (display,server,uname,session_id) values \
-  ('$2','$3','$UNAME','$4');"|psql x2go_sessions
+  echo "insert into \"${SCHEMA}\"sessions (display,server,uname,session_id) values \
+  ('$2','$3','$UNAME','$4');"|psql ${DBNAME}
   ;;
 
 createsession)
-  echo "update sessions set status='R',last_time=now(),cookie='$2',agent_pid='$3',\
-   client='$4',gr_port='$5',sound_port='$6',fs_port='$7' where session_id='$8' and uname='$UNAME';"|psql x2go_sessions
+  echo "update \"${SCHEMA}\"sessions set status='R',last_time=now(),cookie='$2',agent_pid='$3',\
+   client='$4',gr_port='$5',sound_port='$6',fs_port='$7' where session_id='$8' and uname='$UNAME';"|psql ${DBNAME}
   ;;
 
 insertport)
-  echo "insert into used_ports (server,session_id,port) values \
-  ('$2','$3','$4');"|psql x2go_sessions
+  echo "insert into \"${SCHEMA}\"used_ports (server,session_id,port) values \
+  ('$2','$3','$4');"|psql ${DBNAME}
   ;;
 
 insertmount)
-  echo "insert into mounts (session_id,path,client) values \
-  ('$2','$3','$4');"|psql x2go_sessions
+  echo "insert into \"${SCHEMA}\"mounts (session_id,path,client) values \
+  ('$2','$3','$4');"|psql ${DBNAME}
   ;;
 
 deletemount)
-  echo "delete from mounts where session_id='$2' and path='$3';"|psql x2go_sessions
+  echo "delete from \"${SCHEMA}\"mounts where session_id='$2' and path='$3';"|psql ${DBNAME}
   ;;
 
 getmounts)
-  echo "select client,path from mounts where session_id = '$2';"|psql -t x2go_sessions
+  echo "select client,path from \"${SCHEMA}\"mounts where session_id = '$2';"|psql -t ${DBNAME}
   ;;
 
 *)
