summaryrefslogtreecommitdiff
path: root/update-archlinux32-package
blob: e2e8ba03b0a25c19a3ffbf9bc4763d1de6b86df2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#!/bin/bash

# update an archlinux32 package
#   *-bootstrap
#   *-dummy
#   archiso
#   archlinux32-keyring
#   archlinux32-keyring-transition
#   asp
#   dart
#   devtools
#   eclipse
#   flashplugin
#   freebasic
#   linux
#   linux-lts
#   linux-olpc-xo1 (no upstream)
#   linux-pae (no upstream)
#   linux-zen
#   reflector
#   teamspeak3-server

git_repo_path='/usr/src/archlinux32/packages'
upstream_git_path='/usr/src/archlinux/packages'
upstream_community_git_path='/usr/src/archlinux/community'
archlinuxewe_git_path=~erich/'archlinuxewe'
base_dir=$(
  dirname "$(
    readlink -f "$0"
  )"
)

if [ "x$1" = "x-" ]; then
  shift
else
  if ! git -C "${git_repo_path}" pull --ff-only || \
    ! git -C "${upstream_git_path}" pull --ff-only || \
    ! git -C "${upstream_community_git_path}" pull --ff-only || \
    ! git -C "${archlinuxewe_git_path}" pull --ff-only; then
    >&2 echo 'Your git repos cannot cleanly be updated'
    exit 1
  fi
fi

case $# in
  0)
    >&2 echo "usage: $0 pkg1 pkg2 ..."
    exit 1
  ;;
  1)
  ;;
  *)
    for param in "$@"; do
      "$0" - "${param}" || exit $?
    done
    exit 0
  ;;
esac

pkgname="$1"
repo=$(
  git -C "${git_repo_path}" archive HEAD -- | \
    tar -t --wildcards "*/${pkgname}/PKGBUILD" | \
    cut -d/ -f1
)

if [ "$(printf '%s\n' "${repo}" | wc -l)" -ne 1 ]; then
  >&2 printf 'package "%s" does not exist in exactly one repository\n' "${pkgname}"
  exit 1
fi

update_checksum() {
  checksums=$(
    ssh arch32-test '
      cd '"${pkgname}"'
      makepkg -g
    ' \
    </dev/null \
    | sed '
      $! s/$/\\n/
    ' | \
    tr -d '\n'
  )
  sed -i '
    /^\S\+sums=(/{
      :a
        s/^\S\+sums=(.*)/'"${checksums}"'/
        t
        N
        ba
    }
  ' "${git_repo_path}/${repo}/${pkgname}/PKGBUILD"
}

# selecting the "update path"
case "${pkgname}" in
  'asp'|'devtools'|'archiso')
    # a package which is in [archlinuxewe] -> mostly upstream, but with
    # replaced sources, checksums and pkgver (might be identical to
    # upstream, though)
    update_path='archlinuxewe'
  ;;
  'dart'|'eclipse'|'flashplugin'|'freebasic'|'reflector'|'teamspeak3-server')
    # an upstream package which is updated by solely updating its checksum
    update_path='checksum'
  ;;
  'archlinux32-keyring'|'archlinux32-keyring-transition'|*'-bootstrap'|*'-dummy')
    # a package without upstream which is updated by updating its
    # version (according to watch-versions) and checksum
    update_path='version and checksum without upstream'
  ;;
  'linux'|'linux-lts'|'linux-zen')
    # a kernel which exists upstream -> different config and checksums
    update_path='kernel with upstream'
  ;;
  'linux-olpc-xo1'|'linux-pae')
    # a kernel which does not exist upstream -> complete package sources
    # in our repository
    update_path='kernel without upstream'
  ;;
  *)
    >&2 printf 'I don'"'"'t know how to update package "%s"\n' "${pkgname}"
    exit 1
  ;;
esac

if ! ssh arch32-test 'rm -rf --one-file-system "'"${pkgname}"'"'; then
  >&2 echo 'arch32-test vm is not running'
fi


case ${update_path} in
  'archlinuxewe')
    archlinuxewe_PKGBUILD=$(
      git -C "${archlinuxewe_git_path}" archive HEAD -- "${pkgname}32/PKGBUILD" | \
        tar -Ox
    )
    repo_arch=$(
      printf '%s\n' "${archlinuxewe_PKGBUILD}" | \
        grep '^arch=' | \
        cut -d'=' -f2 | \
        tr '()"'"'" '\n' | \
        grep -xF 'x86_64' || \
        echo any
    )
    old_pkgver=$(
      grep '^pkgver=' "${git_repo_path}/${repo}/${pkgname}/PKGBUILD" | \
        cut -d'=' -f2
    )
    new_pkgver=$(
      printf '%s\n' "${archlinuxewe_PKGBUILD}" | \
        grep '^pkgver=' | \
        cut -d'=' -f2
    )
    if [ "${old_pkgver}" = "${new_pkgver}" ]; then
      >&2 echo 'nothing to do'
      exit
    fi
    sha512sums=$(
      cd "${archlinuxewe_git_path}/${pkgname}32"
      makepkg -g | \
        sed '
          s/^/\\1/
          $! s/$/\\n/
        ' | \
        tr -d '\n'
    )
    sed -i '
      s/^pkgver=.*/pkgver='"${new_pkgver}"'/
      s/^pkgrel=.*/pkgrel=1/
      /^\s*sha512sums=(/ {
        :sum_loop
        $b
        N
        s/^\(\s*\)sha512sums=(.*)/'"${sha512sums}"'/
        T sum_loop
      }
    ' "${git_repo_path}/${repo}/${pkgname}/PKGBUILD"
    scp -r "${upstream_git_path}/${pkgname}/repos/${repo}-${repo_arch}" \
      "arch32-test:${pkgname}"
    if ! ssh arch32-test '
        cd "'"${pkgname}"'"
        cat >> PKGBUILD
        makepkg --verifysource
      ' < \
        "${git_repo_path}/${repo}/${pkgname}/PKGBUILD"; then
      >&2 echo 'something went wrong'
      exit 1
    fi
    git -C "${git_repo_path}" commit "${repo}/${pkgname}/PKGBUILD" -m "${repo}/${pkgname}: ${old_pkgver} -> ${new_pkgver}"
  ;;
  'checksum')
    used_upstream_git_path="${upstream_git_path}"
    repo_arch=$(
      git -C "${used_upstream_git_path}/${pkgname}/repos" archive HEAD -- 2>/dev/null | \
        tar -t 2>/dev/null | \
        sed -n '
          s/^'"${repo}-"'//
          T
          s@/$@@
          p
        ' | \
        head -n1
    )
    if [ -z "${repo_arch}" ]; then
      used_upstream_git_path="${upstream_community_git_path}"
      repo_arch=$(
        git -C "${used_upstream_git_path}/${pkgname}/repos" archive HEAD -- | \
          tar -t | \
          sed -n '
            s/^'"${repo}-"'//
            T
            s@/$@@
            p
          ' | \
          head -n1
      )
    fi
    scp -r "${used_upstream_git_path}/${pkgname}/repos/${repo}-${repo_arch}" \
      "arch32-test:${pkgname}"
    ssh arch32-test '
      cd "'"${pkgname}"'"
      cat >> PKGBUILD
      echo '"'"'[ "${arch[0]}" = any ] || arch+=(i686 pentium4)'"'"' >> PKGBUILD
    ' < "${git_repo_path}/${repo}/${pkgname}/PKGBUILD"
    update_checksum
    scp "${used_upstream_git_path}/${pkgname}/repos/${repo}-${repo_arch}/PKGBUILD" \
      "arch32-test:${pkgname}/"
    if ! ssh arch32-test '
      cd "'"${pkgname}"'"
      cat >> PKGBUILD
      echo '"'"'[ "${arch[0]}" = any ] || arch+=(i686 pentium4)'"'"' >> PKGBUILD
      makepkg --verifysource
    ' < "${git_repo_path}/${repo}/${pkgname}/PKGBUILD"; then
      >&2 echo 'something went wrong'
      exit 1
    fi
    git -C "${git_repo_path}" commit "${repo}/${pkgname}/PKGBUILD" -m "${repo}/${pkgname}: new version => new checksum"
  ;;
  'version and checksum without upstream')
    newver=$(
      "${base_dir}/watch-versions" -m "${pkgname}" \
        | sed '
          s/^newver="\([^"]\+\)";$/\1/
          t
          d
        '
    )
    if [ -z "${newver}" ]; then
      >&2 printf 'watch-versions reports no new version for %s\n' "${pkgname}"
      exit
    fi
    sed -i '
      s/^epoch=.*$/epoch='"'${newver%:*}'"'/
      s/^pkgver=.*$/pkgver='"'${newver#*:}'"'/
      s/^pkgrel=.*$/pkgrel='"'1'"'/
    ' "${git_repo_path}/${repo}/${pkgname}/PKGBUILD"
    git -C "${git_repo_path}/${repo}/${pkgname}" diff HEAD -- PKGBUILD
    scp -r "${git_repo_path}/${repo}/${pkgname}" \
      "arch32-test:${pkgname}"
    update_checksum
    if ! ssh arch32-test '
        cd "'"${pkgname}"'"
        cat > PKGBUILD
        echo '"'"'[ "${arch[0]}" = any ] || arch+=(i686 pentium4)'"'"' >> PKGBUILD
        makepkg --verifysource
      ' < "${git_repo_path}/${repo}/${pkgname}/PKGBUILD"; then
      >&2 echo 'something went wrong'
      exit 1
    fi
    git -C "${git_repo_path}" commit "${repo}/${pkgname}/PKGBUILD" -m "${repo}/${pkgname}: new version => new checksum"
  ;;
  'kernel without upstream')
    infos=$(
      "${base_dir}/watch-versions" "${pkgname}"
    )
    if [ -z "${infos}" ]; then
      >&2 echo 'Nothing to do.'
      exit
    fi
    old_pkgver=$(
      printf '%s\n' "${infos}" | \
        cut -d' ' -f4
    )
    new_pkgver=$(
      printf '%s\n' "${infos}" | \
        cut -d' ' -f2
    )
    sed -i '
      s/^pkgver=.*$/pkgver='"'${new_pkgver}'"'/
      s/^pkgrel=.*$/pkgrel='"'1'"'/
    ' "${git_repo_path}/${repo}/${pkgname}/PKGBUILD"
    scp -r "${git_repo_path}/${repo}/${pkgname}" 'arch32-test:'
    update_checksum

    case "${pkgname}" in
      'linux-olpc-xo1'|'linux-pae')
        config_name=$(
          ls "${git_repo_path}/${repo}/${pkgname}" \
          | grep '^config'
        )
        scp "${git_repo_path}/${repo}/${pkgname}/PKGBUILD" 'arch32-test:'"${pkgname}/"
        ssh arch32-test '
          cd '"${pkgname}"'
          sed -i '"'"'
            /make oldconfig/ s/^\s*#//
            s/^}$/return 1\n\0/
          '"'"' PKGBUILD
          makepkg -fcrs --asdeps --noconfirm
          cp src/linux-'"${new_pkgver}"'/.config config
        '
        update_checksum
        scp 'arch32-test:'"${pkgname}"'/config' "${git_repo_path}/${repo}/${pkgname}/${config_name}"
        git -C "${git_repo_path}/${repo}/${pkgname}" add 'PKGBUILD' "${config_name}"
      ;;
      *)
        >&2 printf 'Whoops, I thought %s should be updated as %s, but I don'"'"'t know the details.\n' \
          "${pkgname}" "${update_path}"
        exit 1
      ;;
    esac

    git -C "${git_repo_path}" commit -m "${repo}/${pkgname}: ${old_pkgver} -> ${new_pkgver}"
  ;;
  'kernel with upstream')
    old_revision=$(
      sed -n '
        s/^# upstream git\( revision\)\?: *//
        T
        p
      ' "${git_repo_path}/${repo}/${pkgname}/PKGBUILD"
    )
    if [ -z "${old_revision}" ]; then
      >&2 printf 'Cannot determine old upstream git revision of "%s".\n' "${pkgname}"
      >&2 echo '"# upstream git revision: ..." line is missing.'
      exit 1
    fi
    config_names=$(
      git -C "${git_repo_path}/${repo}/${pkgname}" archive HEAD -- | \
        tar -t | \
        grep '^config\($\|\.\)' | \
        tr '\n' ' '
    )
    diff=$(
      diff -u <(
          git -C "${upstream_git_path}/${pkgname}/repos/${repo}-x86_64" archive "${old_revision}" -- config | \
            tar -Ox | \
            sort
        ) \
        <(
          git -C "${upstream_git_path}/${pkgname}/repos/${repo}-x86_64" archive HEAD -- config | \
            tar -Ox | \
            sort
        ) | \
        grep '^[+-].' | \
        grep -v '^+++\|^---'
    )
    if [ -z "${diff}" ]; then
      >&2 echo 'nothing changed.'
      exit 0
    fi
    for config_name in ${config_names}; do
      {
        grep -vxF "$(
          printf '%s\n' "${diff}" | \
            sed '
              s/^-//
              t
              d
            '
        )" "${git_repo_path}/${repo}/${pkgname}/${config_name}"
        printf '%s\n' "${diff}" | \
          sed '
            s/^+//
            t
            d
          '
      } | \
        sponge "${git_repo_path}/${repo}/${pkgname}/${config_name}"
    done
    sed -i '
      1 s/^#.*$/# upstream git revision: '"$(
        git -C "${upstream_git_path}" rev-parse HEAD
      )"'/
      s/'"$(
        git -C "${upstream_git_path}/${pkgname}/repos/${repo}-x86_64" archive "${old_revision}" -- config | \
          tar -Ox | \
          sha256sum | \
          awk '{print $1}'
      )"'/'"$(
        git -C "${upstream_git_path}/${pkgname}/repos/${repo}-x86_64" archive HEAD -- config | \
          tar -Ox | \
          sha256sum | \
          awk '{print $1}'
      )"'/g
      ' "${git_repo_path}/${repo}/${pkgname}/PKGBUILD"
    {
      git -C "${upstream_git_path}/${pkgname}/repos/${repo}-x86_64" archive HEAD --
      git -C "${git_repo_path}/${repo}/${pkgname}" archive HEAD \
      | tar -tf - \
      | grep -vxF "$(
        printf '%s\n' \
          'PKGBUILD' \
          ${config_names}
      )" \
      | xargs -r git -C "${git_repo_path}/${repo}/${pkgname}" archive HEAD --
      for config_name in ${config_names}; do
        tar -c -C "${git_repo_path}/${repo}/${pkgname}" "${config_name}"
      done
    } | \
      ssh arch32-test '
        mkdir "'"${pkgname}"'"
        tar -xiC "'"${pkgname}"'"
      '
    ssh arch32-test '
      cd "'"${pkgname}"'"
      cat >> PKGBUILD
      sed -i '"'"'
        '"$(
          for config_name in ${config_names}; do
            printf 's/'
            git -C "${git_repo_path}/${repo}/${pkgname}" archive HEAD -- "${config_name}" | \
              tar -Ox | \
              sha256sum | \
              awk '{print $1}' | \
              tr -d '\n'
            printf '/SKIP/g\n'
          done
        )"'
        /^arch=[^#]*any/!{
          /^arch=(/s/(/(i486 i686 pentium4 /
        }
        /^\s*cp .\+ \.config\s*$/ a make olddefconfig
        /^}$/ i mv .config .processed-config \
        return 1
      '"'"' PKGBUILD
    ' < \
      "${git_repo_path}/${repo}/${pkgname}/PKGBUILD"
    ssh arch32-test '
      cd "'"${pkgname}"'"
      eval "$(grep '"'"'^\(pkgver\|_srcname\)='"'"' PKGBUILD)"
      for config_name in '"${config_names}"'; do
        rm -rf --one-file-system src pkg
        if [ "${config_name}" = "config" ]; then
          makepkg -fcrs --asdeps --noconfirm
        else
          CARCH=${config_name#config.} makepkg -fcrs --asdeps --noconfirm
        fi
        mv src/${_srcname}/.processed-config ${config_name} || exit $?
      done
    ' || exit $?
    for config_name in ${config_names}; do
      scp "arch32-test:${pkgname}/${config_name}" "${git_repo_path}/${repo}/${pkgname}/"
    done
    sed -i "$(
      for config_name in ${config_names}; do
        printf 's/'
        git -C "${git_repo_path}/${repo}/${pkgname}" archive HEAD -- "${config_name}" | \
          tar -Ox | \
          sha256sum | \
          awk '{print $1}' | \
          tr -d '\n'
        printf '/'
        sha256sum "${git_repo_path}/${repo}/${pkgname}/${config_name}" | \
          awk '{print $1}' | \
          tr -d '\n'
        printf '/g\n'
      done
    )" "${git_repo_path}/${repo}/${pkgname}/PKGBUILD"
    git -C "${git_repo_path}/${repo}/${pkgname}" commit PKGBUILD ${config_names} -m "${repo}/${pkgname}: new version => new config => new checksum"
  ;;
  *)
    >&2 printf 'Whoops, I thought I knew how to update %s, but apparently I don'"'"'t.\n' \
      "${update_path}"
    exit 1
  ;;
esac

ssh arch32-test 'rm -rf --one-file-system '"${pkgname}"