+}
+
+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
+
+ echo "$EXENAME" | grep -q "^\.\/" && DOTSLASH="true"
+
+ # now set up the env for the PND, don't run any tools after this point
+ # because PATH and LD_LIBRARY_PATH may be different and incompatible
+
+ 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
+
+ REAL_PATH="$PATH"
+ 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 [ -e "${APPDATADIR}/PND_pre_script.sh" ]; then
+ echo "Starting user configured pre-script ${APPDATADIR}/PND_pre_script.sh"
+ . ${APPDATADIR}/PND_pre_script.sh # Sourcing so it can shared vars with post-script ;)
+ echo "${APPDATADIR}/PND_pre_script.sh is done"
+ fi
+
+ if [ "$DOTSLASH" = "true" ]; then
+ "$EXENAME" $ARGUMENTS "$@"
+ else
+ "./$EXENAME" $ARGUMENTS "$@"
+ fi
+ RC=$?
+
+ #the app could have exited now, OR it went into bg, we still need to wait in that case till it really quits!
+ LD_LIBRARY_PATH= PATH="$REAL_PATH" PND_WaitForExit "$EXENAME"
+
+ if [ -e "${APPDATADIR}/PND_post_script.sh" ]; then
+ echo "Starting user configured post-script ${APPDATADIR}/PND_post_script.sh"
+ . ${APPDATADIR}/PND_post_script.sh
+ echo "${APPDATADIR}/PND_post_script.sh is done"
+ fi
+
+ # clean up to avoid any effects of the .pnd on the remaining parts of this script
+ unset LD_LIBRARY_PATH
+ export HOME="$REAL_HOME"
+ unset REAL_HOME
+ export PATH="$REAL_PATH"
+ unset REAL_PATH
+ # ... the rest should not matter
+
+ return $RC
+}
+
+
+main() {
+ case $ACTION in
+ mount)
+ mountUnion
+ ;;
+ umount)
+ umountUnion
+ ;;
+ run)
+ PND_BeginTask "Mount the PND"
+ mountUnion
+ PND_EndTask
+ if [ $? -ne 0 ];then
+ zenity --warning --title="Mounting the PND failed" --text="Mounting the PND failed. The application wont start. Please have a look at $PND_LOG"
+ return 3
+ fi
+ if [ -e /proc/pandora/cpu_mhz_max ] && [ ! -z "$PND_CPUSPEED" ];then
+ PND_BeginTask "Set CPU speed"
+ PND_setCPUSpeed
+ PND_EndTask
+ fi
+ if [ $CLOSE_X ]; then
+ PND_BeginTask "Closing X"
+ PND_CloseX
+ PND_EndTask
+ fi
+ oPWD=$(pwd)
+ old_fb0_geometry=$(fbset -fb /dev/fb0 -s | grep geometry | awk '{print $2, $3, $4, $5, $6}')
+
+ PND_BeginTask "Starting the application ( $EXENAME $ARGUMENTS "$@")"
+ runApp "$@"
+ PND_EndTask
+
+ cd $oPWD
+ if [ $CLOSE_X ]; then
+ PND_BeginTask "Restarting X"
+ PND_RestartX
+ PND_EndTask
+ fi
+ if [ ! -z "$CURRENTSPEED" ]; then
+ PND_BeginTask "Reset CPU speed to $CURRENTSPEED"
+ PND_resetCPUSpeed
+ PND_EndTask
+ fi
+ PND_BeginTask "Restoring the frame buffer status"
+ ofbset -fb /dev/fb0 -pos 0 0
+ fbset -fb /dev/fb0 -g $old_fb0_geometry
+ old_res=$(echo $old_fb0_geometry | awk '{print $1, $2}')
+ ofbset -fb /dev/fb0 -size $old_res -en 1
+ if ! lsof /dev/fb1 > /dev/null; then
+ ofbset -fb /dev/fb1 -mem 0 -size 0 0 -en 0
+ fi
+ PND_EndTask
+
+ running_pnd_count=$(ps ax | grep "pnd_run.s[h]" | wc -l)
+ # restore settings if we are the last exiting pnd
+ # compare with 2 because one is used by subshell where "ps ax | ..." runs
+ if [ "$running_pnd_count" -eq "2" ]; then
+ PND_BeginTask "Restoring nub mode"
+ sed -n '1p' /etc/pandora/conf/nubs.state > /proc/pandora/nub0/mode
+ sed -n '7p' /etc/pandora/conf/nubs.state > /proc/pandora/nub1/mode
+ PND_EndTask
+ fi
+
+ PND_BeginTask "uMount the PND"
+ umountUnion
+ PND_EndTask
+ ;;
+ esac
+}
+
+######################################################################################
+#### Parsing the arguments :
+##
+if [ "$#" -lt 1 ]; then
+ showHelp
+ exit 1