Fixed getopt in pnd_run.sh and eliminated unused var
[pandora-libraries.git] / testdata / scripts / pnd_run.sh
1 #!/bin/bash
2  
3 #input pnd_run.sh -p "/path/to/foobar.pnd" -e "exe" --a "arguments for exe"
4 # -u to skip union mount
5 # -s startdir
6 # arguments can be inside -e, -a is optional
7  
8 ############################# dont forget to remove the echos! ############################
9  
10 # parse arguments
11 TEMP=`getopt -o p:e:a:b:u::s: --long p-long,e-long:,a-long: -- "$@"`
12  
13 if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
14  
15 # Note the quotes around `$TEMP': they are essential!
16 eval set -- "$TEMP"
17  
18 while true ; do
19         case "$1" in
20                 -p|--p-long) echo "pnd set to \`$2'" ;PND=$2;shift 2;;
21                 -e|--e-long) echo "exec set to \`$2'" ;EXENAME=$2;shift 2 ;;
22                 -u) echo "u set, no union pls!";UNION=1;shift 2 ;;
23                 -b) echo "BASENAME set to $2";BASENAME=$2;shift 2;;
24                 -s) echo "startdir set to $2";STARTDIR=$2;shift 2;;
25                 -a|--a-long) 
26                         case "$2" in
27                                 "") echo "no arguments"; shift 2 ;;
28                                 *)  echo "args set to \`$2'" ;ARGUMENTS=$2;shift 2 ;;
29                         esac ;;
30                 --) shift ; break ;;
31                 *) echo "Error while parsing arguments!" ; exit 1 ;;
32         esac
33 done
34  
35 # add sanity check
36  
37 #vars
38 DFS=$(file -b $PND | awk '{ print $1 }') # is -p a zip iso or folder?
39 MOUNTPOINT=$(df $PND | grep -vE '^Filesystem' | awk '{ print $6  }') #find out which mountpoint the pnd/folder is on
40  
41 #if -b is set use that as basename, else generate it from PND
42 #get basename (strip extension if file) for union mountpoints etc, maybe  this should be changed to something specified inside the xml
43 if [ ! $BASENAME ]; then BASENAME=$(basename "$PND" | cut -d'.' -f1) ; fi
44  
45 oCWD=$(pwd)
46 # add sanity check
47  
48 #detect fs
49 if [ $DFS = ISO ]; then
50         mntline="mount -o loop,fmask=000 $PND /mnt/pnd/$BASENAME"
51         echo "Filetype is $DFS"
52 elif [ $DFS = Zip ]; then
53         mntline="fuse-zip $PND /mnt/pnd/$BASENAME -o ro,fmask=000"
54         echo "Filetype is $DFS"
55 elif [ $DFS = directory ]; then
56         mntline="mount --bind -o ro,fmask=000 $PND /mnt/pnd/$BASENAME"
57 #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
58         echo "Filetype is $DFS"
59 else
60         echo "error"
61         exit 1;
62 fi
63  
64 #create mountpoints
65  
66 if [ ! -d /mnt/pnd/$BASENAME ]; then echo "mkdir -p /mnt/pnd/$BASENAME "; fi
67 if [ ! -d $MOUNTPOINT/appdata/$BASENAME ]; then echo "mkdir -p $MOUNTPOINT/appdata/$BASENAME "; fi
68 if [ ! -d /mnt/utmp/$BASENAME ]; then echo "mkdir -p /mnt/utmp/$BASENAME "; fi 
69  
70 #mount
71 if [ ! $UNION ] ; then
72         #is the union already mounted? if not mount evrything, else launch the stuff
73         mount | grep "on /mnt/utmp/$BASENAME type" > /dev/null
74         if [ ! $? -eq 0 ]; then 
75  
76                 $mntline #mount the pnd/folder
77                 #mount -t unionfs -o exec,dirs\=$MOUNTPOINT/appdata/$BASENAME=rw:/mnt/pnd/$BASENAME=ro unionfs /mnt/utmp/$BASENAME #union mount
78                 #aufs, one of those should work, bit unsure.
79                 mount -t aufs -o exec,fmask=000,dirs\=$MOUNTPOINT/appdata/$BASENAME=rw:/mnt/pnd/$BASENAME=ro none /mnt/utmp/$BASENAME #aufs?
80                 #mount -t aufs -o exec,dirs\=$MOUNTPOINT/appdata/$BASENAME=rw:/mnt/pnd/$BASENAME=ro aufs /mnt/utmp/$BASENAME #aufs?
81  
82         else
83                 echo "doh!"
84         fi
85  
86  
87         #start app
88         cd /mnt/utmp/$BASENAME
89         if [ $STARTDIR ]; then cd $STARTDIR; fi
90         $EXENAME $ARGUMENTS 
91         #app exited
92  
93 else
94  
95         $mntline
96         cd /mnt/pnd/$BASENAME
97         if [ $STARTDIR ]; then cd $STARTDIR; fi
98         $EXENAME $ARGUMENTS 
99 fi
100  
101 #clean up
102 umount /mnt/utmp/$BASENAME
103 if [ $? -eq 0 ]; then 
104  
105         umount /mnt/pnd/$BASENAME
106         rmdir /mnt/pnd/$BASENAME
107         rmdir /mnt/utmp/$BASENAME
108  
109 fi