--- x2gopgwrapper_net	2010-03-09 00:22:29.376611904 -0500
+++ x2gopgwrapper_net.vs	2010-03-09 00:23:11.016611953 -0500
@@ -1,60 +1,19 @@
 #!/bin/bash
 
 cd ~
-#use only with sudo !!
 
 . /etc/x2go/x2godb.conf
 SERVER=$1
-UNAME=$SUDO_USER
 DBNAME=${DBNAME:-x2go_sessions}
+DBUSER=${DBUSER:-postgres}
+SCHEMA=""
 
 case "$2" in
 
-getdisplays)
-  echo "select '|'||display||'|' from sessions;"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
-  ;;
-
-getports)
-  echo "select '|'||port||'|' from used_ports;"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
-  ;;
-
-getservers)
-  echo "select server,count(*) from sessions where status != 'F' group by server;"|psql -U "${DBUSER}" -h ${SERVER} -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='$3' and uname='$UNAME' order by status desc;"|psql -U "${DBUSER}" -h ${SERVER} -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 -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
-  ;;
-
-listsessionsroot)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  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='$3'  order by status desc;"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
-  ;;
-
 listsessionsroot_all)
-  if [ "$UNAME" != "root" ]
+  if [ $(id -u) -ne 0 ]
   then
-    echo "$UNAME, You have not permission to do this job!"
+    echo "${USER}, You have not permission to do this job!"
     exit  
   fi
   echo "select agent_pid, session_id, display, server, status,\
@@ -64,117 +23,36 @@
    order by status desc;"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
   ;;
 
-listsusp)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    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='$3' and status='S';"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
-  ;;
-
-
-listallrunning)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  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 -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
-  ;;
-
-listmails)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    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='$3';"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
-  ;;
-
-getmail)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "select message\
-      from messages where mess_id='$3';"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
-  ;;
-
-
-rmmail)
-  if [ "$UNAME" != "root" ]
-  then
-    echo "$UNAME, You have not permission to do this job!"
-    exit  
-  fi
-  echo "delete from \
-  user_messages where mess_id='$3' and uname='$4';"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
-  ;;
-
-
 rmsessionsroot)
-  if [ "$UNAME" != "root" ]
+  if [ $(id -u) -ne 0 ]
   then
-    echo "$UNAME, You have not permission to do this job!"
+    echo "${USER}, You have not permission to do this job!"
     exit  
   fi
-  echo "delete from  sessions  \
+# ${4} is the server name
+  SCHEMA=${4//./___}
+  echo "delete from ${SCHEMA}.sessions  \
    where session_id='$3' ;"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
   ;;
 
-
-getagent)
-  echo "select agent_pid from sessions  where session_id = '$3';"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
-  ;;
-
 getdisplay)
-  echo "select display from sessions  where session_id = '$3';"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
+  SCHEMA=${4}
+  echo "select display from ${SCHEMA}.sessions  where session_id = '$3';"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
   ;;
 
 changestatus)
-  echo "update sessions set last_time=now(),status='$3' where session_id = '$4' and uname='$UNAME';"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
-  ;;
-
-resume)
-  echo "update sessions set last_time=now(),status='R',client='$3' where session_id = '$4' and uname='$UNAME';"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
-  ;;
-
-insertsession)
-  echo "insert into sessions (display,server,uname,session_id) values \
-  ('$3','$4','$UNAME','$5');"|psql -U "${DBUSER}" -h ${SERVER} ${DBNAME}
-  ;;
-
-createsession)
-  echo "update sessions set status='R',last_time=now(),cookie='$3',agent_pid='$4',\
-   client='$5',gr_port='$6',sound_port='$7',fs_port='$8' where session_id='$9' and uname='$UNAME';"|psql -U "${DBUSER}" -h ${SERVER} ${DBNAME}
-  ;;
-
-insertport)
-  echo "insert into used_ports (server,session_id,port) values \
-  ('$3','$4','$5');"|psql -U "${DBUSER}" -h ${SERVER} ${DBNAME}
-  ;;
-
-insertmount)
-  echo "insert into mounts (session_id,path,client) values \
-  ('$3','$4','$5');"|psql -U "${DBUSER}" -h ${SERVER} ${DBNAME}
+  SCHEMA=${5//./___}
+  echo "update ${SCHEMA}.sessions set last_time=now(),status='$3' where session_id = '$4' and uname='${6}';"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
   ;;
 
 deletemount)
-  echo "delete from mounts where session_id='$3' and path='$4';"|psql -U "${DBUSER}" -h ${SERVER} ${DBNAME}
+  SCHEMA=${5}
+  echo "delete from ${SCHEMA}.mounts where session_id='$3' and path='$4';"|psql -U "${DBUSER}" -h ${SERVER} ${DBNAME}
   ;;
 
 getmounts)
-  echo "select client,path from mounts where session_id = '$3';"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
+  SCHEMA=${4}
+  echo "select client,path from ${SCHEMA}.mounts where session_id = '$3';"|psql -U "${DBUSER}" -h ${SERVER} -t ${DBNAME}
   ;;
 
 *)
