b1828199da61d8059f7ea4d77be0ef7b40a238e6
[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   --directory / -d          Sets the folder that is to be used for the resulting pnd
68                             to <folder>. This option is mandatory for the script to
69                             function correctly.
70
71   --genpxml                 Sets the script used for generating a PXML file (if none
72                             is available already) to <file>. Please make sure to either
73                             provide a full path or prefix a script in the current folder
74                             with './' so that the script can actually be executed. If
75                             this variable is not specified, $GENPXML_PATH
76                             will be used.
77
78   --help / -h               Displays this help text.
79
80   --icon / -i               Sets the icon that will be appended in the pnd to <file>.
81
82   --pndname / -p            Sets the output filename of the resulting pnd to <file>.
83                             This option is mandatory for the script to function
84                             correctly.
85
86   --pxml / -x               Sets the PXML file that is to be used to <file>. If you
87                             neither provide a PXML file or set this entry to 'guess',
88                             an existing 'PXML.xml' in your selected '--directory'
89                             will be used, or the script $GENPXML_PATH
90                             will be called to try to generate a basic PXML file for you.
91
92   --schema / -s             Sets the schema file, that is to be used for validation,
93                             to <file. If this is not defined, the script will try to
94                             use the file '$PXML_schema'. If this fails,
95                             a warning is issued.
96
97 If you select the option to create a compressed squashfs, a version >=4.0 of squashfs
98 is required to be available in your PATH.
99 EOSTREAM
100 }
101
102
103 # Parse command line parameters
104 while [ "${1}" != "" ]; do
105         if [ "${1}" = "--compress-squashfs" ] || [ "${1}" = "-c" ];
106         then
107                 SQUASH=1
108                 shift 1
109         elif [ "${1}" = "--directory" ] || [ "${1}" = "-d" ];
110         then
111                 FOLDER=$2
112                 shift 2
113         elif [ "${1}" = "--genpxml" ];
114         then
115                 GENPXML_PATH=$2
116                 shift 2
117         elif [ "${1}" = "--help" ] || [ "${1}" = "-h" ];
118         then
119                 print_help
120                 exit 0
121         elif [ "${1}" = "--icon" ] || [ "${1}" = "-i" ];
122         then
123                 ICON=$2
124                 shift 2
125         elif [ "${1}" = "--pndname" ] || [ "${1}" = "-p" ];
126         then
127                 PNDNAME=$2
128                 shift 2
129         elif [ "${1}" = "--pxml" ] || [ "${1}" = "-x" ];
130         then
131                 PXML=$2
132                 shift 2
133         elif [ "${1}" = "--schema" ] || [ "${1}" = "-f" ]
134         then
135                 PXML_schema=$2
136                 shift 2
137         else
138                 cecho "ERROR: '$1' is not a known argument. Printing --help and aborting." $red
139                 print_help
140                 exit 1
141         fi
142 done
143
144
145 # Generate a PXML if the param is set to Guess or it is empty.
146 if [ ! $PXML ] || [ $PXML = "guess" ] && [ $PNDNAME ] && [ $FOLDER ];
147 then
148         if [ -f $FOLDER/PXML.xml ]; # use the already existing PXML.xml file if there is one...
149         then
150                 PXML=$FOLDER/PXML.xml
151                 PXML_ALREADY_EXISTING="true"
152         else
153                 if [ -f $GENPXML_PATH ];
154                 then
155                         $GENPXML_PATH --src $FOLDER --dest $FOLDER --author $USER
156                         if [ -f $FOLDER/PXML.xml ];
157                         then
158                                 PXML_GENERATED="true"
159                         else
160                                 cecho "ERROR: Generating a PXML file using '$GENPXML_PATH' failed.
161 Please generate a PXML file manually." $red
162                                 exit 1
163                         fi
164                 else
165                         cecho "ERROR: Could not find '$GENPXML_PATH' for generating a PXML file." $red
166                         exit 1
167                 fi
168         fi
169 fi
170
171
172 # Probe if required variables were set
173 echo -e
174 cecho "Checking if all required variables were set." $green
175 if [ ! $PNDNAME ] || [ ! $FOLDER ] || [ ! $PXML ];
176 then
177         echo -e
178         cecho "ERROR: Not all required options were set! Please see the --help information below." $red
179         echo -e
180         print_help
181         exit 1
182 else
183         echo "PNDNAME set to '$PNDNAME'."
184 fi
185 # Check if the selected folder actually exists
186 if [ ! -d $FOLDER ];
187 then
188         echo -e
189         cecho "ERROR: '$FOLDER' doesn't exist or is not a folder." $red
190         exit 1
191 else
192         echo "FOLDER set to '$FOLDER'."
193 fi
194 # Check if the selected PXML file actually exists
195 if [ ! -f $PXML ];
196 then
197         echo -e
198         cecho "ERROR: '$PXML' doesn't exist or is not a file." $red
199         exit 1
200 else
201         if [ $PXML_ALREADY_EXISTING ];
202         then
203                 echo "You have not explicitly specified a PXML to use, but an existing file was
204 found. Will be using this one."
205         elif [ $PXML_GENERATED ];
206         then
207                 echo "A PXML file was generated for you using '$GENPXML_PATH'. This file will
208 not be removed at the end of this script because you might want to review it, adjust
209 single entries and rerun the script to generate a pnd with a PXML file with all the
210 information you want to have listed."
211         fi
212         echo "PXML set to '$PXML'."
213 fi
214
215 # Print the other variables:
216 if [ $ICON ];
217 then
218         if [ ! -f $ICON ]
219         then
220                 cecho "WARNING: '$ICON' doesn't exist, will not append the selected icon to the pnd." $red
221         else
222                 echo "ICON set to '$ICON'."
223                 USE_ICON="true"
224         fi
225 fi
226 if [ $SQUASH ];
227 then
228         echo "Will use a squashfs for '$PNDNAME'."
229 fi
230
231
232 # Validate the PXML file (if xmllint is available)
233 # Errors and problems in this section will be shown but are not fatal.
234 echo -e
235 cecho "Trying to validate '$PXML' now. Will be using '$PXML_schema' to do so." $green
236 which xmllint &> /dev/null
237 if [ "$?" -ne "0" ];
238 then
239         VALIDATED=false
240         cecho "WARNING: Could not find 'xmllint'. Validity check of '$PXML' is not possible!" $red
241 else
242         if [ ! -f "$PXML_schema" ];
243         then
244                 VALIDATED=false
245                 cecho "WARNING: Could not find '$PXML_schema'. If you want to validate your
246 PXML file please make sure to provide a schema using the --schema option." $red
247         else
248                 xmllint --noout --schema $PXML_schema $PXML
249                 if [ "$?" -ne "0" ]; then VALIDATED=false; else VALIDATED=true; fi
250         fi
251 fi
252 # Print some message at the end about the validation in case the user missed the output above
253 if [ $VALIDATED = "false" ]
254 then
255         cecho "WARNING: Could not successfully validate '$PXML'. Please check the output
256 above. This does not mean that your pnd will be broken. Either you are not following the strict
257 syntax required for validation or you don't have all files/programs required for validating." $red
258 else
259         cecho "Your file '$PXML' was validated successfully. The resulting pnd should
260 work nicely with libpnd." $green
261 fi
262
263
264 # Make iso from folder
265 echo -e
266 cecho "Creating an iso file based on '$FOLDER'." $green
267 if [ $SQUASH ];
268 then
269         check_for_tool mksquashfs
270         if [ $(mksquashfs -version | awk 'BEGIN{r=0} $3>=4{r=1} END{print r}') -eq 0 ];
271         then
272                 cecho "ERROR: Your squashfs version is older then version 4, please upgrade to 4.0 or later" $red
273                 exit 1
274         fi
275         mksquashfs $FOLDER $PNDNAME.iso # -nopad -no-recovery
276 else
277         check_for_tool mkisofs
278         mkisofs -o $PNDNAME.iso -R $FOLDER
279 fi
280
281 # Check that the iso file was actually created before continuing
282 if [ ! -f $PNDNAME.iso ];
283 then
284         cecho "ERROR: The temporary file '$PNDNAME.iso' could not be created.
285 Please check the output above for any errors and retry after fixing them. Aborting." $red
286         exit 1
287 fi
288
289
290 # Append pxml to iso
291 echo -e
292 cecho "Appending '$PXML' to the created iso file." $green
293 cat $PNDNAME.iso $PXML > $PNDNAME
294 rm $PNDNAME.iso #cleanup
295
296
297 # Append icon if specified and available
298 if [ $USE_ICON ];
299 then
300         echo -e
301         cecho "Appending the icon '$ICON' to the pnd." $green
302         mv $PNDNAME $PNDNAME.tmp
303         cat $PNDNAME.tmp $ICON > $PNDNAME # append icon
304         rm $PNDNAME.tmp #cleanup
305 fi
306
307
308 # Final message
309 echo -e
310 if [ -f $PNDNAME ];
311 then
312         cecho "Successfully finished creating the pnd '$PNDNAME'." $green
313 else
314         cecho "There seems to have been a problem and '$PNDNAME' was not created. Please check
315 the output above for any error messages. A possible cause for this is that there was
316 not enough space available." $red
317         exit 1
318 fi
319
320
321 #if [ $PXML = "guess" ];then rm $FOLDER/PXML.xml; fi #cleanup