new pnd_make.sh with many changes (but still compatible when usign the
[pandora-libraries.git] / testdata / scripts / pnd_make.sh
1 #!/bin/bash
2
3 ######adjust path of genpxml.sh if you want to use that "feture"#####
4
5 PXML_schema="PXML_schema.xsd"
6 GENPXML_PATH="genpxml.sh"
7
8 # useful functions ...
9 black='\E[30m'
10 red='\E[31m'
11 green='\E[32m'
12 yellow='\E[33m'
13 blue='\E[34m'
14 magenta='\E[35m'
15 cyan='\E[36m'
16 white='\E[37m'
17
18
19 cecho ()        # Color-echo. Argument $1 = message, Argument $2 = color
20 {
21         local default_msg="No message passed."   # Doesn't really need to be a local variable.
22         message=${1:-$default_msg}               # Defaults to default message.
23         color=${2:-$black}                       # Defaults to black, if not specified.
24         echo -e "$color$message"
25         tput sgr0                                # Reset to normal.
26         return
27
28
29
30 print_help()
31 {
32         cat << EOSTREAM
33 pnd_make.sh - A script to package "something" into a PND.
34
35 Usage:
36   $(basename ${0}) {--directory|-d} <folder> {--pndname|-p} <file> [{--compress-squashfs|-c}]
37                    [{--genpxml} <file>] [{--icon|-i} <file>] [{--pxml|-x} <file>]
38                    [{--schema|-s} <file>] [{--help|-h}]
39
40
41 Switches:
42   --compress-squashfs / -c  Define wether or not the pnd should be compressed using
43                             squashfs. If this parameter is selected, a compressed pnd
44                             will be created.
45
46   --directory / -d          Sets the folder that is to be used for the resulting pnd
47                             to <folder>. This option is mandatory for the script to
48                             function correctly.
49
50   --genpxml                 
51
52   --help / -h               Displays this help text.
53
54   --icon / -i               Sets the icon that will be appended in the pnd to <file>.
55
56   --pndname / -p            Sets the output filename of the resulting pnd to <file>.
57                             This option is mandatory for the script to function
58                             correctly.
59
60   --pxml / -x               Sets the PXML file that is to be used to <file>. If you
61                             neither provide a PXML file or set this entry to 'guess',
62                             an existing 'PXML.xml' in your selected '--directory'
63                             will be used, or the script $GENPXML_PATH will be called
64                             to try to generate a basic PXML file for your.
65
66   --schema / -s             Sets the schema file, that is to be used for validation,
67                             to <file. If this is not defined, the script will try to
68                             use the file 'PXML_schema.xsd'. If this fails, a warning
69                             is issued.
70
71 If you select the option to create a compressed squashfs, a version >=4.0 of squashfs
72 is required to be available in your PATH.
73 EOSTREAM
74 }
75
76
77 # Parse command line parameters
78 while [ "${1}" != "" ]; do
79         if [ "${1}" = "--compress-squashfs" ] || [ "${1}" = "-c" ];
80         then
81                 SQUASH=1
82                 shift 1
83         elif [ "${1}" = "--directory" ] || [ "${1}" = "-d" ];
84         then
85                 FOLDER=$2
86                 shift 2
87         elif [ "${1}" = "--genpxml" ];
88         then
89                 GENPXML_PATH=$2
90                 shift 2
91         elif [ "${1}" = "--help" ] || [ "${1}" = "-h" ];
92         then
93                 print_help
94                 exit 0
95         elif [ "${1}" = "--icon" ] || [ "${1}" = "-i" ];
96         then
97                 ICON=$2
98                 shift 2
99         elif [ "${1}" = "--pndname" ] || [ "${1}" = "-p" ];
100         then
101                 PNDNAME=$2
102                 shift 2
103         elif [ "${1}" = "--pxml" ] || [ "${1}" = "-x" ];
104         then
105                 PXML=$2
106                 shift 2
107         elif [ "${1}" = "--schema" ] || [ "${1}" = "-f" ]
108         then
109                 PXML_schema=$2
110                 shift 2
111         else
112                 cecho "ERROR: '$1' is not a known argument. Printing --help and aborting." $red
113                 print_help
114                 exit 1;
115         fi
116 done
117
118
119 # Generate a PXML if the param is set to Guess or it is empty.
120 #TODO: make sure this does still work nicely with the latest genpxml that sebt3 is working on!
121 if [ ! $PXML ] || [ $PXML = "guess" ] && [ $PNDNAME ] && [ $FOLDER ];
122 then
123         if [ -f $FOLDER/PXML.xml ]; # use the already existing PXML.xml file if there is one...
124         then
125                 PXML=$FOLDER/PXML.xml
126                 PWML_ALREADY_EXISTING="true"
127         else
128                 PXMLtxt=$($GENPXML_PATH $FOLDER $ICON)
129                 PXML=$FOLDER/PXML.xml
130                 echo "$PXMLtxt" > $FOLDER/PXML.xml
131                 PXML_GENERATED="true"
132         fi
133 fi
134
135
136 # Probe if required variables were set
137 echo -e
138 cecho "Checking if all required variables were set." $green
139 if [ ! $PNDNAME ] || [ ! $FOLDER ] || [ ! $PXML ];
140 then
141         echo -e
142         cecho "ERROR: Not all required options were set! Please see the --help information below." $red
143         echo -e
144         print_help
145         exit 1
146 else
147         echo "PNDNAME set to '$PNDNAME'."
148 fi
149 # Check if the selected folder actually exists
150 if [ ! -d $FOLDER ];
151 then
152         echo -e
153         cecho "ERROR: '$FOLDER' doesnt exist or is not a folder." $red
154         exit 1
155 else
156         echo "FOLDER set to '$FOLDER'."
157 fi
158 # Check if the selected PXML file actually exists
159 if [ ! -f $PXML ];
160 then
161         echo -e
162         cecho "ERROR: '$PXML' doesnt exist or is not a file." $red
163         exit 1
164 else
165         if [ $PWML_ALREADY_EXISTING ];
166         then
167                 echo "You have not explicitly specified a PXML to use, but an existing file was
168 fount. Will be using this one."
169         elif [ $PXML_GENERATED ];
170         then
171                 echo "A PXML file was generated for you using '$GENPXML_PATH'. This file will
172 not be removed at the end of this script because you might want to review it, adjust
173 single entries and rerun the script to generate a pnd with a PXML file with all the
174 information you want to have listed."
175         fi
176         echo "PXML set to '$PXML'."
177 fi
178
179 # Print the other variables:
180 if [ $ICON ];
181 then
182         if [ ! -f $ICON ]
183         then
184                 cecho "'$ICON' doesn't exist, will not append the selected icon to the pnd." $red
185         else
186                 echo "ICON set to '$ICON'."
187                 USE_ICON="true"
188         fi
189 fi
190 if [ $SQUASH ];
191 then
192         echo "Will use a squashfs for '$PNDNAME'."
193 fi
194
195
196 # Validate the PXML file (if xmllint is available)
197 # Errors and problems in this section will be shown but are not fatal.
198 echo -e
199 cecho "Trying to validate '$PXML' now. Will be using '$PXML_schema' to do so." $green
200 which xmllint &> /dev/null
201 if [ "$?" -ne "0" ];
202 then
203         VALIDATED=false
204         cecho "WARNING: Could not find 'xmllint'. Validity check of '$PXML' is not possible!" $red
205 else
206         if [ ! -f "$PXML_schema" ];
207         then
208                 VALIDATED=false
209                 cecho "WARNING: Could not find '$PXML_schema'. If you want to validate your PXML file
210 please make sure to provide a schema using the --schema option." $red
211         else
212                 xmllint --noout --schema $PXML_schema $PXML
213                 if [ "$?" -ne "0" ]; then VALIDATED=false; else VALIDATED=true; fi
214         fi
215 fi
216 # Print some message at the end about the validation in case the user missed the output above
217 if [ $VALIDATED = "false" ]
218 then
219         cecho "WARNING: Could not successfully validate '$PXML'. Please check the output
220 above. This does not mean that your pnd will be broken. Either you are not following the strict
221 syntax required for validation or you don't have all files/programs required for validating." $red
222 else
223         cecho "Your file '$PXML' was validated successfully. The resulting pnd should
224 work nicely with libpnd." $green
225 fi
226
227
228 # Make iso from folder
229 echo -e
230 cecho "Creating an iso file based on '$FOLDER'." $green
231 if [ $SQUASH ];
232 then
233         if [ $(mksquashfs -version | awk 'BEGIN{r=0} $3>=4{r=1} END{print r}') -eq 0 ];
234         then
235                 cecho "ERROR: Your squashfs version is older then version 4, pleas upgrade to 4.0 or later" $red
236                 exit 1
237         fi
238         mksquashfs $FOLDER $PNDNAME.iso -nopad -no-recovery
239 else
240         mkisofs -o $PNDNAME.iso -R $FOLDER
241 fi
242
243
244 # Append pxml to iso
245 echo -e
246 cecho "Appending '$PXML' to the created iso file." $green
247 cat $PNDNAME.iso $PXML > $PNDNAME
248 rm $PNDNAME.iso #cleanup
249  
250  
251 # Append icon if specified and available
252 if [ $USE_ICON ];
253 then
254         echo -e
255         cecho "Appending the icon '$ICON' to the pnd." $green
256         mv $PNDNAME $PNDNAME.tmp
257         cat $PNDNAME.tmp $ICON > $PNDNAME # append icon
258         rm $PNDNAME.tmp #cleanup
259 fi
260
261
262 # Final message
263 echo -e
264 if [ -f $PNDNAME ];
265 then
266         cecho "Finished creating the pnd '$PNDNAME'." $green
267 else
268         cecho "There seems to have been a problem and '$PNDNAME' was not created. Please check
269 the output above for any error messages." $red
270 fi
271
272
273 #if [ $PXML = "guess" ];then rm $FOLDER/PXML.xml; fi #cleanup