Index: /branches/livecd/LiveTools/rc.local.sh
===================================================================
--- /branches/livecd/LiveTools/rc.local.sh	(revision 599)
+++ /branches/livecd/LiveTools/rc.local.sh	(revision 599)
@@ -0,0 +1,88 @@
+# workaround MATLAB problems
+#mkdir -p /mnt/rofs
+#ln -s /* /mnt/rofs/
+#mount --bind /mnt/rofs /rofs
+
+# Try to find a mindstorms.fat file in any local partition
+# If it exists, mount it to /home/mindstorms/work.
+# Also try to find mindstorms.swp files to use as swap space.
+
+mkdir -p /mnt/localfs /home/mindstorms/work
+chmod 700 /mnt
+
+shopt -s nullglob
+
+# Find boot-time plugged in USB drive to use
+for PART in /dev/disk/by-id/usb-*; do
+	[ "$( vol_id -t "$PART" )" = "vfat" ] || continue
+	mount -t vfat -o rw,uid=999,gid=999,fmask=0133,dmask=0022,iocharset=utf8 \
+		"$PART" /home/mindstorms/work && break
+done
+
+# Mount all partitions read-only
+for PART in /dev/disk/by-id/{ata,scsi}-*-part?; do
+	MOUNTPOINT="/mnt/localfs/${PART##*/}"
+	mkdir "$MOUNTPOINT" || continue
+	mount -o ro "$PART" "$MOUNTPOINT"
+done
+
+# If USB failed, try to find one mindstorms.dir directory
+if ! mountpoint -q /home/mindstorms/work; then
+	for DIR in /mnt/localfs/*/mindstorms.dir; do
+		MOUNTPOINT="${DIR%/*}"
+		if umount "$MOUNTPOINT"; then
+			PART="/dev/disk/by-id/${MOUNTPOINT##*/}"
+			if [ "$( vol_id -t "$PART" )" = "ntfs" ]; then
+				mount -t ntfs-3g -o rw,no_def_opts,umask=077 "$PART" "$MOUNTPOINT" || continue
+			else
+				mount -o rw "$PART" "$MOUNTPOINT" || continue
+			fi
+		fi
+		chown -R mindstorms.mindstorms "$DIR"
+		chmod -R u+rw "$DIR"
+		mount -o bind "$DIR" /home/mindstorms/work && break
+	done
+fi
+
+# If this also failed, try to find a mindstorms.fat file
+if ! mountpoint -q /home/mindstorms/work; then
+	for FAT in /mnt/localfs/*/mindstorms.fat; do
+		MOUNTPOINT="${FAT%/*}"
+		[ "$( vol_id -t "$FAT" )" = "vfat" ] || continue
+		if umount "$MOUNTPOINT"; then
+			PART="/dev/disk/by-id/${MOUNTPOINT##*/}"
+			if [ "$( vol_id -t "$PART" )" = "ntfs" ]; then
+				mount -t ntfs-3g -o rw,no_def_opts,umask=077 "$PART" "$MOUNTPOINT" || continue
+			else
+				mount -o rw "$PART" "$MOUNTPOINT" || continue
+			fi
+		fi
+		mount -t vfat -o loop,rw,uid=999,gid=999,fmask=0133,dmask=0022,iocharset=utf8 \
+			"$FAT" /home/mindstorms/work && break
+	done
+fi
+
+# Try to find all mindstorms.swp files
+for SWP in /mnt/localfs/*/mindstorms.swp; do
+	MOUNTPOINT="${SWP%/*}"
+	PART="/dev/disk/by-id/${MOUNTPOINT##*/}"
+	if umount "$MOUNTPOINT"; then
+		if [ "$( vol_id -t "$PART" )" = "ntfs" ]; then
+			mount -t ntfs-3g -o rw,no_def_opts,umask=077 "$PART" "$MOUNTPOINT" || continue
+		else
+			mount -o rw "$PART" "$MOUNTPOINT" || continue
+		fi
+	fi
+	mkswap "$SWP" || continue
+	swapon "$SWP"
+done
+
+# Try to unmount all unused partitions
+for PART in /mnt/localfs/*; do
+	umount "$PART" 2>/dev/null
+done
+
+# Remove work directory if not mounted
+if ! mountpoint -q /home/mindstorms/work; then
+	rm -rf /home/mindstorms/work
+fi
