update some scripts
[shellsnippets/shellsnippets.git] / mksh / sysadmin / mvndebri.sh
1 #!/bin/mksh
2 rcsid='$MirOS: contrib/hosted/tg/deb/mkdebidx.sh,v 1.51 2011/05/13 20:53:29 tg Exp $'
3 rcsid='$Id: mvndebri.sh 2534 2011-11-24 16:35:54Z tglase $'
4 #-
5 # Copyright (c) 2008, 2009, 2010, 2011
6 #       Thorsten Glaser <tg@mirbsd.org>
7 # Copyright (c) 2011
8 #       Thorsten Glaser <t.glaser@tarent.de>
9 #
10 # Provided that these terms and disclaimer and all copyright notices
11 # are retained or reproduced in an accompanying document, permission
12 # is granted to deal in this work without restriction, including un-
13 # limited rights to use, publicly perform, distribute, sell, modify,
14 # merge, give away, or sublicence.
15 #
16 # This work is provided "AS IS" and WITHOUT WARRANTY of any kind, to
17 # the utmost extent permitted by applicable law, neither express nor
18 # implied; without malicious intent or gross negligence. In no event
19 # may a licensor, author or contributor be held liable for indirect,
20 # direct, other damage, loss, or other issues arising in any way out
21 # of dealing in the work, even if advised of the possibility of such
22 # damage or existence of a defect, except proven that it results out
23 # of said person's immediate fault when using the work as intended.
24
25 if [[ $1 != /* || -z $2 || ! -d $1/$2/. ]]; then
26         print -u2 Syntax error.
27         exit 1
28 fi
29 jobname=$2
30
31 hn=$(hostname)
32 case $hn {
33 (ci-evolvis?(.*))
34         hn=ci-evolvis
35         repo_keyid=0x16B5D1B2
36         ;;
37 #[…]
38 #(test-hudson?(.*))
39 #       hn=test-hudson
40 #       repo_keyid=0xDB58AF73
41 #       ;;
42 (tglase.bonn.tarent.de)
43         hn=devel-testsystem
44         repo_keyid=0x5EB8D3B3
45         ;;
46 (*)
47         print -u2 "Cannot determine hostname from '$hn'"
48         exit 1
49         ;;
50 }
51 case $hn {
52 (test-hudson|devel-testsystem)
53         print -u2 moved
54         exit 1
55         dhn=jenkins-debs.evolvis.org
56         ;;
57 (*)
58         dhn=${hn}-debs.lan.tarent.de
59         ;;
60 }
61 repo_origin='tarent solutions GmbH'
62 repo_label=tarent_auto_${hn}_$jobname
63 repo_title="tarent $hn $jobname automatic DEB Repository"
64 function repo_description {
65         typeset suite_nick=$1
66
67         print -nr -- "tarent $hn $jobname/$suite_nick autobuild repository"
68 }
69
70
71 #set -A dpkgarchs -- alpha amd64 arm armeb armel armhf avr32 hppa \
72 #    i386 ia64 kfreebsd-amd64 kfreebsd-i386 lpia m32r m68k mips mipsel \
73 #    powerpc powerpcspe ppc64 s390 s390x sh3 sh3eb sh4 sh4eb sparc
74 set -A dpkgarchs -- amd64 i386
75
76 function putfile {
77         tee $1 | gzip -n9 >$1.gz
78 }
79
80 function sortlist {
81         typeset x u=$1
82
83         if [[ $u = -u ]]; then
84                 shift
85         else
86                 u=
87         fi
88
89         for x in "$@"; do
90                 print -r -- "$x"
91         done | sort $u
92 }
93
94 set +U
95 export LC_ALL=C
96 unset LANGUAGE
97 saveIFS=$IFS
98 cd "$1/$2"
99 shift
100 shift
101 if [[ ! -d dists/. ]]; then
102         print -u2 Not an APT repository.
103         exit 1
104 fi
105
106 suites=:
107 for suite in "$@"; do
108         suites=:dists/$suite$suites
109 done
110
111 allsuites=
112 for suite in dists/*; do
113         allsuites="$allsuites${allsuites:+ }${suite##*/}"
114         [[ $suites = : || $suites = *:"$suite":* ]] || continue
115         components=Components:
116         for dist in $suite/*; do
117                 [[ -d $dist/. ]] || continue
118                 rm -rf $dist/binary-* $dist/source
119                 ovf= oef= osf=
120                 [[ -s $dist/override.file ]] && ovf=$dist/override.file
121                 [[ -s $dist/override.extra ]] && oef="-e $dist/override.extra"
122                 [[ -s $dist/override.src ]] && osf="-s $dist/override.src"
123                 components="$components ${dist##*/}"
124                 for arch in ${dpkgarchs[*]} all; do
125                                 print "\n===> Creating" \
126                                     "${dist#dists/}/$arch/Packages\n"
127                                 mkdir -p $dist/binary-$arch
128                                 dpkg-scanpackages $oef -m -a $arch \
129                                     $dist $ovf | \
130                                     putfile $dist/binary-$arch/Packages
131                 done
132                 print "\n===> Creating ${dist#dists/}/Sources"
133                 mkdir -p $dist/source
134                 dpkg-scansources $osf \
135                     $dist $ovf | \
136                     putfile $dist/source/Sources
137                 print done.
138         done
139         print "\n===> Creating ${suite#dists/}/Release.gpg"
140         rm -f $suite/Release*
141         (cat <<-EOF
142                 Origin: ${repo_origin}
143                 Label: ${repo_label}
144                 Suite: ${suite##*/}
145                 Codename: ${suite##*/}
146                 Date: $(date -u)
147                 Architectures: all ${dpkgarchs[*]} source
148                 $components
149                 Description: $(repo_description "${suite##*/}")
150                 MD5Sum:
151         EOF
152         exec 4>$suite/Release-sha1
153         exec 5>$suite/Release-sha2
154         print -u4 SHA1:
155         print -u5 SHA256:
156         cd $suite
157         for n in Contents-* */{binary-*,source}/{Packag,Sourc}es*; do
158                 [[ -f $n ]] || continue
159                 # GNU *sum tools are horridly inefficient
160                 set -A x -- $(md5sum "$n")
161                 nm=${x[0]}
162                 set -A x -- $(sha1sum "$n")
163                 nsha1=${x[0]}
164                 set -A x -- $(sha256sum "$n")
165                 nsha2=${x[0]}
166                 ns=$(stat -c '%s' "$n")
167                 print " $nm $ns $n"
168                 print -u4 " $nsha1 $ns $n"
169                 print -u5 " $nsha2 $ns $n"
170         done) >$suite/Release
171         cat $suite/Release-sha1 $suite/Release-sha2 >>$suite/Release
172         rm $suite/Release-sha1 $suite/Release-sha2
173         gpg -u $repo_keyid -sb --batch \
174             --passphrase-file /etc/tarent/maven.kpw \
175             <$suite/Release >$suite/Release.gpg
176 done
177
178 print "\n===> Creating debidx.htm\n"
179
180 integer nsrc=0 nbin=0
181 br='<br />'
182
183 for suite in dists/*; do
184         for dist in $suite/*; do
185                 [[ -d $dist/. ]] || continue
186                 suitename=${suite##*/}
187                 if [[ $suitename != +([a-z0-9_]) ]]; then
188                         print -u2 "Invalid suite name '$suitename'"
189                         continue 2
190                 fi
191                 distname=${dist##*/}
192                 if [[ $distname != +([a-z0-9_-]) ]]; then
193                         print -u2 "Invalid dist name '$distname'"
194                         continue
195                 fi
196
197                 gzip -dc $dist/source/Sources.gz |&
198                 pn=; pv=; pd=; pp=; Lf=
199                 while IFS= read -pr line; do
200                         case $line {
201                         (" "*)
202                                 if [[ -n $Lf ]]; then
203                                         eval x=\$$Lf
204                                         x=$x$line
205                                         eval $Lf=\$x
206                                 fi
207                                 ;;
208                         ("Package: "*)
209                                 pn=${line##Package:*([   ])}
210                                 Lf=pn
211                                 ;;
212                         ("Version: "*)
213                                 pv=${line##Version:*([   ])}
214                                 Lf=pv
215                                 ;;
216                         ("Binary: "*)
217                                 pd=${line##Binary:*([    ])}
218                                 Lf=pd
219                                 ;;
220                         ("Directory: "*)
221                                 pp=${line##Directory:*([         ])}
222                                 Lf=pp
223                                 ;;
224                         (?*)    # anything else
225                                 Lf=
226                                 ;;
227                         (*)     # empty line
228                                 if [[ -n $pn && -n $pv && -n $pd && -n $pp ]]; then
229                                         i=0
230                                         while (( i < nsrc )); do
231                                                 [[ ${sp_name[i]} = "$pn" && \
232                                                     ${sp_dist[i]} = "$distname" ]] && break
233                                                 let i++
234                                         done
235                                         if (( i == nsrc )); then
236                                                 let nsrc++
237                                                 pvo=
238                                                 ppo=
239                                         else
240                                                 eval pvo=\$\{sp_ver_${suitename}[i]\}
241                                                 eval ppo=\$\{sp_dir_${suitename}[i]\}
242                                         fi
243                                         sp_name[i]=$pn
244                                         sp_dist[i]=$distname
245                                         #sp_suites[i]="${sp_suites[i]} $suitename"
246                                         eval sp_ver_${suitename}[i]='${pvo:+$pvo,}$pv'
247                                         eval sp_dir_${suitename}[i]='${ppo:+$ppo,}$pp/'
248                                         sp_desc[i]=${sp_desc[i]},$pd
249                                 fi
250                                 pn=; pv=; pd=; pp=; Lf=
251                                 ;;
252                         }
253                 done
254
255                 gzip -dc $(for f in $dist/binary-*/Packages.gz; do
256                         [[ -e $f ]] || continue
257                         realpath "$f"
258                 done | sort -u) |&
259                 pn=; pv=; pd=; pp=; pN=; pf=; Lf=
260                 while IFS= read -pr line; do
261                         case $line {
262                         (" "*)
263                                 if [[ -n $Lf ]]; then
264                                         eval x=\$$Lf
265                                         x=$x$line
266                                         eval $Lf=\$x
267                                 fi
268                                 ;;
269                         ("Package: "*)
270                                 pN=${line##Package:*([   ])}
271                                 Lf=pN
272                                 ;;
273                         ("Source: "*)
274                                 pn=${line##Source:*([    ])}
275                                 pn=${pn%% *}
276                                 Lf=pn
277                                 ;;
278                         ("Version: "*)
279                                 pv=${line##Version:*([   ])}
280                                 Lf=pv
281                                 ;;
282                         ("Description: "*)
283                                 pd=${line##Description:*([       ])}
284                                 ;;
285                         ("Architecture: "*)
286                                 pp=${line##Architecture:*([      ])}
287                                 Lf=pp
288                                 ;;
289                         ("Filename: "*)
290                                 pf=${line##Filename:*([  ])}
291                                 Lf=pf
292                                 ;;
293                         (?*)    # anything else
294                                 Lf=
295                                 ;;
296                         (*)     # empty line
297                                 if [[ $pf = *:* || $pf = *'%'* ]]; then
298                                         print -u2 Illegal character in $dist \
299                                             packages $pp "'Filename: $pf'"
300                                         exit 1
301                                 fi
302                                 [[ -n $pn ]] || pn=$pN
303                                 if [[ -n $pn && -n $pv && -n $pd && -n $pp ]]; then
304                                         i=0
305                                         while (( i < nbin )); do
306                                                 [[ ${bp_disp[i]} = "$pN" && ${bp_desc[i]} = "$pd" && \
307                                                     ${bp_dist[i]} = "$distname" ]] && break
308                                                 let i++
309                                         done
310                                         (( i == nbin )) && let nbin++
311                                         bp_name[i]=$pn
312                                         bp_disp[i]=$pN
313                                         bp_dist[i]=$distname
314                                         #bp_suites[i]="${bp_suites[i]} $suitename"
315                                         [[ -n $pf ]] && pv="<a href=\"$pf\">$pv</a>"
316                                         pv="$pp: $pv"
317                                         eval x=\${bp_ver_${suitename}[i]}
318                                         [[ $br$x$br = *"$br$pv$br"* ]] || x=$x${x:+$br}$pv
319                                         eval bp_ver_${suitename}[i]=\$x
320                                         bp_desc[i]=$pd
321                                 fi
322                                 pn=; pv=; pd=; pp=; pN=; pf=; Lf=
323                                 ;;
324                         }
325                 done
326         done
327 done
328
329 (cat <<'EOF'
330 <?xml version="1.0"?>\r
331 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
332  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
333 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head>
334  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
335  <meta name="MSSmartTagsPreventParsing" content="TRUE" />
336 EOF
337 print -r -- " <title>${repo_title} Index</title>"
338 cat <<'EOF'
339  <meta name="generator" content="Evolvis shellsnippets git based on $Id: mvndebri.sh 2534 2011-11-24 16:35:54Z tglase $ based on $MirOS: contrib/hosted/tg/deb/mkdebidx.sh,v 1.51 2011/05/13 20:53:29 tg Exp $" />
340  <style type="text/css">
341   table {
342    border: 1px solid black;
343    border-collapse: collapse;
344    text-align: left;
345    vertical-align: top;
346   }
347   tr {
348    border: 1px solid black;
349    text-align: left;
350    vertical-align: top;
351   }
352   td {
353    border: 1px solid black;
354    text-align: left;
355    vertical-align: top;
356   }
357   th {
358    background-color: #000000;
359    color: #FFFFFF;
360   }
361   .tableheadcell {
362    border: 1px solid #999999;
363    padding: 3px;
364    white-space: nowrap;
365   }
366   .srcpkgline {
367    background-color: #CCCCCC;
368   }
369   .srcpkgdist {
370    background-color: #666666;
371    color: #FFFFFF;
372    font-weight: bold;
373   }
374   .binpkgdist {
375    background-color: #999999;
376    color: #FFFFFF;
377    font-weight: bold;
378   }
379  </style>
380 </head><body>
381 EOF
382 print -r -- "<h1>${repo_title}</h1>"
383 cat <<EOF
384 <p><a href="dists/">Browse</a> the repository.
385  This repository uses <a
386   href="http://pgp.uni-mainz.de:11371/pks/lookup?search=${repo_keyid}&amp;op=vindex">${repo_keyid}</a>
387  as signing key.
388 </p>
389 <h2>Suites</h2>
390 <ul>
391 EOF
392
393 allsuites=$(for suitename in $allsuites; do
394         print $suitename
395 done | sort -u)
396
397 for suitename in $allsuites; do
398         suite=dists/$suitename
399         print -n " <li>${suite##*/}: <a href=\"$suite/\">$jobname for $suitename</a> (dists:"
400         vdists=
401         for dist in $suite/*; do
402                 [[ -d $dist/. ]] || continue
403                 distname=${dist##*/}
404                 vdists="$vdists $distname"
405                 print -n " <a href=\"$suite/$distname/\">$distname</a>"
406         done
407         print ")<br />"
408         print " <tt>deb https://${dhn}/$jobname $suitename$vdists</tt>"
409         print "</li>"
410 done
411 print "</ul>"
412 print "<h2>Packages</h2>"
413 print "<table width=\"100%\"><thead>"
414 print "<tr class=\"tablehead\">"
415 print " <th class=\"tableheadcell\">dist</th>"
416 print " <th class=\"tableheadcell\" rowspan=\"2\">Binary / Description</th>"
417 for suitename in $allsuites; do
418         print " <th class=\"tableheadcell\" rowspan=\"2\">$suitename</th>"
419 done
420 print "</tr><tr class=\"tablehead\">"
421 print " <th class=\"tableheadcell\">package name</th>"
422 print "</tr></thead><tbody>"
423
424 set -A bp_sort
425 i=0
426 while (( i < nbin )); do
427         print $i ${bp_disp[i++]} #${bp_suites[i]}
428 done | sort -k2 |&
429 while read -p num rest; do
430         bp_sort[${#bp_sort[*]}]=$num
431 done
432
433 i=0
434 while (( i < nsrc )); do
435         print $i ${sp_name[i++]}
436 done | sort -k2 |&
437 while read -p num rest; do
438         print "\n<!-- sp #$num = ${sp_name[num]} -->"
439         print "<tr class=\"srcpkgline\">"
440         print " <td class=\"srcpkgdist\">${sp_dist[num]}</td>"
441         pd=
442         for x in $(tr ', ' '\n' <<<"${sp_desc[num]}" | sort -u); do
443                 [[ -n $x ]] && pd="$pd, $x"
444         done
445         print " <td rowspan=\"2\" class=\"srcpkgdesc\">${pd#, }</td>"
446         for suitename in $allsuites; do
447                 eval pvo=\${sp_ver_${suitename}[num]}
448                 eval ppo=\${sp_dir_${suitename}[num]}
449                 IFS=,
450                 set -A pva -- $pvo
451                 set -A ppa -- $ppo
452                 IFS=$saveIFS
453                 (( ${#pva[*]} )) || pva[0]=
454                 y=
455                 i=0
456                 while (( i < ${#pva[*]} )); do
457                         pv=${pva[i]}
458                         pp=${ppa[i]}
459                         if [[ $pv = *"\1a"* ]]; then
460                                 pvdsc=${pv%%"\1a"*}
461                                 pv=${pv##*"\1a"}
462                         else
463                                 pvdsc=$pv
464                         fi
465                         if [[ -z $pv ]]; then
466                                 pv=-
467                         elif [[ $pp != ?(/) ]]; then
468                                 pv="<a href=\"$pp${sp_name[num]}_${pvdsc##+([0-9]):}.dsc\">$pv</a>"
469                         fi
470                         [[ $pp != ?(/) ]] && pv="<a href=\"$pp\">[dir]</a> $pv"
471                         y=${y:+"$y<br />"}$pv
472                         let i++
473                 done
474                 print " <td rowspan=\"2\" class=\"srcpkgitem\">$y</td>"
475         done
476         print "</tr><tr class=\"srcpkgline\">"
477         print " <td class=\"srcpkgname\">${sp_name[num]}</td>"
478         print "</tr>"
479         k=0
480         while (( k < nbin )); do
481                 (( (i = bp_sort[k++]) < 0 )) && continue
482                 [[ ${bp_name[i]} = "${sp_name[num]}" && \
483                     ${bp_dist[i]} = "${sp_dist[num]}" ]] || continue
484                 bp_sort[k - 1]=-1
485                 #print "<!-- bp #$i for${bp_suites[i]} -->"
486                 print "<!-- bp #$i -->"
487                 print "<tr class=\"binpkgline\">"
488                 print " <td class=\"binpkgname\">${bp_disp[i]}</td>"
489                 print " <td class=\"binpkgdesc\">${bp_desc[i]}</td>"
490                 for suitename in $allsuites; do
491                         eval pv=\${bp_ver_${suitename}[i]}
492                         if [[ -z $pv ]]; then
493                                 pv=-
494                         fi
495                         print " <td class=\"binpkgitem\">$pv</td>"
496                 done
497                 print "</tr>"
498         done
499 done
500
501 num=0
502 for i in ${bp_sort[*]}; do
503         (( i < 0 )) && continue
504         if (( !num )); then
505                 print "\n<!-- sp ENOENT -->"
506                 print "<tr class=\"srcpkgline\">"
507                 print " <td class=\"srcpkgname\">~ENOENT~</td>"
508                 print " <td class=\"srcpkgdesc\">binary" \
509                     "packages without a matching source package</td>"
510                 for suitename in $allsuites; do
511                         print " <td class=\"srcpkgitem\">-</td>"
512                 done
513                 print "</tr>"
514                 num=1
515         fi
516         #print "<!-- bp #$i for${bp_suites[i]} -->"
517         print "<!-- bp #$i -->"
518         print "<tr class=\"binpkgline\">"
519         print " <td class=\"binpkgdist\">${bp_dist[i]}</td>"
520         print " <td rowspan=\"2\" class=\"binpkgdesc\">${bp_desc[i]}</td>"
521         for suitename in $allsuites; do
522                 eval pv=\${bp_ver_${suitename}[i]}
523                 if [[ -z $pv ]]; then
524                         pv=-
525                 fi
526                 print " <td rowspan=\"2\" class=\"binpkgitem\">$pv</td>"
527         done
528         print "</tr><tr class=\"binpkgline\">"
529         print " <td class=\"binpkgname\">${bp_disp[i]}</td>"
530         print "</tr>"
531 done
532
533 cat <<EOF
534
535 </tbody></table>
536
537 <p>• <a href="http://validator.w3.org/check/referer">Valid XHTML/1.1!</a>
538  • <small>Generated on $(date -u +'%F %T') by <tt
539  style="white-space:pre;">$rcsid</tt></small> •</p>
540 </body></html>
541 EOF
542
543 :) >debidx.htm
544 print done.