+}
+
+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 XDG_CONFIG_HOME="$UNION_MOUNT_DIR/$PND_NAME"
+ export REAL_HOME="$HOME"
+ export HOME="$UNION_MOUNT_DIR/$PND_NAME"
+
+ if echo "$EXENAME"|grep -q ^\.\/;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!
+ PID=$(pidof -o %PPID -x \"$EXENAME\") # get pid of app
+ while [ "$PID" ];do # wait till we get no pid back for tha app, again a bit ugly, but it works
+ sleep 10s
+ PID=`pidof -o %PPID -x \"$EXENAME\"`
+ done
+ export HOME="$REAL_HOME"
+ 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)
+ if [ -e "${APPDATADIR}/PND_pre_script.sh" ]; then
+ PND_BeginTask "Starting user configured pre-script"
+ . ${APPDATADIR}/PND_pre_script.sh # Sourcing so it can shared vars with post-script ;)
+ PND_EndTask
+ fi
+ PND_BeginTask "Starting the application ($EXENAME $ARGUMENTS)"
+ runApp
+ PND_EndTask
+ if [ -e "${APPDATADIR}/PND_post_script.sh" ]; then
+ PND_BeginTask "Starting user configured post-script"
+ . ${APPDATADIR}/PND_post_script.sh
+ PND_EndTask
+ fi
+ 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
+ if ! lsof /dev/fb1 > /dev/null; then
+ PND_BeginTask "Restoring the frame buffer status"
+ ofbset -fb /dev/fb1 -mem 0 -size 0 0 -en 0
+ PND_EndTask
+ fi
+ PND_BeginTask "uMount the PND"
+ umountUnion
+ PND_EndTask
+ ;;
+ esac
+}
+
+######################################################################################
+#### Parsing the arguments :
+##
+ACTION=run
+while [ "$#" -gt 0 ];do
+ if [ "$#" -gt 1 ] && ( [[ "$(echo $2|cut -c 1)" != "-" ]] || [[ "$1" = "-a" ]] );then
+ case "$1" in
+ -p) PND="$2";;
+ -e) EXENAME="$2";;
+ -b) PND_NAME="$2";;
+ -s) STARTDIR="$2";;
+ -j) append="$2";;
+ -c) PND_CPUSPEED="$2";;
+ -d) APPDATASET=1;APPDATADIR="$2";;
+ -a) ARGUMENTS="$2";;
+ *) echo "ERROR while parsing arguments: \"$1 $2\" is not a valid argument";
+ echo "Arguments were : $PND_ARGS"
+ showHelp;
+ exit 1 ;;
+ esac
+ shift 2
+ else # there's no $2 or it's an argument
+ case "$1" in
+ -m) ACTION=mount;;
+ -u) ACTION=umount;;
+ -x) CLOSE_X=1;;
+ -d) APPDATASET=1;;
+ *) echo "ERROR while parsing arguments: \"$1\" is not a valid argument";
+ echo "Arguments were : $PND_ARGS"
+ showHelp;
+ exit 1 ;;
+ esac
+ shift
+
+ fi
+done
+
+# getting the real full path to the file
+PND="$(readlink -f $PND)"
+
+#PND_NAME really should be something sensible and somewhat unique
+#if -b is set use that as pnd_name, else generate it from PND
+#get basename (strip extension if file) for union mountpoints etc, maybe this should be changed to something specified inside the xml
+#this should probably be changed to .... something more sensible
+#currently only everything up to the first '.' inside the filenames is used.
+PND_NAME="${PND_NAME:-"$(basename $PND | cut -d'.' -f1)"}"
+
+PND_LOG="/tmp/pndrun_${PND_NAME}.out"
+PND_HEADER="PND PND_FSTYPE APPDATADIR APPDD_FSTYPE PND_CPUSPEED EXENAME ARGUMENTS"
+
+if [ ! -e "$PND" ]; then #check if theres a pnd suplied, need to clean that up a bit more
+ echo "ERROR: selected PND($PND) file does not exist!"
+ showHelp
+ exit 1