Merged updated scripts from vimacs, updated Makefile with some deployment rules
[pandora-libraries.git] / testdata / scripts / pnd_run.sh
1 #!/bin/bash
2  
3 #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)]
4 # -n to skip union mount, should probably be removed before release
5 # -s startdir
6 # arguments can be inside -e, -a is optional
7  
8 #/etc/sudoers needs to be adjusted if you touch any of the sudo lines in the wrong place.
9  
10  
11 # parse arguments
12 TEMP=`getopt -o p:e:a:b:s:m::u::n:: -- "$@"`
13  
14 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
15  
16 # Note the quotes around `$TEMP': they are essential!
17 eval set -- "$TEMP"
18  
19 while true ; do
20         case "$1" in
21                 -p) echo "pnd set to \`$2'" ;PND=$2;shift 2;;
22                 -e) echo "exec set to \`$2'" ;EXENAME=$2;shift 2 ;;
23                 -n) echo "u set, no union pls!";NOUNION=1;shift 2;;
24                 -b) echo "BASENAME set to $2";BASENAME=$2;shift 2;;
25                 -s) echo "startdir set to $2";STARTDIR=$2;shift 2;;
26                 -m) echo "mount";mount=1;shift 2;;
27                 -u) echo "umount";umount=1;shift 2;;
28                 -a) 
29                         case "$2" in
30                                 "") echo "no arguments"; shift 2 ;;
31                                 *)  echo "args set to \`$2'" ;ARGUMENTS=$2;shift 2 ;;
32                         esac ;;
33                 --) shift ; break ;;
34                 *) echo "Error while parsing arguments!" ; exit 1 ;;
35         esac
36 done
37  
38 if [ ! $PND ] || [ ! $EXENAME ]; then
39         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)]"
40         exit 1
41 fi
42  
43 #vars
44 DFS=$(file -b $PND | awk '{ print $1 }') #is -p a zip/iso or folder?
45 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
46  
47  
48 #BASENAME really should be something sensible and somewhat unique
49 #if -b is set use that as basename, else generate it from PND
50 #get basename (strip extension if file) for union mountpoints etc, maybe  this should be changed to something specified inside the xml
51 #this should probably be changed to .... something more sensible
52 if [ ! $BASENAME ]; then BASENAME=$(basename "$PND" | cut -d'.' -f1) ; fi
53  
54  
55  
56 oCWD=$(pwd)
57  
58 #detect fs
59 if [ $DFS = ISO ]; then
60         mntline="sudo mount -o loop,mode=777 $PND /mnt/pnd/$BASENAME"
61         echo "Filetype is $DFS"
62 elif [ $DFS = Zip ]; then
63         mntline="fuse-zip $PND /mnt/pnd/$BASENAME -o ro,fmask=000" #TOTALLY untested right now
64         echo "Filetype is $DFS"
65 elif [ $DFS = directory ]; then
66         mntline="sudo mount --bind -o ro $PND /mnt/pnd/$BASENAME"
67 #we bind the folder, now it can be treated in a unified way ATENTION: -o ro doesnt work for --bind at least on 25, on 26 its possible using remount, may have changed on 27
68         echo "Filetype is $DFS"
69 else
70         echo "error"
71         exit 1;
72 fi
73  
74 #create mountpoints, check if they exist already first to avoid annoying error messages
75 if [ ! -d /mnt/pnd/$BASENAME ]; then sudo mkdir -p /mnt/pnd/$BASENAME ; fi
76 if [ ! -d $MOUNTPOINT/appdata/$BASENAME ]; then sudo mkdir -p $MOUNTPOINT/appdata/$BASENAME; fi
77 if [ ! -d /mnt/utmp/$BASENAME ]; then sudo mkdir -p /mnt/utmp/$BASENAME; fi 
78  
79 #mount
80  
81 if [ ! $NOUNION ] && [ ! $umount ]; then
82         #is the union already mounted? if not mount evrything, else launch the stuff
83         mount | grep "on /mnt/utmp/$BASENAME type" # > /dev/null
84         if [ ! $? -eq 0 ]; then 
85                 $mntline #mount the pnd/folder
86                 echo "mounting union!"
87                 sudo mount -t aufs -o exec,dirs\=$MOUNTPOINT/appdata/$BASENAME=rw+nolwh:/mnt/pnd/$BASENAME=rr none /mnt/utmp/$BASENAME # put union on top
88  
89         else
90                 echo "Union already mounted"
91         fi
92  
93         if [ $mount ]; then echo "mounted /mnt/utmp/$BASENAME"; exit 1; fi;
94  
95         #start app
96         cd /mnt/utmp/$BASENAME
97         if [ $STARTDIR ]; then cd $STARTDIR; fi #cd to folder specified by the optional arg -s
98         ./$EXENAME $ARGUMENTS
99  
100 #the app could have exited now, OR it went into bg, we still need to wait in that case till it really quits!
101         PID=`pidof -o %PPID -x $EXENAME`
102         while [ "$PID" -gt 0 ]
103         do
104         sleep 10s
105         PID=`pidof -o %PPID -x $EXENAME`
106         done
107         echo end
108  
109         #app exited
110         cd $oCWD #cd out of the mountpoint so we can umount, doesnt really matter to where...
111  
112 elif [ ! $umount ]; then
113         $mntline
114         if [ $mount ]; then echo "mounted /mnt/pnd/$BASENAME"; exit 1; fi;
115         cd /mnt/pnd/$BASENAME
116         if [ $STARTDIR ]; then cd $STARTDIR; fi
117         echo $(pwd)
118         ./$EXENAME $ARGUMENTS 
119  
120 #the app could have exited now, OR it went into bg, we still need to wait in that case till it really quits!
121         PID=`pidof -o %PPID -x $EXENAME`
122         while [ "$PID" -gt 0 ]
123         do
124         sleep 10s
125         PID=`pidof -o %PPID -x $EXENAME`
126         done
127         echo end
128  
129         cd $oCWD
130 else
131 echo "-u set, nothing to do here"
132 fi
133  
134  
135 #clean up
136  
137 if [ ! $NOUNION ] ; then sudo umount /mnt/utmp/$BASENAME; fi #umount union if -u wasnt set
138 if [ $NOUNION ] ; then sudo umount /mnt/pnd/$BASENAME; fi #umount iso if -u WAS set
139 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
140         if [ ! $NOUNION ] ; then
141                 sudo umount /mnt/pnd/$BASENAME
142                 sudo rmdir $MOUNTPOINT/appdata/$BASENAME/.wh..wh.plink 
143                 sudo rmdir $MOUNTPOINT/appdata/$BASENAME/
144                 sudo rmdir /mnt/utmp/$BASENAME;
145         fi
146         sudo rmdir /mnt/pnd/$BASENAME 
147 fi