1 | #!/bin/bash |
---|
2 | #Script that generates xml info file from a desktop file |
---|
3 | #Usage: |
---|
4 | #Download lliurex-artwork-default from svn |
---|
5 | #Download the sources of the package(s) |
---|
6 | #Execute this script adding the dirs of sources to analyze as parameters |
---|
7 | #Profit |
---|
8 | |
---|
9 | msg_need_svn="In order to add the right icons you need to download lliurex-artwork-default from svn to the same dir as the source packages" |
---|
10 | msg_dir_not_found="dir not found" |
---|
11 | msg_icons_dir_error="Can't access %s or %s.\nSome icons may be missing.\n" |
---|
12 | msg_select_file="Select one install file:\n" |
---|
13 | msg_selected_file="\nSelected file: %s\n\n" |
---|
14 | msg_select_dir="\nSelect the dir containing the desktop file:\n" |
---|
15 | msg_gen_metainfo="Generating metainfo data path %s\n" |
---|
16 | msg_parsing_desktop="Parsing %s\n" |
---|
17 | msg_metainfo_generated="\nMetainfo file generated.\n" |
---|
18 | msg_desktop_updated="\nDesktop file updated.\n" |
---|
19 | msg_icon_found="Icon %s found.\nGenerating llx-resources dir\n" |
---|
20 | msg_icon_not_found="\nNo icon named %s found on dirs %s, %s or %s\nRemember that the icon must be in SVG format and no in PNG or other image format\nOPERATION ABORTED\n" |
---|
21 | msg_icon_location_info="\nPlease add %s as svg to %s and relaunch this program or use the llxsrc debhelper. Until then the app will not be included in our software store.\n" |
---|
22 | msg_icon_exists="\nIcon package found on the right location. Checking if llxsrc helper exists in rules\n" |
---|
23 | msg_work_done="\n----------\n\nWork done. If the process has no errors please check that the appfile.xml is right and that the rules file has the llxsrc helper. Also check that the icon is present in llx-resources.\nIn other case correct the errors and relaunch the script.\n\nRemember that the generated appfile.xml isn't a full one and is missing some fields like the screenshots. Take a cup of coffee and fulfill the empty fields following the specs at https://www.freedesktop.org/software/appstream/docs/sect-Metadata-Application.html\n\n" |
---|
24 | msg_install_not_found="Install file not found in debian dir. Aborting.\n" |
---|
25 | msg_select_workdir="Select the workdir\n" |
---|
26 | msg_debhelper_enabled="package type llxsrc detected. Setting llx-resources as workdir\n" |
---|
27 | msg_rules_old_format="\n********** WARNING!!!! **********\nrules file has an old format.\nIt's HIGHLY recommended to update it to the new rules format.\n *************************** \n" |
---|
28 | msg_select_pkg="\nSelect the name of the package to process\n" |
---|
29 | msg_desktop_not_found="\nDesktop file not found at %s. Process aborted.\n" |
---|
30 | |
---|
31 | function usage_help |
---|
32 | { |
---|
33 | printf "Usage:\n" |
---|
34 | printf "$0 [svn_package_dir1] [svn_package_dir2]\n\n" |
---|
35 | printf "$msg_need_svn\n\n" |
---|
36 | exit 1 |
---|
37 | } |
---|
38 | |
---|
39 | function analyze_dir |
---|
40 | { |
---|
41 | printf "Analyzing $1\n" |
---|
42 | rootDir=$PWD |
---|
43 | svnDir=`dirname $1` |
---|
44 | pkgDir=$1 |
---|
45 | srcDir=${1}"/trunk/fuentes" |
---|
46 | debianDir=${1}"/trunk/fuentes/debian" |
---|
47 | lliurexArtworkDir=${svnDir}"/vibrancy-colors/trunk/fuentes/vibrancy-lliurex/apps" |
---|
48 | if [ ! -d $lliurexArtworkDir ] || [ ! -d $vibrancyArtworkDir ] |
---|
49 | then |
---|
50 | printf "$msg_icons_dir_error" $lliurexArtworkDir $vibrancyArtworkDir |
---|
51 | fi |
---|
52 | |
---|
53 | cd $debianDir |
---|
54 | #Set the package name |
---|
55 | set_appName |
---|
56 | #Get the install file. If there're many choose one... |
---|
57 | declare -a installFiles |
---|
58 | count=0 |
---|
59 | for i in *install |
---|
60 | do |
---|
61 | installFiles[$count]=$i |
---|
62 | let count++ |
---|
63 | done |
---|
64 | index=0 |
---|
65 | let count-- |
---|
66 | if [[ ${installFiles[0]} = '*install' ]] |
---|
67 | then |
---|
68 | printf "$msg_install_not_found" |
---|
69 | get_workdir |
---|
70 | debhelper_mode=1 |
---|
71 | else |
---|
72 | if [ $count -gt 0 ] |
---|
73 | then |
---|
74 | printf "$msg_select_file" |
---|
75 | for i in `seq 0 ${count}` |
---|
76 | do |
---|
77 | printf "${i}) ${installFiles[$i]}\n" |
---|
78 | done |
---|
79 | printf "Select file [0]: " |
---|
80 | read index |
---|
81 | [ -z $index ] && index=0 |
---|
82 | installFile=${installFiles[$index]} |
---|
83 | else |
---|
84 | installFile=${installFiles[0]} |
---|
85 | fi |
---|
86 | printf "$msg_selected_file" $installFile |
---|
87 | process_installFile ${installFile} |
---|
88 | fi |
---|
89 | } |
---|
90 | |
---|
91 | function set_appName |
---|
92 | { |
---|
93 | appName=$(basename $pkgDir) |
---|
94 | declare -a pkgsArray |
---|
95 | count=0 |
---|
96 | for pkg in `grep "Package:\ .*" control | cut -f2 -d ' '` |
---|
97 | do |
---|
98 | pkgsArray[$count]=$pkg |
---|
99 | let count++ |
---|
100 | done |
---|
101 | let count-- |
---|
102 | if [ $count -gt 0 ] |
---|
103 | then |
---|
104 | printf "$msg_select_pkg" |
---|
105 | for i in `seq 0 ${count}` |
---|
106 | do |
---|
107 | printf "$i) ${pkgsArray[$i]}\n" |
---|
108 | done |
---|
109 | printf "Select pkg name [0]: " |
---|
110 | read index |
---|
111 | [ -z $index ] && index=0 |
---|
112 | appName=${pkgsArray[$index]} |
---|
113 | else |
---|
114 | installFile=${pkgsArray[0]} |
---|
115 | fi |
---|
116 | } |
---|
117 | |
---|
118 | function get_workdir |
---|
119 | { |
---|
120 | cd $rootDir |
---|
121 | cd $srcDir |
---|
122 | if [ -d llx-resources ] |
---|
123 | then |
---|
124 | printf "$msg_debhelper_enabled" |
---|
125 | installDir="llx-resources" |
---|
126 | else |
---|
127 | printf "$msg_select_workdir" |
---|
128 | count=0 |
---|
129 | declare -a dirArray |
---|
130 | for directory in * |
---|
131 | do |
---|
132 | dirArray[$count]=$directory |
---|
133 | let count++ |
---|
134 | done |
---|
135 | printf "Selected Dir [0]: " |
---|
136 | read index |
---|
137 | installDir=${dirArray[$index]} |
---|
138 | fi |
---|
139 | process_pkg $installDir |
---|
140 | } |
---|
141 | |
---|
142 | function process_installFile |
---|
143 | { |
---|
144 | installFile=$1 |
---|
145 | if [ `wc -l $installFile | cut -f1 -d ' '` -gt 1 ] |
---|
146 | then |
---|
147 | printf "$msg_select_dir" |
---|
148 | count=0 |
---|
149 | declare -a fileLines |
---|
150 | while read -r dirLine |
---|
151 | do |
---|
152 | fileLines[$count]=$dirLine |
---|
153 | printf "$count) $dirLine\n" |
---|
154 | let count++ |
---|
155 | done < ${installFile} |
---|
156 | printf "Selected file [0]: " |
---|
157 | read index |
---|
158 | installDir=${fileLines[$index]} |
---|
159 | else |
---|
160 | installDir=`head -n1 $installFile` |
---|
161 | fi |
---|
162 | [ -z $index ] && index=0 |
---|
163 | installDir=${installDir/\**/} |
---|
164 | process_pkg $installDir |
---|
165 | } |
---|
166 | |
---|
167 | function process_pkg |
---|
168 | { |
---|
169 | cd $rootDir"/"$srcDir |
---|
170 | wrkDir=`realpath $1` |
---|
171 | printf "Entering $wrkDir\n" |
---|
172 | if [ -d $wrkDir ] |
---|
173 | then |
---|
174 | cd $wrkDir |
---|
175 | else |
---|
176 | wrkDir=`dirname $wrkDir` |
---|
177 | cd $wrkDir |
---|
178 | fi |
---|
179 | #Find the desktop file of the application |
---|
180 | desktopFiles=$(find . -name "*.desktop") |
---|
181 | if [[ ! $desktopFiles ]] |
---|
182 | then |
---|
183 | printf "$msg_desktop_not_found" $wrkDir |
---|
184 | exit 1 |
---|
185 | fi |
---|
186 | for desktopFile in $desktopFiles |
---|
187 | do |
---|
188 | cd $rootDir"/"$srcDir |
---|
189 | printf "Entering $wrkDir\n" |
---|
190 | cd $wrkDir |
---|
191 | #If workdir != $1 then it means that the install file has a file and not a dir |
---|
192 | #In this case we assume that the install is putting the desktop file so metainfoDir becames llx-resources directly |
---|
193 | if [[ $wrkDir != `realpath $1` ]] |
---|
194 | then |
---|
195 | metainfoDir=${rootDir}"/"${srcDir}"/llx-resources/"${appName} |
---|
196 | else |
---|
197 | metainfoDir=`dirname $desktopFile` |
---|
198 | metainfoDir=`dirname $metainfoDir` |
---|
199 | fi |
---|
200 | metainfoDir=$metainfoDir"/metainfo" |
---|
201 | printf "$msg_gen_metainfo" $metainfoDir |
---|
202 | mkdir $metainfoDir -p |
---|
203 | parse_desktop $metainfoDir $desktopFile |
---|
204 | iconName=$(grep ^Icon= $desktopFile) |
---|
205 | iconName=${iconName/*=/} |
---|
206 | iconName=${iconName/.*/} |
---|
207 | get_icon $iconName |
---|
208 | done |
---|
209 | } |
---|
210 | |
---|
211 | function parse_desktop |
---|
212 | { |
---|
213 | metainfoDir=$1 |
---|
214 | shift |
---|
215 | for desktopFile in $@ |
---|
216 | do |
---|
217 | printf "$msg_parsing_desktop" $desktopFile |
---|
218 | item=`basename $desktopFile` |
---|
219 | awk -v processFile=$item -v metainfoDir=$metainfoDir -F '=' ' |
---|
220 | BEGIN{ |
---|
221 | split(processFile,array,".",seps) |
---|
222 | # revDomainName="net.lliurex."array[1] |
---|
223 | outFile="/tmp/"processFile |
---|
224 | printf("") > outFile |
---|
225 | xmlFile=metainfoDir"/"array[1]".appdata.xml" |
---|
226 | print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > xmlFile |
---|
227 | print "<component type=\"desktop-application\">" >> xmlFile |
---|
228 | # print "<id>"revDomainName"</id>" >> xmlFile |
---|
229 | print "<id>"array[1]"</id>" >> xmlFile |
---|
230 | print "<metadata_license>CC0-1.0</metadata_license>" >> xmlFile |
---|
231 | execPrinted=0 |
---|
232 | commentArrayIndex=1 |
---|
233 | nameArrayIndex=1 |
---|
234 | } |
---|
235 | { |
---|
236 | if ($1~"^Name") |
---|
237 | { |
---|
238 | if ($1=="Name") |
---|
239 | { |
---|
240 | tagName="<name>" |
---|
241 | } else { |
---|
242 | lang=$1 |
---|
243 | split(lang,array,"[",seps) |
---|
244 | lang=substr(array[2], 1, length(array[2])-1) |
---|
245 | tagName="<name xml:lang=\""lang"\">" |
---|
246 | } |
---|
247 | tagName=tagName""$2"</name>" |
---|
248 | nameArray[nameArrayIndex]=tagName |
---|
249 | nameArrayIndex++; |
---|
250 | } else if ($1~"Comment") { |
---|
251 | if ($1=="Comment") |
---|
252 | { |
---|
253 | tagSum="<summary>" |
---|
254 | tagDes="<p>" |
---|
255 | } else { |
---|
256 | lang=$1 |
---|
257 | split(lang,array,"[",seps) |
---|
258 | lang=substr(array[2], 1, length(array[2])-1) |
---|
259 | tagSum="<summary xml:lang=\""lang"\">" |
---|
260 | tagDes="<p xml:lang=\""lang"\">" |
---|
261 | } |
---|
262 | sumario=$2 |
---|
263 | split(sumario,array,".",seps) |
---|
264 | $0=$1"="array[1] |
---|
265 | summaryArray[commentArrayIndex]=tagSum""array[1]"</summary>" |
---|
266 | descriptionArray[commentArrayIndex]=tagDes""sumario"</p>" |
---|
267 | commentArrayIndex++ |
---|
268 | } else if ($1=="Categories") { |
---|
269 | customCat=0 |
---|
270 | countCat=0 |
---|
271 | split($2,array,";",seps) |
---|
272 | for (catIndex in array) |
---|
273 | { |
---|
274 | if (array[catIndex]!="") |
---|
275 | { |
---|
276 | if (array[catIndex]~"-") |
---|
277 | { |
---|
278 | customCat=1 |
---|
279 | split(array[catIndex],lliurexCats,"-",seps) |
---|
280 | for (lliurexCatIndex in lliurexCats) |
---|
281 | { |
---|
282 | lliurexCat="<category>"lliurexCats[lliurexCatIndex]"</category>\n"lliurexCat |
---|
283 | } |
---|
284 | categoryArray[catIndex]=lliurexCat |
---|
285 | } else { |
---|
286 | categoryArray[catIndex]="<category>"array[catIndex]"</category>" |
---|
287 | } |
---|
288 | countCat++ |
---|
289 | } |
---|
290 | } |
---|
291 | if (customCat==1 && countCat==1) |
---|
292 | { |
---|
293 | if (substr($0,length($0),1)==";") |
---|
294 | $0=$0"GTK" |
---|
295 | else |
---|
296 | $0=$0;"GTK" |
---|
297 | } |
---|
298 | } else if ($1=="Icon") { |
---|
299 | arrayItemNames=split($2,array,"/",seps) |
---|
300 | arrayItems=split(array[arrayItemNames],array,".",seps) |
---|
301 | iconBaseName=array[1] |
---|
302 | $0=$1"="iconBaseName |
---|
303 | # split(iconBaseName,array,".",seps) |
---|
304 | tagIcon="<icon type=\"stock\">"iconBaseName"</icon>"; |
---|
305 | } else if ($1=="Exec") { |
---|
306 | if (execPrinted==0) |
---|
307 | { |
---|
308 | split($2,array," ",seps) |
---|
309 | tagExec="<provides><binary>"array[1]"</binary></provides>" |
---|
310 | execPrinted=1 |
---|
311 | } |
---|
312 | } |
---|
313 | print $0>>outFile |
---|
314 | } |
---|
315 | END{ |
---|
316 | for (nameIndex in nameArray) |
---|
317 | print nameArray[nameIndex] >> xmlFile |
---|
318 | for (summaryIndex in summaryArray) |
---|
319 | print summaryArray[summaryIndex] >> xmlFile; |
---|
320 | print "<description>" >> xmlFile |
---|
321 | for (descriptionIndex in descriptionArray) |
---|
322 | print descriptionArray[descriptionIndex] >> xmlFile; |
---|
323 | print "</description>" >> xmlFile |
---|
324 | print "<categories>" >> xmlFile |
---|
325 | for (categoryIndex in categoryArray) |
---|
326 | print categoryArray[categoryIndex] >> xmlFile; |
---|
327 | print "</categories>" >> xmlFile |
---|
328 | print tagIcon >> xmlFile |
---|
329 | print tagExec >> xmlFile |
---|
330 | print "<developer_name>Lliurex Team</developer_name>" >> xmlFile |
---|
331 | print "</component>" >> xmlFile |
---|
332 | } |
---|
333 | ' $desktopFile |
---|
334 | printf "$msg_metainfo_generated" |
---|
335 | cp /tmp/${item} $desktopFile |
---|
336 | printf "$msg_desktop_updated" |
---|
337 | done |
---|
338 | } |
---|
339 | |
---|
340 | function get_icon |
---|
341 | { |
---|
342 | iconName=$1 |
---|
343 | #Check if a svg icon exists |
---|
344 | workDir=$PWD |
---|
345 | printf "\nSearching for icon ${iconName}.svg in our package\n" |
---|
346 | #First we'll check the dir for resources |
---|
347 | resourcesDir=llx-resources/ |
---|
348 | cd $rootDir |
---|
349 | cd $srcDir |
---|
350 | iconFile=$(find ${resourcesDir} -name "$iconName.svg" | grep icons) |
---|
351 | if [[ ! $iconFile ]] |
---|
352 | then |
---|
353 | cd $workDir |
---|
354 | printf "Entering $workDir\n" |
---|
355 | iconPath=${srcDir}"/"${installDir} |
---|
356 | iconFile=$(find . -name "$iconName.svg") |
---|
357 | if [[ ! $iconFile ]] |
---|
358 | then |
---|
359 | printf "Accesing $lliurexArtworkDir\n" |
---|
360 | #Look for the svg in lliurex-theme |
---|
361 | cd $rootDir |
---|
362 | iconPath=$lliurexArtworkDir |
---|
363 | cd $lliurexArtworkDir |
---|
364 | iconFile=$(find . -name "${iconName}.svg") |
---|
365 | fi |
---|
366 | if [[ $iconFile ]] |
---|
367 | then |
---|
368 | cd $rootDir |
---|
369 | printf "$msg_icon_found" $iconFile |
---|
370 | cd $srcDir |
---|
371 | resourcesDir="llx-resources/"${appName}"/icons/apps/" |
---|
372 | mkdir $resourcesDir -p |
---|
373 | cd $OLDPWD |
---|
374 | cp ${iconPath}"/"${iconFile} ${srcDir}"/"${resourcesDir} |
---|
375 | cd $OLDPWD |
---|
376 | else |
---|
377 | printf "$msg_icon_not_found" ${iconName} $lliurexArtworkDir $resourcesDir $installDir |
---|
378 | printf "$msg_icon_location_info" ${iconName} $lliurexArtworkDir |
---|
379 | exit 1 |
---|
380 | fi |
---|
381 | else |
---|
382 | printf "$msg_icon_exists" |
---|
383 | fi |
---|
384 | add_llxsrc_helper |
---|
385 | } |
---|
386 | |
---|
387 | function add_llxsrc_helper |
---|
388 | { |
---|
389 | cd $rootDir |
---|
390 | cd $debianDir |
---|
391 | printf "Adding llxsrc to rules" |
---|
392 | if [[ ! `grep 'llxsrc' rules` ]] |
---|
393 | then |
---|
394 | if [[ `grep 'dh \$@' rules` ]] |
---|
395 | then |
---|
396 | if [[ `grep '\-\-with' rules` ]] |
---|
397 | then |
---|
398 | sed -i 's/\(dh $@\ --with\ \)/\0llxsrc,/' rules |
---|
399 | else |
---|
400 | sed -i 's/\(dh $@\ \)/\0 --with\ llxsrc\ /' rules |
---|
401 | fi |
---|
402 | else |
---|
403 | printf "$msg_rules_old_format" |
---|
404 | sed -i 's/\tdh_installdeb/\tdh_llxsrcinstall\n\tdh_installdeb/' rules |
---|
405 | fi |
---|
406 | fi |
---|
407 | } |
---|
408 | |
---|
409 | |
---|
410 | [[ $@ ]] || usage_help |
---|
411 | |
---|
412 | for parm in $@ |
---|
413 | do |
---|
414 | if [ ! -d $parm ] |
---|
415 | then |
---|
416 | printf "\n${parm}: $msg_dir_not_found\n" |
---|
417 | usage_help |
---|
418 | exit 1 |
---|
419 | fi |
---|
420 | done |
---|
421 | |
---|
422 | for parm in $@ |
---|
423 | do |
---|
424 | debhelper_mode=0 |
---|
425 | analyze_dir $parm |
---|
426 | done |
---|
427 | |
---|
428 | printf "$msg_work_done" |
---|
429 | |
---|
430 | exit 0 |
---|
431 | |
---|