summaryrefslogtreecommitdiff
path: root/build_files/patch-bazzite-rollback-helper.sh
blob: 6d565b0fe6942388ce6a0750bdf57c778afd3611 (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
#!/usr/bin/bash

set -euo pipefail

file=/usr/bin/bazzite-rollback-helper

if [[ ! -f "$file" ]]; then
  echo "Unable to locate Bazzite rollback helper: $file" >&2
  exit 1
fi

if grep -Fq 'current_image_prefix()' "$file"; then
  exit 0
fi

patched_file=$(mktemp)
awk '
  /^signing_scheme\(\) \{/ {
    print "signing_scheme() {"
    print "  local current=$(rpm-ostree status -b --json | jq -r '\''.deployments | map(select(.booted == true)) | first | .[\"container-image-reference\"] // empty'\'')"
    print "  local scheme=\"ostree-unverified-registry\""
    print "  [[ \"$current\" == *\"ostree-image-signed\"* ]] && scheme=\"ostree-image-signed\""
    print "  echo \"$scheme:docker://\""
    print "}"
    print ""
    print "current_image_prefix() {"
    print "  local current"
    print "  current=$(rpm-ostree status -b --json | jq -r '\''.deployments | map(select(.booted == true)) | first | .[\"container-image-reference\"] // empty'\'')"
    print "  if [[ -z \"$current\" ]]; then"
    print "    echo \"Unable to determine the current booted image reference.\" >&2"
    print "    return 1"
    print "  fi"
    print "  current=\"${current#ostree-image-signed:}\""
    print "  current=\"${current#ostree-unverified-registry:}\""
    print "  current=\"${current#docker://}\""
    print "  echo \"${current%/*}\""
    print "}"
    skip=1
    next
  }
  skip && /^warn_if_de_mismatch\(\) \{/ { skip=0; print; next }
  skip { next }
  {
    gsub("ostree-image-signed:docker://[^\"]+/", "$(signing_scheme)$(current_image_prefix)/")
    gsub("\\$\\(signing_scheme\\)/\\$\\(current_image_prefix\\)/", "$(signing_scheme)$(current_image_prefix)/")
    gsub("\\$\\(signing_scheme\\)/", "$(signing_scheme)")
    print
  }
' "$file" > "$patched_file"

install -m 0755 "$patched_file" "$file"
rm -f "$patched_file"