+}
+
+cleanups() {
+ #delete folders created by aufs if empty
+ rmdir -rf "${APPDATADIR}/.wh..wh.plnk" 2>/dev/null
+ rmdir -rf "${APPDATADIR}/.wh..wh..tmp" 2>/dev/null
+ rmdir "${APPDATADIR}/.wh..wh.orph" 2>/dev/null
+ rm "${APPDATADIR}/.aufs.xino" 2>/dev/null
+
+ #delete appdata folder and ancestors if _empty_
+ rmdir -p "${APPDATADIR}" 2>/dev/null
+
+ # Clean the loopback device
+ if [ $PND_FSTYPE = ISO ] || [ $PND_FSTYPE = Squashfs ]; then # check if we where running an iso, clean up loop device if we did
+ LOOP=$(/sbin/losetup -a | grep "$(basename $PND)" | tail -n1 | awk -F: '{print $1}')
+ /sbin/losetup -d $LOOP
+ #rm $LOOP
+ fi
+ /sbin/losetup -a|cut -d':' -f 1|while read l;do
+ if ! mount|grep -q $l;then
+ echo "WARNING Found $l loop as unused. flushing"
+ /sbin/losetup -d $l
+ fi
+ done
+
+ echo cleanup done
+}
+
+umountPnd() {
+ MOUNT_TARGET="${1:-$PND_MOUNT_DIR}"
+ if is_pnd_mounted;then
+ PND_WaitFor noMoreProcessPnd "Waiting the PND mount dir to be free"
+ umount "$MOUNT_TARGET/$PND_NAME"
+ fi
+ if is_pnd_mounted; then
+ echo WARNING umount PND failed, didnt clean up. Process still using this FS :
+ list_using_fs "$MOUNT_TARGET/$PND_NAME"
+ show_mounted_info
+ else
+ # removing the now useless mountpoint
+ if [ -d "$MOUNT_TARGET/$PND_NAME" ];then
+ rmdir "$MOUNT_TARGET/$PND_NAME"
+ fi
+ if [ -h "$PND_MOUNT_DIR/$PND_NAME" ];then
+ rm "$PND_MOUNT_DIR/$PND_NAME"
+ fi
+
+ # All went well, cleaning
+ cleanups
+ fi
+}
+
+umountUnion() {
+ # Are we root yet ?
+ if [ $(id -u) -ne 0 ];then
+ sudo /usr/pandora/scripts/pnd_run.sh -u -p "$PND" -b "$PND_NAME"
+ return $?
+ fi
+
+ # Make sure the Union FS is unmounted
+ #PND_INTERACTIVE=2
+ if is_union_mounted;then
+ PND_WaitFor noMoreProcessUnion "Waiting the Union to be available"
+ umount "$UNION_MOUNT_DIR/$PND_NAME" #umount union
+ fi
+ if is_union_mounted; then
+ echo "WARNING umount UNION failed, didnt clean up. Process still using this FS :"
+ list_using_fs "$UNION_MOUNT_DIR/$PND_NAME"
+ show_mounted_info
+ else
+ # the Union is umounted, removing the now empty mountpoint
+ if [[ "$PND_MOUNT_DIR" != "$UNION_MOUNT_DIR" ]];then
+ if [ -d "$UNION_MOUNT_DIR/$PND_NAME" ];then
+ rmdir "$UNION_MOUNT_DIR/$PND_NAME"
+ elif [ -e "$UNION_MOUNT_DIR/$PND_NAME" ];then
+ rm "$UNION_MOUNT_DIR/$PND_NAME" >/dev/null 2>&1 # as it might be a symlink
+ fi
+ fi
+ # Try umounting the PND
+ umountPnd $UNION_MOUNT_DIR
+ fi
+}
+
+
+
+#=============================================================================
+# Create the condition to run an app, run it and wait for it's end
+runApp() {
+ cd "$UNION_MOUNT_DIR/$PND_NAME" # cd to union mount
+ if [ "$STARTDIR" ] && [ -d "$STARTDIR" ]; then
+ cd "$STARTDIR"; # cd to folder specified by the optional arg -s
+ fi
+
+ if [ -d "$UNION_MOUNT_DIR/$PND_NAME/lib" ];then
+ export LD_LIBRARY_PATH="$UNION_MOUNT_DIR/$PND_NAME/lib:${LD_LIBRARY_PATH:-"/usr/lib:/lib"}"
+ else
+ export LD_LIBRARY_PATH="$UNION_MOUNT_DIR/$PND_NAME:${LD_LIBRARY_PATH:-"/usr/lib:/lib"}"
+ fi
+
+ if [ -d "$UNION_MOUNT_DIR/$PND_NAME/bin" ];then
+ export PATH="$UNION_MOUNT_DIR/$PND_NAME/bin:${PATH:-"/usr/bin:/bin:/usr/local/bin"}"
+ fi
+
+ if [ -d "$UNION_MOUNT_DIR/$PND_NAME/share" ];then
+ export XDG_DATA_DIRS="$UNION_MOUNT_DIR/$PND_NAME/share:$XDG_DATA_DIRS:/usr/share"
+ fi
+
+ export REAL_HOME="$HOME"
+ export HOME="$UNION_MOUNT_DIR/$PND_NAME"
+ export XDG_CONFIG_HOME="$HOME"
+ export XDG_DATA_HOME="$HOME"
+ export XDG_CACHE_HOME="$HOME"
+
+ if echo "$EXENAME"|grep -q ^\.\/;then
+ "$EXENAME" $ARGUMENTS "$@"
+ else
+ "./$EXENAME" $ARGUMENTS "$@"
+ fi
+ RC=$?
+