pndnotifyd: fix some crashes
[pandora-libraries.git] / testdata / scripts / pnd_make.sh
1 #!/bin/bash
2 #
3 # pnd_make.sh
4 #
5 # This script is meant to ease generation of a pnd file. Please consult the output
6 # when running --help for a list of available parameters and an explaination of
7 # those.
8 #
9 # Required tools when running the script:
10 # bash
11 # echo, cat, mv, rm
12 # mkisofs or mksquashfs (the latter when using the -c param!)
13 # xmllint (optional, only for validation of the PXML against the schema)
14
15
16 PXML_schema=$(dirname ${0})/PXML_schema.xsd
17 GENPXML_PATH=$(dirname ${0})/genpxml.sh
18
19 # useful functions ...
20 black='\E[30m'
21 red='\E[31m'
22 green='\E[32m'
23 yellow='\E[33m'
24 blue='\E[34m'
25 magenta='\E[35m'
26 cyan='\E[36m'
27 white='\E[37m'
28
29 check_for_tool()
30 {
31         which $1 &> /dev/null
32         if [ "$?" -ne "0" ];
33         then
34                 cecho "ERROR: Could not find the program '$1'. Please make sure
35 that it is available in your PATH since it is required to complete your request." $red
36                 exit 1
37         fi
38 }
39
40 cecho ()        # Color-echo. Argument $1 = message, Argument $2 = color
41 {
42         local default_msg="No message passed."   # Doesn't really need to be a local variable.
43         message=${1:-$default_msg}               # Defaults to default message.
44         color=${2:-$black}                       # Defaults to black, if not specified.
45         echo -e "$color$message"
46         tput sgr0                                # Reset to normal.
47         return
48
49
50
51 print_help()
52 {
53         cat << EOSTREAM
54 pnd_make.sh - A script to package "something" into a PND.
55
56 Usage:
57   $(basename ${0}) {--directory|-d} <folder> {--pndname|-p} <file> [{--compress-squashfs|-c}]
58                    [{--genpxml} <file>] [{--icon|-i} <file>] [{--pxml|-x} <file>]
59                    [{--schema|-s} <file>] [{--help|-h}]
60
61
62 Switches:
63   --compress-squashfs / -c  Define whether or not the pnd should be compressed using
64                             squashfs. If this parameter is selected, a compressed pnd
65                             will be created.
66
67   --compress-squashfs-xz    Define whether or not the pnd should be compressed using
68     / -cx                   squashfs with xz compression. Note that older firmwares
69                             will be unable to start such .pnd files.
70
71   --directory / -d          Sets the folder that is to be used for the resulting pnd
72                             to <folder>. This option is mandatory for the script to
73                             function correctly.
74
75   --genpxml                 Sets the script used for generating a PXML file (if none
76                             is available already) to <file>. Please make sure to either
77                             provide a full path or prefix a script in the current folder
78                             with './' so that the script can actually be executed. If
79                             this variable is not specified, $GENPXML_PATH
80                             will be used.
81
82   --help / -h               Displays this help text.
83
84   --icon / -i               Sets the icon that will be appended in the pnd to <file>.
85
86   --pndname / -p            Sets the output filename of the resulting pnd to <file>.
87                             This option is mandatory for the script to function
88                             correctly.
89
90   --pxml / -x               Sets the PXML file that is to be used to <file>. If you
91                             neither provide a PXML file or set this entry to 'guess',
92                             an existing 'PXML.xml' in your selected '--directory'
93                             will be used, or the script $GENPXML_PATH
94                             will be called to try to generate a basic PXML file for you.
95
96   --schema / -s             Sets the schema file, that is to be used for validation,
97                             to <file. If this is not defined, the script will try to
98                             use the file '$PXML_schema'. If this fails,
99                             a warning is issued.
100
101 If you select the option to create a compressed squashfs, a version >=4.0 of squashfs
102 is required to be available in your PATH.
103 EOSTREAM
104 }
105
106
107 # Parse command line parameters
108 while [ "${1}" != "" ]; do
109         if [ "${1}" = "--compress-squashfs" ] || [ "${1}" = "-c" ];
110         then
111                 SQUASH=1
112                 shift 1
113         elif [ "${1}" = "--compress-squashfs-xz" ] || [ "${1}" = "-cx" ];
114         then
115                 SQUASH=1
116                 SQUASHXZ=1
117                 shift 1
118         elif [ "${1}" = "--directory" ] || [ "${1}" = "-d" ];
119         then
120                 FOLDER=$2
121                 shift 2
122         elif [ "${1}" = "--genpxml" ];
123         then
124                 GENPXML_PATH=$2
125                 shift 2
126         elif [ "${1}" = "--help" ] || [ "${1}" = "-h" ];
127         then
128                 print_help
129                 exit 0
130         elif [ "${1}" = "--icon" ] || [ "${1}" = "-i" ];
131         then
132                 ICON=$2
133                 shift 2
134         elif [ "${1}" = "--pndname" ] || [ "${1}" = "-p" ];
135         then
136                 PNDNAME=$2
137                 shift 2
138         elif [ "${1}" = "--pxml" ] || [ "${1}" = "-x" ];
139         then
140                 PXML=$2
141                 shift 2
142         elif [ "${1}" = "--schema" ] || [ "${1}" = "-s" ]
143         then
144                 PXML_schema=$2
145                 shift 2
146         else
147                 cecho "ERROR: '$1' is not a known argument. Printing --help and aborting." $red
148                 print_help
149                 exit 1
150         fi
151 done
152
153
154 # Generate a PXML if the param is set to Guess or it is empty.
155 if [ ! $PXML ] || [ $PXML = "guess" ] && [ $PNDNAME ] && [ $FOLDER ];
156 then
157         if [ -f $FOLDER/PXML.xml ]; # use the already existing PXML.xml file if there is one...
158         then
159                 PXML=$FOLDER/PXML.xml
160                 PXML_ALREADY_EXISTING="true"
161         else
162                 if [ -f $GENPXML_PATH ];
163                 then
164                         $GENPXML_PATH --src $FOLDER --dest $FOLDER --author $USER
165                         if [ -f $FOLDER/PXML.xml ];
166                         then
167                                 PXML_GENERATED="true"
168                         else
169                                 cecho "ERROR: Generating a PXML file using '$GENPXML_PATH' failed.
170 Please generate a PXML file manually." $red
171                                 exit 1
172                         fi
173                 else
174                         cecho "ERROR: Could not find '$GENPXML_PATH' for generating a PXML file." $red
175                         exit 1
176                 fi
177         fi
178 fi
179
180
181 # Probe if required variables were set
182 echo -e
183 cecho "Checking if all required variables were set." $green
184 if [ ! $PNDNAME ] || [ ! $FOLDER ] || [ ! $PXML ];
185 then
186         echo -e
187         cecho "ERROR: Not all required options were set! Please see the --help information below." $red
188         echo -e
189         print_help
190         exit 1
191 else
192         echo "PNDNAME set to '$PNDNAME'."
193 fi
194 # Check if the selected folder actually exists
195 if [ ! -d $FOLDER ];
196 then
197         echo -e
198         cecho "ERROR: '$FOLDER' doesn't exist or is not a folder." $red
199         exit 1
200 else
201         echo "FOLDER set to '$FOLDER'."
202 fi
203 # Check if the selected PXML file actually exists
204 if [ ! -f $PXML ];
205 then
206         echo -e
207         cecho "ERROR: '$PXML' doesn't exist or is not a file." $red
208         exit 1
209 else
210         if [ $PXML_ALREADY_EXISTING ];
211         then
212                 echo "You have not explicitly specified a PXML to use, but an existing file was
213 found. Will be using this one."
214         elif [ $PXML_GENERATED ];
215         then
216                 echo "A PXML file was generated for you using '$GENPXML_PATH'. This file will
217 not be removed at the end of this script because you might want to review it, adjust
218 single entries and rerun the script to generate a pnd with a PXML file with all the
219 information you want to have listed."
220         fi
221         echo "PXML set to '$PXML'."
222 fi
223
224 # Print the other variables:
225 if [ $ICON ];
226 then
227         if [ ! -f $ICON ]
228         then
229                 cecho "WARNING: '$ICON' doesn't exist, will not append the selected icon to the pnd." $red
230         else
231                 echo "ICON set to '$ICON'."
232                 USE_ICON="true"
233         fi
234 fi
235 if [ $SQUASH ];
236 then
237         echo "Will use a squashfs for '$PNDNAME'."
238 fi
239
240
241 # Validate the PXML file (if xmllint is available)
242 # Errors and problems in this section will be shown but are not fatal.
243 echo -e
244 cecho "Trying to validate '$PXML' now. Will be using '$PXML_schema' to do so." $green
245 which xmllint &> /dev/null
246 if [ "$?" -ne "0" ];
247 then
248         VALIDATED=false
249         cecho "WARNING: Could not find 'xmllint'. Validity check of '$PXML' is not possible!" $red
250 else
251         if [ ! -f "$PXML_schema" ];
252         then
253                 VALIDATED=false
254                 cecho "WARNING: Could not find '$PXML_schema'. If you want to validate your
255 PXML file please make sure to provide a schema using the --schema option." $red
256         else
257                 xmllint --noout --schema $PXML_schema $PXML
258                 if [ "$?" -ne "0" ]; then VALIDATED=false; else VALIDATED=true; fi
259         fi
260 fi
261 # Print some message at the end about the validation in case the user missed the output above
262 if [ $VALIDATED = "false" ]
263 then
264         cecho "WARNING: Could not successfully validate '$PXML'. Please check the output
265 above. This does not mean that your pnd will be broken. Either you are not following the strict
266 syntax required for validation or you don't have all files/programs required for validating." $red
267 else
268         cecho "Your file '$PXML' was validated successfully. The resulting pnd should
269 work nicely with libpnd." $green
270 fi
271
272
273 # Make iso from folder
274 echo -e
275 cecho "Creating an iso file based on '$FOLDER'." $green
276 if [ $SQUASH ];
277 then
278         check_for_tool mksquashfs
279         if [ $(mksquashfs -version | awk 'BEGIN{r=0} $3>=4{r=1} END{print r}') -eq 0 ];
280         then
281                 cecho "ERROR: Your squashfs version is older then version 4, please upgrade to 4.0 or later" $red
282                 exit 1
283         fi
284         if [ $SQUASHXZ ];
285         then
286                 mksquashfs $FOLDER $PNDNAME.iso -all-root -force-gid 0 -comp xz -Xbcj arm,armthumb
287         else
288                 mksquashfs $FOLDER $PNDNAME.iso -all-root -force-gid 0
289         fi
290 else
291         check_for_tool mkisofs
292         mkisofs -o $PNDNAME.iso -R $FOLDER
293 fi
294
295 # Check that the iso file was actually created before continuing
296 if [ ! -f $PNDNAME.iso ];
297 then
298         cecho "ERROR: The temporary file '$PNDNAME.iso' could not be created.
299 Please check the output above for any errors and retry after fixing them. Aborting." $red
300         exit 1
301 fi
302
303
304 # Append pxml to iso
305 echo -e
306 cecho "Appending '$PXML' to the created iso file." $green
307 cat $PNDNAME.iso $PXML > $PNDNAME
308 rm $PNDNAME.iso #cleanup
309
310
311 # Append icon if specified and available
312 if [ $USE_ICON ];
313 then
314         echo -e
315         cecho "Appending the icon '$ICON' to the pnd." $green
316         mv $PNDNAME $PNDNAME.tmp
317         cat $PNDNAME.tmp $ICON > $PNDNAME # append icon
318         rm $PNDNAME.tmp #cleanup
319 fi
320
321
322 # Final message
323 echo -e
324 if [ -f $PNDNAME ];
325 then
326         cecho "Successfully finished creating the pnd '$PNDNAME'." $green
327 else
328         cecho "There seems to have been a problem and '$PNDNAME' was not created. Please check
329 the output above for any error messages. A possible cause for this is that there was
330 not enough space available." $red
331         exit 1
332 fi
333
334
335 #if [ $PXML = "guess" ];then rm $FOLDER/PXML.xml; fi #cleanup