c645a29018de4b9066c4a239fae6bb4a8ce99703
[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 #TODO: make sure this does still work nicely with the latest genpxml that sebt3 is working on!
147 if [ ! $PXML ] || [ $PXML = "guess" ] && [ $PNDNAME ] && [ $FOLDER ];
148 then
149         if [ -f $FOLDER/PXML.xml ]; # use the already existing PXML.xml file if there is one...
150         then
151                 PXML=$FOLDER/PXML.xml
152                 PWML_ALREADY_EXISTING="true"
153         else
154                 if [ -f $GENPXML_PATH ];
155                 then
156                         PXMLtxt=$($GENPXML_PATH $FOLDER $ICON)
157                         if [ -z $PXMLtxt ];
158                         then
159                                 cecho "ERROR: Generating a PXML file using '$GENPXML_PATH' failed.
160 Please generate a PXML file manually." $red
161                                 exit 1
162                         else
163                                 PXML=$FOLDER/PXML.xml
164                                 echo "$PXMLtxt" > $FOLDER/PXML.xml
165                                 PXML_GENERATED="true"
166                         fi
167                 else
168                         cecho "ERROR: Could not find '$GENPXML_PATH' for generating a PXML file." $red
169                         exit 1
170                 fi
171         fi
172 fi
173
174
175 # Probe if required variables were set
176 echo -e
177 cecho "Checking if all required variables were set." $green
178 if [ ! $PNDNAME ] || [ ! $FOLDER ] || [ ! $PXML ];
179 then
180         echo -e
181         cecho "ERROR: Not all required options were set! Please see the --help information below." $red
182         echo -e
183         print_help
184         exit 1
185 else
186         echo "PNDNAME set to '$PNDNAME'."
187 fi
188 # Check if the selected folder actually exists
189 if [ ! -d $FOLDER ];
190 then
191         echo -e
192         cecho "ERROR: '$FOLDER' doesn't exist or is not a folder." $red
193         exit 1
194 else
195         echo "FOLDER set to '$FOLDER'."
196 fi
197 # Check if the selected PXML file actually exists
198 if [ ! -f $PXML ];
199 then
200         echo -e
201         cecho "ERROR: '$PXML' doesn't exist or is not a file." $red
202         exit 1
203 else
204         if [ $PWML_ALREADY_EXISTING ];
205         then
206                 echo "You have not explicitly specified a PXML to use, but an existing file was
207 found. Will be using this one."
208         elif [ $PXML_GENERATED ];
209         then
210                 echo "A PXML file was generated for you using '$GENPXML_PATH'. This file will
211 not be removed at the end of this script because you might want to review it, adjust
212 single entries and rerun the script to generate a pnd with a PXML file with all the
213 information you want to have listed."
214         fi
215         echo "PXML set to '$PXML'."
216 fi
217
218 # Print the other variables:
219 if [ $ICON ];
220 then
221         if [ ! -f $ICON ]
222         then
223                 cecho "WARNING: '$ICON' doesn't exist, will not append the selected icon to the pnd." $red
224         else
225                 echo "ICON set to '$ICON'."
226                 USE_ICON="true"
227         fi
228 fi
229 if [ $SQUASH ];
230 then
231         echo "Will use a squashfs for '$PNDNAME'."
232 fi
233
234
235 # Validate the PXML file (if xmllint is available)
236 # Errors and problems in this section will be shown but are not fatal.
237 echo -e
238 cecho "Trying to validate '$PXML' now. Will be using '$PXML_schema' to do so." $green
239 which xmllint &> /dev/null
240 if [ "$?" -ne "0" ];
241 then
242         VALIDATED=false
243         cecho "WARNING: Could not find 'xmllint'. Validity check of '$PXML' is not possible!" $red
244 else
245         if [ ! -f "$PXML_schema" ];
246         then
247                 VALIDATED=false
248                 cecho "WARNING: Could not find '$PXML_schema'. If you want to validate your
249 PXML file please make sure to provide a schema using the --schema option." $red
250         else
251                 xmllint --noout --schema $PXML_schema $PXML
252                 if [ "$?" -ne "0" ]; then VALIDATED=false; else VALIDATED=true; fi
253         fi
254 fi
255 # Print some message at the end about the validation in case the user missed the output above
256 if [ $VALIDATED = "false" ]
257 then
258         cecho "WARNING: Could not successfully validate '$PXML'. Please check the output
259 above. This does not mean that your pnd will be broken. Either you are not following the strict
260 syntax required for validation or you don't have all files/programs required for validating." $red
261 else
262         cecho "Your file '$PXML' was validated successfully. The resulting pnd should
263 work nicely with libpnd." $green
264 fi
265
266
267 # Make iso from folder
268 echo -e
269 cecho "Creating an iso file based on '$FOLDER'." $green
270 if [ $SQUASH ];
271 then
272         check_for_tool mksquashfs
273         if [ $(mksquashfs -version | awk 'BEGIN{r=0} $3>=4{r=1} END{print r}') -eq 0 ];
274         then
275                 cecho "ERROR: Your squashfs version is older then version 4, please upgrade to 4.0 or later" $red
276                 exit 1
277         fi
278         mksquashfs $FOLDER $PNDNAME.iso -nopad -no-recovery
279 else
280         check_for_tool mkisofs
281         mkisofs -o $PNDNAME.iso -R $FOLDER
282 fi
283
284 # Check that the iso file was actually created before continuing
285 if [ ! -f $PNDNAME.iso ];
286 then
287         cecho "ERROR: The temporary file '$PNDNAME.iso' could not be created.
288 Please check the output above for any errors and retry after fixing them. Aborting." $red
289         exit 1
290 fi
291
292
293 # Append pxml to iso
294 echo -e
295 cecho "Appending '$PXML' to the created iso file." $green
296 cat $PNDNAME.iso $PXML > $PNDNAME
297 rm $PNDNAME.iso #cleanup
298
299
300 # Append icon if specified and available
301 if [ $USE_ICON ];
302 then
303         echo -e
304         cecho "Appending the icon '$ICON' to the pnd." $green
305         mv $PNDNAME $PNDNAME.tmp
306         cat $PNDNAME.tmp $ICON > $PNDNAME # append icon
307         rm $PNDNAME.tmp #cleanup
308 fi
309
310
311 # Final message
312 echo -e
313 if [ -f $PNDNAME ];
314 then
315         cecho "Successfully finished creating the pnd '$PNDNAME'." $green
316 else
317         cecho "There seems to have been a problem and '$PNDNAME' was not created. Please check
318 the output above for any error messages. A possible cause for this is that there was
319 not enough space available." $red
320         exit 1
321 fi
322
323
324 #if [ $PXML = "guess" ];then rm $FOLDER/PXML.xml; fi #cleanup