Bringing in patches from cpasjuste
[pandora-libraries.git] / testdata / scripts / pnd_run.sh
index 6ee7523..c78d3a9 100755 (executable)
@@ -1,15 +1,25 @@
 #!/bin/bash
+ #needs some serious cleanup!
  
-#Usage: pnd_run.sh -p your.pnd -e executeable [-a \"(arguments)\"] [ -s \"cd to folder inside pnd\"] [-u (skip union)] [-b override BASENAME (name of mountpoint/appdata)]
+#Usage: pnd_run.sh -p your.pnd -e executeable [-a "(arguments)"] [ -s "cd to folder inside pnd"] [-u (skip union)] [-b override BASENAME (name of mountpoint/appdata)] [-x close x before launching(script needs to be started with nohup for this to work]
 # -n to skip union mount, should probably be removed before release
 # -s startdir
 # arguments can be inside -e, -a is optional
  
 #/etc/sudoers needs to be adjusted if you touch any of the sudo lines in the wrong place.
  
+# look at the comments in the nox part, adjust 
+#use "lsof /usr/lib/libX11.so.6 | awk '{print $1}'| sort | uniq > whitelist" with nothing running to generate the whitelist
+#launch the script with nohup for -x to work!
+#todo
+#make sure to only use free loop devices!
+#cleanup
  
 # parse arguments
-TEMP=`getopt -o p:e:a:b:s:m::u::n:: -- "$@"`
+TEMP=`getopt -o p:e:a:b:s:m::u::n::x:: -- "$@"`
  
 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
  
@@ -20,11 +30,12 @@ while true ; do
        case "$1" in
                -p) echo "pnd set to \`$2'" ;PND=$2;shift 2;;
                -e) echo "exec set to \`$2'" ;EXENAME=$2;shift 2 ;;
-               -n) echo "u set, no union pls!";NOUNION=1;shift 2;;
+               -n) echo "n set, no union pls!";NOUNION=1;shift 2;;
                -b) echo "BASENAME set to $2";BASENAME=$2;shift 2;;
                -s) echo "startdir set to $2";STARTDIR=$2;shift 2;;
                -m) echo "mount";mount=1;shift 2;;
                -u) echo "umount";umount=1;shift 2;;
+               -x) echo "no x";nox=1;shift 2;;
                -a) 
                        case "$2" in
                                "") echo "no arguments"; shift 2 ;;
@@ -35,14 +46,40 @@ while true ; do
        esac
 done
  
-if [ ! $PND ] || [ ! $EXENAME ]; then
-       echo "Usage: pnd_run.sh -p your.pnd -e executeable [-a \"(arguments)\"] [ -s \"cd to folder inside pnd\"] [-u (skip union)] [-b override BASENAME (name of mountpoint/appdata)]"
+if [ ! $PND ]; then
+       echo "Usage: pnd_run.sh -p your.pnd -e executeable [-a \"(arguments)\"] [ -s \"cd to folder inside pnd\"] [-u (skip union)] [-b override BASENAME (name of mountpoint/appdata)] [-x close x before launching(script needs to be started with nohup for this to work]"
        exit 1
 fi
+if [ $nox ]; then
+       applist=$(lsof /usr/lib/libX11.so.6 | awk '{print $1}'| sort | uniq)
+       whitelist=$(cat ~/pndtest/whitelist) #adjust this to a fixed whitelist, maybe in the config dir
+       filteredlist=$(echo -e "$applist\n\n$whitelist\n\n$whitelist" | sort | uniq -u) #whitelist appended two times so those items are always removed
+       if [ ${#filteredlist} -ge 1 ]; then
+               message=$(echo -e "The following applications are still running, are you sure you want to close x? \n$filteredlist")
+               echo -e “ae[34me[30m”
+               xmessage -center "$message", -buttons yes,no
+               if [ $? = 102 ]; then
+               exit 1
+               fi
+               sudo /etc/init.d/gdm stop
+               sleep 5s
+       else
+               echo -e “ae[34me[30m”
+               xmessage -center "killing x, nothing of value will be lost", -buttons ok,cancel
+               if [ $? = 102 ]; then
+               exit 1
+               fi
+               # close x now, do we want to use gdm stop or just kill x?
+               sudo /etc/init.d/gdm stop
+               sleep 5s
+       fi
+fi
  
 #vars
 DFS=$(file -b $PND | awk '{ print $1 }') #is -p a zip/iso or folder?
-MOUNTPOINT=$(df $PND | grep -vE '^Filesystem' | awk '{ print $6  }') #find out which mountpoint the pnd/folder is on, there probably is a better way to do this
+MOUNTPOINT=$(df $PND | sed -ne 's/.*\% \(\S*\)/\1/p' | tail -n1)
+echo "mountpoint: $MOUNTPOINT"
+#MOUNTPOINT=$(df -h $PND | grep -E '[1-9]%' | awk '{ print $6  }') #find out which mountpoint the pnd/folder is on, there probably is a better way to do this
  
  
 #BASENAME really should be something sensible and somewhat unique
@@ -57,7 +94,14 @@ oCWD=$(pwd)
  
 #detect fs
 if [ $DFS = ISO ]; then
-       mntline="sudo mount -o loop,mode=777 $PND /mnt/pnd/$BASENAME"
+       usedminor=$( ls -l /dev/loop* | awk '{print $6}')
+       freeminor=$( echo -e "$(seq 0 64)\n$usedminor" | sort -rn | uniq -u | tail -n1)
+       sudo mknod -m777 /dev/loop$freeminor b 7 $freeminor
+       sudo losetup /dev/loop$freeminor $PND
+       mntline="sudo mount /dev/loop$freeminor /mnt/pnd/$BASENAME/"
+#      mntline="sudo mount -o loop,mode=777 $PND /mnt/pnd/$BASENAME"
        echo "Filetype is $DFS"
 elif [ $DFS = Zip ]; then
        mntline="fuse-zip $PND /mnt/pnd/$BASENAME -o ro,fmask=000" #TOTALLY untested right now
@@ -82,6 +126,7 @@ if [ ! $NOUNION ] && [ ! $umount ]; then
        #is the union already mounted? if not mount evrything, else launch the stuff
        mount | grep "on /mnt/utmp/$BASENAME type" # > /dev/null
        if [ ! $? -eq 0 ]; then 
+               echo "$mntline"
                $mntline #mount the pnd/folder
                echo "mounting union!"
                sudo mount -t aufs -o exec,dirs\=$MOUNTPOINT/appdata/$BASENAME=rw+nolwh:/mnt/pnd/$BASENAME=rr none /mnt/utmp/$BASENAME # put union on top
@@ -95,11 +140,12 @@ if [ ! $NOUNION ] && [ ! $umount ]; then
        #start app
        cd /mnt/utmp/$BASENAME
        if [ $STARTDIR ]; then cd $STARTDIR; fi #cd to folder specified by the optional arg -s
-       ./$EXENAME $ARGUMENTS
-#the app could have exited now, OR it went into bg, we still need to wait in that case till it really quits!
+       #echo "/lib/ld-linux.so.2 --library-path /mnt/utmp/$BASENAME/ $EXENAME $ARGUMENTS"
+       #/lib/ld-linux.so.2 --library-path /mnt/utmp/$BASENAME/ $EXENAME $ARGUMENTS
+       LD_LIBRARY_PATH=/mnt/utmp/$BASENAME ./$EXENAME $ARGUMENTS
+       #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`
-       while [ "$PID" -gt 0 ]
+       while [ "$PID" ]
        do
        sleep 10s
        PID=`pidof -o %PPID -x $EXENAME`
@@ -115,11 +161,12 @@ elif [ ! $umount ]; then
        cd /mnt/pnd/$BASENAME
        if [ $STARTDIR ]; then cd $STARTDIR; fi
        echo $(pwd)
+       #/lib/ld-linux.so.2 --library-path /mnt/pnd/$BASENAME/ $EXENAME $ARGUMENTS      
        ./$EXENAME $ARGUMENTS 
-#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=/mnt/pnd/$BASENAME ./$EXENAME $ARGUMENTS
+       #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`
-       while [ "$PID" -gt 0 ]
+       while [ $PID ]
        do
        sleep 10s
        PID=`pidof -o %PPID -x $EXENAME`
@@ -133,15 +180,25 @@ fi
  
  
 #clean up
 if [ ! $NOUNION ] ; then sudo umount /mnt/utmp/$BASENAME; fi #umount union if -u wasnt set
 if [ $NOUNION ] ; then sudo umount /mnt/pnd/$BASENAME; fi #umount iso if -u WAS set
-if [ $? -eq 0 ]; then # check if the umount was successfull, if it wasnt it would mean that theres still something running so we skip this stuff
+if [ $? -eq 0 ]; then # check if the umount was successfull, if it wasnt it would mean that theres still something running so we skip this stuff, this WILL lead to clutter if it happens, so we should make damn sure it never happens
        if [ ! $NOUNION ] ; then
                sudo umount /mnt/pnd/$BASENAME
                sudo rmdir $MOUNTPOINT/appdata/$BASENAME/.wh..wh.plink 
                sudo rmdir $MOUNTPOINT/appdata/$BASENAME/
                sudo rmdir /mnt/utmp/$BASENAME;
        fi
+       if [ $DFS = ISO ]; then
+               sudo losetup -d /dev/loop$freeminor
+               sudo rm /dev/loop$freeminor
+       fi
        sudo rmdir /mnt/pnd/$BASENAME 
-fi
\ No newline at end of file
+fi
+if [ $nox ]; then
+echo "starting x in 5s"
+sleep 5
+sudo /etc/init.d/gdm start
+fi
+
+