pnd_run.sh: restore nub settings on last pnd exit
[pandora-libraries.git] / testdata / scripts / pnd_run.sh
index f9b03fe..c6cdc9b 100755 (executable)
@@ -15,8 +15,7 @@
 #. $SCRIPT_DIR/pnd_loging
 #PND_LogDateFormat=PND_Time
 
-#PND_MOUNT_DIR="/mnt/pnd"
-PND_MOUNT_DIR="/mnt/utmp"
+PND_MOUNT_DIR="/mnt/pnd"
 UNION_MOUNT_DIR="/mnt/utmp"
 CPUSPEEDSCRIPT=/usr/pandora/scripts/op_cpuspeed.sh
 
@@ -35,7 +34,7 @@ PND_Start() {
                printf "%-15s : %s\n" "$v"  "$(eval "echo \${$v:-'<unset>'}")"
        done
        echo "======================================================================================="
-       }>$PND_LOG
+       }>"$PND_LOG"
 }
 
 PND_checkLog() {
@@ -43,7 +42,7 @@ awk 'BEGIN{R=0}END{exit R}
 /cannot open display/||/unary operator expected/||/No such file or directory/||/command not found/{R=R+1}
 {IGNORECASE=1}
 !/gpg-error/&&/ERROR/||/FAILED/{R=R+1}
-{IGNORECASE=0}' < $PND_LOG
+{IGNORECASE=0}' < "$PND_LOG"
 }
 
 PND_Stop() {
@@ -53,7 +52,7 @@ PND_Stop() {
        {
        echo "======================================================================================="
        echo "Return code is : $RC"
-       }>>$PND_LOG
+       }>>"$PND_LOG"
        return $RC
 }
 
@@ -136,7 +135,7 @@ PND_Exec() {
 showHelp() {
        cat <<endHELP
 Usage:
-  pnd_run.sh -p file.pnd -e cmd [-a args] [-b pndid] [-s path] [-c speed] [-d [path]] [-x] [-m] [-u]
+  pnd_run.sh -p file.pnd -e cmd [-a args] [-b pndid] [-s path] [-c speed] [-d [path]] [-x] [-m] [-u] [-- more_args]
     -p file.pnd        : Specify the pnd file to execute
     -e cmd     : Command to run
     -a args    : Arguments to the command
@@ -147,6 +146,9 @@ Usage:
     -x         : Stop X before starting the apps
     -m         : Only mount the pnd, dont run it (-e become optional)
     -u         : Only umount the pnd, dont run it (-e become optional)
+
+  If '--' is specified, all subsequent arguments are passed through to the command
+  (useful if you want to pass quotes and weird chars to the command)
 endHELP
 }
 
@@ -260,7 +262,7 @@ PND_RestartX(){
 show_mounted_info(){
        echo "+++++++"
        echo "Loopback devices :"
-       sudo losetup -a
+       sudo /sbin/losetup -a
        echo "Are mounted on :"
        mount|grep loop
        echo "For these Union :"
@@ -272,7 +274,8 @@ is_union_mounted() {
 }
 
 is_pnd_mounted() {
-       mount |grep -v aufs | grep -q "on $PND_MOUNT_DIR/${PND_NAME} type"
+       mount |grep -v aufs | grep -q "on $PND_MOUNT_DIR/${PND_NAME} type" || \
+       mount |grep -v aufs | grep -q "on $UNION_MOUNT_DIR/${PND_NAME} type"
 }
 
 noMoreProcessPnd() {
@@ -284,9 +287,10 @@ noMoreProcessUnion() {
 }
 
 mountPnd() {
+       MOUNT_TARGET="${1:-$PND_MOUNT_DIR}"
        if ! is_pnd_mounted;then
                #check if pnd is already attached to loop 
-               LOOP=$(losetup -a | grep "$PND" | tail -n1 | awk -F: '{print $1}')
+               LOOP=$(/sbin/losetup -a | grep "$PND" | tail -n1 | awk -F: '{print $1}')
                #check if the loop device is already mounted
                if ! [ -z "$LOOP" ];then
                        echo "Found a loop ($LOOP), using it"
@@ -313,8 +317,8 @@ mountPnd() {
                        #detect fs
                        case $PND_FSTYPE in
                        ISO)
-                               /sbin/losetup $FREELOOP "$PND" #attach the pnd to the loop device
-                               mntline="mount" #setup the mountline for later
+                               /sbin/losetup -r $FREELOOP "$PND" #attach the pnd to the loop device
+                               mntline="mount -o ro" #setup the mountline for later
                                mntdev="${FREELOOP}"
                                ;;
                        directory)
@@ -324,22 +328,22 @@ mountPnd() {
                                mntdev="${PND}"
                                ;;
                        Squashfs)
-                               /sbin/losetup $FREELOOP "$PND" #attach the pnd to the loop device
-                               mntline="mount -t squashfs"
+                               /sbin/losetup -r $FREELOOP "$PND" #attach the pnd to the loop device
+                               mntline="mount -t squashfs -o ro"
                                mntdev="${FREELOOP}"
                                ;;
                        *)
                                echo "ERROR Unknown filesystem type : $PND_FSTYPE"
                                exit 1;;
                        esac
-                       echo "Mounting : $mntline \"$mntdev\" \"$PND_MOUNT_DIR/${PND_NAME}\""
-                       $mntline "$mntdev" "$PND_MOUNT_DIR/${PND_NAME}" #mount the pnd/folder
+                       echo "Mounting : $mntline \"$mntdev\" \"$MOUNT_TARGET/${PND_NAME}\""
+                       $mntline "$mntdev" "$MOUNT_TARGET/${PND_NAME}" #mount the pnd/folder
 
                        if ! is_pnd_mounted ;then
                                sleep 1
                                echo "WARNING : mount faild, re-tring"
                                sleep 1
-                               $mntline "$mntdev" "$PND_MOUNT_DIR/${PND_NAME}" #mount the pnd/folder
+                               $mntline "$mntdev" "$MOUNT_TARGET/${PND_NAME}" #mount the pnd/folder
                                if ! is_pnd_mounted ;then
                                        echo "ERROR The PND File-system is not mounted !"
                                        show_mounted_info
@@ -353,6 +357,18 @@ mountPnd() {
                      ln -s $loopmountedon "$UNION_MOUNT_DIR/$PND_NAME" 
                fi
        fi
+
+       # 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
+                       rm "$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"
+               fi
+       fi
 }
 
 mountUnion() {
@@ -381,7 +397,7 @@ mountUnion() {
        #is the union already mounted? if not mount evrything, else launch the stuff
        if ! is_union_mounted;then
                if ! is_pnd_mounted;then
-                       mountPnd || return 2; # quit mounting the union if the PND first didnt mount
+                       mountPnd "$UNION_MOUNT_DIR"|| return 2; # quit mounting the union if the PND first didnt mount
                else
                        echo "WARNING The PND is already mounted, using it"
                        show_mounted_info
@@ -430,11 +446,11 @@ cleanups() {
 
        # 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=$(losetup -a | grep "$(basename $PND)" | tail -n1 | awk -F: '{print $1}')
+               LOOP=$(/sbin/losetup -a | grep "$(basename $PND)" | tail -n1 | awk -F: '{print $1}')
                /sbin/losetup -d $LOOP
                #rm $LOOP
        fi
-       losetup -a|cut -d':' -f 1|while read l;do
+       /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
@@ -445,18 +461,22 @@ cleanups() {
 }
 
 umountPnd() {
+       MOUNT_TARGET="${1:-$PND_MOUNT_DIR}"
        if is_pnd_mounted;then
                PND_WaitFor noMoreProcessPnd "Waiting the PND mount dir to be free"
-               umount "$PND_MOUNT_DIR/$PND_NAME"
+               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 "$PND_MOUNT_DIR/$PND_NAME"
+               list_using_fs "$MOUNT_TARGET/$PND_NAME"
                show_mounted_info
        else
                # removing the now useless mountpoint
-               if [ -d $PND_MOUNT_DIR/$PND_NAME ];then
-                       rmdir "$PND_MOUNT_DIR/$PND_NAME"
+               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
@@ -491,7 +511,7 @@ umountUnion() {
                        fi
                fi
                # Try umounting the PND
-               umountPnd
+               umountPnd $UNION_MOUNT_DIR
        fi
 }
 
@@ -505,23 +525,31 @@ runApp() {
                cd "$STARTDIR";                 # cd to folder specified by the optional arg -s
        fi
 
-       if [ -d $UNION_MOUNT_DIR/$PND_NAME/lib ];then
+       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
+       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
+       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"
+       export XDG_CONFIG_HOME="$HOME"
+       export XDG_DATA_HOME="$HOME"
+       export XDG_CACHE_HOME="$HOME"
 
-       "./$EXENAME" $ARGUMENTS
+       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!
@@ -530,6 +558,7 @@ runApp() {
                sleep 10s
                PID=`pidof -o %PPID -x \"$EXENAME\"`
        done
+       export HOME="$REAL_HOME"
        return $RC
 }
 
@@ -561,14 +590,17 @@ main() {
                        PND_EndTask
                fi
                oPWD=$(pwd)
+               old_fb0_geometry=$(fbset -fb /dev/fb0 -s | grep geometry | awk '{print $2, $3, $4, $5, $6}')
                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_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
@@ -585,6 +617,26 @@ main() {
                        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
@@ -595,8 +647,18 @@ main() {
 ######################################################################################
 ####   Parsing the arguments :
 ##
+if [ "$#" -lt 1 ]; then
+       showHelp
+       exit 1
+fi
+
 ACTION=run
 while [ "$#" -gt 0 ];do
+       if [ "$1" == "--" ]; then
+               shift
+               break
+       fi
+
        if [ "$#" -gt 1 ] && ( [[ "$(echo $2|cut -c 1)" != "-" ]] || [[ "$1" = "-a" ]] );then
                case "$1" in
                 -p) PND="$2";;
@@ -608,7 +670,6 @@ while [ "$#" -gt 0 ];do
                 -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
@@ -620,7 +681,6 @@ while [ "$#" -gt 0 ];do
                 -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
@@ -629,12 +689,21 @@ while [ "$#" -gt 0 ];do
        fi
 done
 
+if test -z "$PND"; then
+       echo "ERROR: pnd file provided (-p)"
+       showHelp
+       exit 1
+fi
+
+# 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_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"
@@ -646,7 +715,7 @@ if [ ! -e "$PND" ]; then #check if theres a pnd suplied, need to clean that up a
 fi
 
 if [ ! "$EXENAME" ] && [[ "$ACTION" = "run" ]]; then
-       echo "ERROR: no executable name provided!"
+       echo "ERROR: no executable name provided! (-e)"
        showHelp
        exit 1
 fi
@@ -672,12 +741,12 @@ if [[ "$ACTION" == "run" ]];then
        PND_Start
        {
        if [ $CLOSE_X ]; then
-               main 2>&1 & 
+               main "$@" 2>&1 & 
                disown
        else
-               main 2>&1
+               main "$@" 2>&1
        fi
-       }>>$PND_LOG
+       }>>"$PND_LOG"
        PND_Stop
 else
        main