Index: scripts/etc/7dtd.conf
===================================================================
--- scripts/etc/7dtd.conf	(revision 7)
+++ scripts/etc/7dtd.conf	(revision 9)
@@ -8,5 +8,7 @@
 # Root directory of the engine
 export SDTD_ROOT=/home/sdtd/7dtd
-export SDTD_SERVERCONFIG=$SDTD_ROOT/../serverconfig.xml
+
+# Root of the 7dtd folders, containing e.g. the "instances" folder
+export SDTD_BASE=/home/sdtd
 
 # Root directory for backups
@@ -16,47 +18,4 @@
 export SDTD_USER=sdtd
 export SDTD_GROUP=sdtd
-
-# World to load
-export SDTD_WORLD=Navezgane
-
-# Name of savegame to load
-export SDTD_SAVE=a7
-
-# Base port to use for networking
-# Note that baseport (UDP) and base+1 up to base+3 (each TCP) will be used
-export SDTD_PORT=25000
-
-# Set this to a reasonable value to get notified if the engine uses up too much RAM
-# Value in mebibytes (MiB)
-# For a server only used for 7dtd this should be ~500 MiB lower than the machines total RAM
-export MEM_NOTIFY_MAX_RAM=2500
-
-# If limit exceeded notify someone by email?
-export MEM_NOTIFY_MAIL=no
-
-# If limit exceeded notify someone by XMPP (aka Jabber)?
-export MEM_NOTIFY_XMPP=yes
-
-# XMPP user to use for sending
-export XMPP_USER=
-# XMPP server for sending
-export XMPP_SERVER=
-# XMPP account password
-export XMPP_PASSWORD=
-# Receiver of message
-export XMPP_TARGET=
-
-# Mail from field
-export MAIL_FROM=
-# Mail receiver
-export MAIL_TO=
-# Subject of mail
-export MAIL_SUBJECT=7dtd memory usage
-# SMTP server to use for sending
-export MAIL_SMTP=
-# SMTP user (not used if empty)
-export MAIL_USER=
-# SMTP password (not used if empty)
-export MAIL_PASS=
 
 # Paths to binaries. Use "which BINARYNAME" to find the path of a single binary
@@ -68,8 +27,2 @@
 export PIDOF=/bin/pidof
 export WINE=/usr/bin/wine
-
-if [ -e $SDTD_SERVERCONFIG ]; then
-	export SDTD_TELNET_ENABLED=`xmllint --xpath "string(/ServerSettings/property[@name='TelnetEnabled']/@value)" $SDTD_SERVERCONFIG`
-	export SDTD_TELNET_PORT=`xmllint --xpath "string(/ServerSettings/property[@name='TelnetPort']/@value)" $SDTD_SERVERCONFIG`
-	export SDTD_TELNET_PASSWORD=`xmllint --xpath "string(/ServerSettings/property[@name='TelnetPassword']/@value)" $SDTD_SERVERCONFIG`
-fi
Index: scripts/etc/init.d/7dtd.sh
===================================================================
--- scripts/etc/init.d/7dtd.sh	(revision 7)
+++ scripts/etc/init.d/7dtd.sh	(revision 9)
@@ -13,67 +13,30 @@
 ### END INIT INFO
 
-. /etc/7dtd.conf
-
-NAME="7DtD"
-BINARYPATH=$SDTD_ROOT
-USER=$SDTD_USER
-RUNTIME=$WINE
-BINARYNAME=7DaysToDie.exe
-PIDFILE="7dtd-server.pid"
-
-OPTS="-quit -batchmode -nographics -configfile=../serverconfig.xml -dedicated"
-
-cd "$BINARYPATH"
+. /usr/local/bin/7dtd-common.sh
+checkRootLoadConf
 
 case "$1" in
     start)
-        echo -n "Starting the $NAME server... "
-        if [ ! `pgrep Xvfb` ]; then
-        	su -c "/usr/bin/Xvfb :1 -screen 0 640x480x16" sdtd &
-        	sleep 3
-        fi
-	if [ -e "$BINARYNAME" ]; then
-	    if [ `pgrep $BINARYNAME` ]; then
-	        echo "Already running!"
-	    else
-	    	export DISPLAY=localhost:1.0
-                start-stop-daemon --start --pidfile $SDTD_ROOT/$PIDFILE --background --no-close --make-pidfile --chuid $USER --user $USER --chdir $BINARYPATH --exec  -- -D -m -S $SCREENREF $RUNTIME $BINARYPATH/$BINARYNAME $OPTS
-                sleep 1
-            fi
-        else
-            echo "Could not find binary, aborting!"
-            exit 5
-        fi
-        if [ `pgrep -F $SDTD_ROOT/$PIDFILE` ]; then
-            echo "Done!"
-        else
-            echo "Failed!"
-            rm -f $SDTD_ROOT/$PIDFILE
-        fi
+    	echo "Starting all 7dtd instances:"
+        for I in $SDTD_BASE/instances/*; do
+            ins=`basename $I`
+            echo "Starting: $ins"
+            /usr/local/bin/7dtd-start.sh $ins
+        done
+        echo "All done"
     ;;
     stop)
-    	start-stop-daemon --status --pidfile $SDTD_ROOT/$PIDFILE
-        if [ $? -eq 0 ]; then
-            echo -n "Stopping the $NAME server... "
-            if [ "$SDTD_TELNET_ENABLED" = "true" ] && [ -n "$SDTD_TELNET_PASSWORD" ]; then
-                echo "$SDTD_TELNET_PASSWORD\nshutdown" | nc -q 2 127.0.0.1 $SDTD_TELNET_PORT
-                sleep 5
-            else
-                echo "Telnet not enabled or no password set. No graceful shutdown!"
-            fi
-            start-stop-daemon --stop --pidfile $SDTD_ROOT/$PIDFILE
-            rm $SDTD_ROOT/$PIDFILE
-            echo "Done!"
-        else
-            echo "$NAME not running"
-        fi
+    	echo "Stopping all 7dtd instances:"
+        for I in $SDTD_BASE/instances/*; do
+            ins=`basename $I`
+            echo "Stopping: $ins"
+            /usr/local/bin/7dtd-kill.sh $ins
+            echo
+            echo
+        done
+        echo "All done"
     ;;
     status)
-    	start-stop-daemon --status --pidfile $SDTD_ROOT/$PIDFILE
-        if [ $? -eq 0 ]; then
-            echo "$NAME server is running"
-        else
-            echo "$NAME server not running"
-        fi
+        /usr/local/bin/7dtd-instances.sh
     ;;
     *)
Index: scripts/home/sdtd/.screenrc
===================================================================
--- scripts/home/sdtd/.screenrc	(revision 7)
+++ 	(revision )
@@ -1,3 +1,0 @@
-multiuser on
-acladd root
-
Index: scripts/usr/local/bin/7dtd-backup.sh
===================================================================
--- scripts/usr/local/bin/7dtd-backup.sh	(revision 7)
+++ scripts/usr/local/bin/7dtd-backup.sh	(revision 9)
@@ -18,5 +18,5 @@
 fi
 
-$RSYNC -a --delete --numeric-ids --delete-excluded $SDTD_ROOT/Data/Worlds/./ $NewBackup
+$RSYNC -a --delete --numeric-ids --delete-excluded $SDTD_BASE/instances/./ $NewBackup
 touch $NewBackup
 
Index: scripts/usr/local/bin/7dtd-check_mem.sh
===================================================================
--- scripts/usr/local/bin/7dtd-check_mem.sh	(revision 7)
+++ 	(revision )
@@ -1,36 +1,0 @@
-#!/bin/bash
-. /etc/7dtd.conf
-
-if [[ "$MEM_NOTIFY_XMPP" = "no" ]]; then
-	if [[ "$MEM_NOTIFY_MAIL" = "no" ]]; then
-		exit
-	fi
-fi
-
-PID=`$PIDOF 7DaysToDie.exe`
-
-if [[ -z "$PID" ]]; then
-	exit 0
-fi
-
-USED_CUR_MB=`$FREE -m | $AWK '/buffers\/cache/{print $3;}'`
-#USED_CUR_MB=`cat /proc/$PID/status | $AWK '/VmRSS/{print int($2/1024);}'`
-TELNETPORT=$(($SDTD_PORT + 3))
-#USED_CHUNKS=`echo -e "cc" | nc -q 3 127.0.0.1 $TELNETPORT | grep -a Mem | awk '/Chunk/{print $3;}'`
-
-if [ "$USED_CUR_MB" -gt "$MEM_NOTIFY_MAX_RAM" ]
-then
-	MESSAGE="7dtd: $USED_CUR_MB MiB of memory used! Chunk memory: $USED_CHUNKS"
-	if [[ "$MEM_NOTIFY_XMPP" = "yes" ]]; then
-		echo $MESSAGE | sendxmpp -u $XMPP_USER -p $XMPP_PASSWORD -j $XMPP_SERVER -t $XMPP_TARGET
-	fi
-	if [[ "$MEM_NOTIFY_MAIL" = "yes" ]]; then
-		if [[ -n "$MAIL_USER" ]]; then
-			userparam="-xu $MAIL_USER"
-		fi
-		if [[ -n "$MAIL_PASS" ]]; then
-			passparam="-xp $MAIL_PASS"
-		fi
-		sendEmail -q -f $MAIL_FROM -t $MAIL_TO -u "$MAIL_SUBJECT" -m "$MESSAGE" -s $MAIL_SMTP $userparam $passparam
-	fi
-fi
Index: scripts/usr/local/bin/7dtd-common.sh
===================================================================
--- scripts/usr/local/bin/7dtd-common.sh	(revision 9)
+++ scripts/usr/local/bin/7dtd-common.sh	(revision 9)
@@ -0,0 +1,78 @@
+#!/bin/bash
+# Provides common functions for 7dtd-scripts. Not intended to be run directly.
+
+# Check if the script is run as root (exit otherwise) and load global config
+checkRootLoadConf() {
+	if [ `id -u` -ne 0 ]; then
+		echo "This script has to be run as root!"
+		exit 10
+	fi
+	. /etc/7dtd.conf
+}
+
+# Get the config path for the given instance
+# Params:
+#   1: Instance name
+# Returns:
+#   Config path for instance
+getInstancePath() {
+	echo $SDTD_BASE/instances/$1
+}
+
+# Check if the given instance name is an existing instance
+# On failure exit the script!
+# Params:
+#   1: Instance name
+checkInstance() {
+	if [ -z $1 ]; then
+		echo "No instance given!"
+		exit 2
+	fi
+	if [ ! -d $(getInstancePath $1) ]; then
+		echo "Instance $1 does not exist!"
+		exit 3
+	fi
+}
+
+# Check if the given instance is currently running
+# Params:
+#   1: Instance name
+# Returns:
+#   0 = not running
+#   1 = running
+isRunning() {
+	start-stop-daemon --status --pidfile $(getInstancePath $1)/7dtd.pid
+	if [ $? -eq 0 ]; then
+		echo 1
+	else
+		echo 0
+	fi
+}
+
+# Get a single value from a serverconfig
+# Params:
+#   1: Instance name
+#   2: Property name
+# Returns:
+#   Property value
+getConfigValue() {
+	CONF=$(getInstancePath $1)/serverconfig.xml
+	xmllint --xpath "string(/ServerSettings/property[@name='$2']/@value)" $CONF
+}
+
+# Send a single command to the telnet port
+# Params:
+#   1: Instance name
+#   2: Command
+# Returns:
+#   String of telnet output
+telnetCommand() {
+	TEL_ENABLED=$(getConfigValue $1 TelnetEnabled)
+	TEL_PORT=$(getConfigValue $1 TelnetPort)
+	TEL_PASS=$(getConfigValue $1 TelnetPassword)	
+	if [ "$TEL_ENABLED" = "true" ] && [ -n "$TEL_PASS" ]; then
+		echo -e "$TEL_PASS\n$2\nexit" | nc -q 2 127.0.0.1 $TEL_PORT
+	else
+		echo "Telnet not enabled or no password set."
+	fi
+}
Index: scripts/usr/local/bin/7dtd-instances.sh
===================================================================
--- scripts/usr/local/bin/7dtd-instances.sh	(revision 9)
+++ scripts/usr/local/bin/7dtd-instances.sh	(revision 9)
@@ -0,0 +1,27 @@
+#!/bin/bash
+# Lists available 7dtd instances.
+# Returns:
+#  0
+
+. /usr/local/bin/7dtd-common.sh
+checkRootLoadConf
+
+printf "%-*s | %-*s | %-*s\n" 20 "Instance name" 8 "Running" 7 "Players"
+printf -v line "%*s-+-%*s-+-%*s\n" 20 " " 8 " " 7 " "
+echo ${line// /-}
+for I in $SDTD_BASE/instances/*; do
+	ins=`basename $I`
+	run=$(isRunning $ins)
+	if [ $run -eq 1 ]; then
+		run="yes"
+	else
+		run="no"
+	fi
+
+	tel=$(telnetCommand $ins lp)
+	max=$(getConfigValue $ins ServerMaxPlayerCount)
+	cur=`echo $tel | sed "s/\r/\n/g" | sed "s/^ //g" | grep "Total of " | cut -d\  -f 3`
+	
+	printf "%-*s | %*s |   %2d/%2d\n" 20 "$ins" 8 "$run" $cur $max
+done
+exit 0
Index: scripts/usr/local/bin/7dtd-kill.sh
===================================================================
--- scripts/usr/local/bin/7dtd-kill.sh	(revision 7)
+++ scripts/usr/local/bin/7dtd-kill.sh	(revision 9)
@@ -1,2 +1,24 @@
 #!/bin/bash
-/etc/init.d/7dtd.sh stop
+# Tries to stop the 7dtd instance given as first parameter.
+# Returns:
+#  0 : Done
+#  1 : Was not running
+#  2 : No instance name given
+#  3 : No such instance
+
+. /usr/local/bin/7dtd-common.sh
+checkRootLoadConf
+
+checkInstance $1
+
+res=$(isRunning $1)
+if [ $res -eq 1 ]; then
+	telnetCommand shutdown
+	start-stop-daemon --stop --pidfile $(getInstancePath $1)/7dtd.pid
+	rm $(getInstancePath $1)/7dtd.pid
+	echo "Done"	
+	exit 0
+else
+	echo "7dtd instance $1 is NOT running"
+	exit 1
+fi
Index: scripts/usr/local/bin/7dtd-running.sh
===================================================================
--- scripts/usr/local/bin/7dtd-running.sh	(revision 9)
+++ scripts/usr/local/bin/7dtd-running.sh	(revision 9)
@@ -0,0 +1,23 @@
+#!/bin/bash
+# Checks if the 7dtd instance given as first parameter is running.
+# Returns:
+#  0 : Running
+#  1 : Not running
+#  2 : No instance name given
+#  3 : No such instance
+
+. /usr/local/bin/7dtd-common.sh
+checkRootLoadConf
+
+checkInstance $1
+
+res=$(isRunning $1)
+if [ $res -eq 1 ]; then
+	echo "7dtd instance $1 is running"
+	echo "Open ports:"
+	netstat -nlp | grep 15161 | sed -r 's/^.*[^ :]*:([^ ]*).*[^ :]*:[^ ]*.*/    \1/g' | sort
+	exit 0
+else
+	echo "7dtd instance $1 is NOT running"
+	exit 1
+fi
Index: scripts/usr/local/bin/7dtd-start.sh
===================================================================
--- scripts/usr/local/bin/7dtd-start.sh	(revision 7)
+++ scripts/usr/local/bin/7dtd-start.sh	(revision 9)
@@ -1,2 +1,43 @@
 #!/bin/bash
-/etc/init.d/7dtd.sh start
+# Tries to start the 7dtd instance given as first parameter.
+# Returns:
+#  0 : Done
+#  1 : Was already running
+#  2 : No instance name given
+#  3 : No such instance
+#  5 : Unknown error when starting engine
+
+. /usr/local/bin/7dtd-common.sh
+checkRootLoadConf
+
+checkInstance $1
+
+res=$(isRunning $1)
+if [ $res -eq 0 ]; then
+	if [ ! `pgrep Xvfb` ]; then
+		echo "Xvfb not yet running. Starting..."
+		su -c "/usr/bin/Xvfb :1 -screen 0 640x480x16" $SDTD_USER &> /dev/null &
+		sleep 3
+	fi
+	export DISPLAY=localhost:1.0
+	
+	SSD_PID="--pidfile $(getInstancePath $1)/7dtd.pid --make-pidfile"
+	SSD_DAEMON="--background --no-close"
+	SSD_USER="--chuid $SDTD_USER:$SDTD_GROUP --user $SDTD_USER"
+	OPTS="-quit -batchmode -nographics -configfile=$(getInstancePath $1)/serverconfig.xml -dedicated"
+	
+	start-stop-daemon --start $SSD_PID $SSD_DAEMON $SSD_USER --chdir $SDTD_ROOT --exec $WINE -- $SDTD_ROOT/7DaysToDie.exe $OPTS > $(getInstancePath $1)/stdout.log 2>&1
+	sleep 1
+	res=$(isRunning $1)
+	if [ $res -eq 1 ]; then
+		echo "Done!"
+		exit 0
+	else
+		echo "Failed!"
+		rm -f $(getInstancePath $1)/7dtd.pid
+		exit 5
+	fi
+else
+	echo "7dtd instance $1 is already running"
+	exit 1
+fi
Index: scripts/usr/local/bin/7dtd-update.sh
===================================================================
--- scripts/usr/local/bin/7dtd-update.sh	(revision 7)
+++ scripts/usr/local/bin/7dtd-update.sh	(revision 9)
@@ -1,4 +1,15 @@
 #!/bin/bash
-. /etc/7dtd.conf
+. /usr/local/bin/7dtd-common.sh
+checkRootLoadConf
+
+for I in $SDTD_BASE/instances/*; do
+	ins=`basename $I`
+	run=$(isRunning $ins)
+	if [ $run -eq 1 ]; then
+		echo "At least one instance is still running."
+		echo "Before updating the engine please stop all instances!"
+		exit 1
+	fi
+done
 
 if [ ! -e $STEAMCMD_ROOT ]; then
@@ -13,5 +24,5 @@
 cd $STEAMCMD_ROOT
 
-./steamcmd.sh +@sSteamCmdForcePlatformType windows +login $STEAM_USER $STEAM_PASS +force_install_dir $SDTD_ROOT +app_update 251570 validate +quit
+./steamcmd.sh +@sSteamCmdForcePlatformType windows +login $STEAM_USER $STEAM_PASS +force_install_dir $SDTD_ROOT "+app_update 251570" validate +quit
 chown $SDTD_USER.$SDTD_GROUP -R $SDTD_ROOT
 
@@ -19,3 +30,3 @@
 cp $SDTD_ROOT/Install/32bit/mono.dll $SDTD_ROOT/7DaysToDie_Data/Mono/
 cp $SDTD_ROOT/Install/32bit/SteamworksManaged.dll $SDTD_ROOT/7DaysToDie_Data/Managed/
-cp $SDTD_ROOT/../msvcr100.dll $SDTD_ROOT/
+cp $SDTD_BASE/msvcr100.dll $SDTD_ROOT/
