pnd_run.sh: clean up paths after pnd exit
[pandora-libraries.git] / testdata / scripts / pnd_run.sh
index ab02b26..abf1b1e 100755 (executable)
@@ -100,6 +100,16 @@ PND_WaitFor() {
        PND_EndTask
 }
 
+PND_WaitForExit() {
+       local l_sleep=1
+       PID=$(pidof -o %PPID -x "$1")   # get pid of app
+       while [ -n "$PID" ]; do
+               sleep $l_sleep
+               test $l_sleep -lt 10 && l_sleep=$(($l_sleep + 1))
+               PID=$(pidof -o %PPID -x "$1")
+       done
+}
+
 PND_Exec() {
        local CMD="$*"
        {
@@ -360,10 +370,10 @@ mountPnd() {
 
        # For backward compatibility
        if [[ "$MOUNT_TARGET" != "$PND_MOUNT_DIR" ]];then
-               if [ -d "$PND_MOUNT_DIR/$PND_NAME" ];then
-                       rmdir "$PND_MOUNT_DIR/$PND_NAME"
-               else
+               if [ -h "$PND_MOUNT_DIR/$PND_NAME" ];then
                        rm "$PND_MOUNT_DIR/$PND_NAME"
+               else
+                       rmdir "$PND_MOUNT_DIR/$PND_NAME"
                fi
                if [ ! -e "$PND_MOUNT_DIR/$PND_NAME" ];then
                        ln -s "$MOUNT_TARGET/$PND_NAME" "$PND_MOUNT_DIR/$PND_NAME"
@@ -525,12 +535,18 @@ runApp() {
                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
@@ -545,7 +561,7 @@ runApp() {
        export XDG_DATA_HOME="$HOME"
        export XDG_CACHE_HOME="$HOME"
 
-       if echo "$EXENAME"|grep -q ^\.\/;then
+       if [ "$DOTSLASH" = "true" ]; then
                "$EXENAME" $ARGUMENTS "$@"
        else
                "./$EXENAME" $ARGUMENTS "$@"
@@ -553,12 +569,16 @@ runApp() {
        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
+       LD_LIBRARY_PATH= PATH="$REAL_PATH" PND_WaitForExit "$EXENAME"
+
+       # 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
 }
 
@@ -626,6 +646,17 @@ main() {
                        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
@@ -718,7 +749,7 @@ elif [ ! -d "$MOUNTPOINT" ]; then
 fi
 [ ! -z $APPDATASET ] || [ -z ${MOUNTPOINT} ] && APPDATADIR=${APPDATADIR:-$(dirname $PND)/$PND_NAME}
 APPDATADIR=${APPDATADIR:-${MOUNTPOINT}/pandora/appdata/${PND_NAME}}
-APPDD_FSTYPE=$(mount|awk '$3=="'${MOUNTPOINT}'"{print $5}')
+APPDD_FSTYPE=$(cat /proc/mounts | awk '$2=="'${MOUNTPOINT}'"{print $3}' | head -1)
 CPUSPEED_FILE=${MOUNTPOINT}/pandora/appdata/${PND_NAME}/cpuspeed
 if [ -f "$CPUSPEED_FILE" ]; then
        PND_CPUSPEED=$(cat "$CPUSPEED_FILE")