summaryrefslogtreecommitdiff
path: root/build_files/patch-bazzite-deck-identity.sh
blob: d3ee8607f649043414203e95d345a6a981df840a (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
#!/usr/bin/bash

set -euo pipefail

root="${AEROCORE_DECK_COMPAT_ROOT:-}"
root="${root%/}"

target_path() {
  printf '%s%s\n' "$root" "$1"
}

patch_condition() {
  local file="$1"
  local old_condition="$2"
  local new_condition="$3"
  local expected_count="$4"
  local old_count new_count patched_file

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

  new_count="$(grep -Foc "$new_condition" "$file" || true)"
  if [[ "$new_count" -eq "$expected_count" ]]; then
    return 0
  fi

  old_count="$(grep -Foc "$old_condition" "$file" || true)"
  if [[ "$old_count" -ne "$expected_count" ]]; then
    echo "Unable to locate the expected Bazzite Deck image check in $file" >&2
    echo "Expected $expected_count occurrence(s), found old=$old_count new=$new_count" >&2
    exit 1
  fi

  patched_file="$(mktemp)"
  awk \
    -v old_condition="$old_condition" \
    -v new_condition="$new_condition" \
    ' {
      position = index($0, old_condition)
      if (position) {
        $0 = substr($0, 1, position - 1) new_condition substr($0, position + length(old_condition))
      }
      print
    }' "$file" > "$patched_file"

  cp "$patched_file" "$file"
  rm -f "$patched_file"

  new_count="$(grep -Foc "$new_condition" "$file" || true)"
  if [[ "$new_count" -ne "$expected_count" ]]; then
    echo "Failed to patch the Bazzite Deck image check in $file" >&2
    exit 1
  fi
}

patch_steam_state_migration() {
  local file="$1"
  # shellcheck disable=SC2016
  local condition='if [[ $IMAGE_NAME =~ "deck" || $IMAGE_NAME == aerocore-os ]]; then'
  local marker='AeroCore Steam state migration'
  local patched_file

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

  if grep -Fq "$marker" "$file"; then
    return 0
  fi

  if [[ "$(grep -Foc "$condition" "$file" || true)" -ne 1 ]]; then
    echo "Unable to locate the AeroCore Steam bootstrap condition in $file" >&2
    exit 1
  fi

  patched_file="$(mktemp)"
  awk -v condition="$condition" '
    !inserted && $0 == condition {
      print
      print "  # AeroCore Steam state migration"
      print "  # Older AeroCore images could leave ~/.steam/steam as a directory."
      print "  # Preserve it before restoring the symlink expected by Steam."
      print "  STEAM_DATA_HOME=\"$HOME/.local/share/Steam\""
      print "  STEAM_DATA_LINK=\"$HOME/.steam/steam\""
      print "  if [[ -d \"$STEAM_DATA_LINK\" && ! -L \"$STEAM_DATA_LINK\" && -x \"$STEAM_DATA_HOME/steam.sh\" ]]; then"
      print "    STEAM_DATA_BACKUP=\"${STEAM_DATA_LINK}.aerocore-backup-$(date +%s)-$$\""
      print "    STEAM_DATA_TEMP=\"${STEAM_DATA_LINK}.aerocore-link-$$\""
      print "    if ln -s \"$STEAM_DATA_HOME\" \"$STEAM_DATA_TEMP\" && mv -- \"$STEAM_DATA_LINK\" \"$STEAM_DATA_BACKUP\" && mv -- \"$STEAM_DATA_TEMP\" \"$STEAM_DATA_LINK\"; then"
      print "      :"
      print "    else"
      print "      rm -f -- \"$STEAM_DATA_TEMP\""
      print "    fi"
      print "  fi"
      inserted=1
      next
    }
    { print }
    END { if (!inserted) exit 1 }
  ' "$file" > "$patched_file"

  cp "$patched_file" "$file"
  rm -f "$patched_file"

  if ! grep -Fq "$marker" "$file"; then
    echo "Failed to add the AeroCore Steam state migration to $file" >&2
    exit 1
  fi
}

patch_desktop_branding_migration() {
  local file="$1"
  local marker='AeroCore desktop branding migration'
  local condition='pkexec /usr/libexec/bazzite-privileged-user-setup "$USER"'
  local patched_file

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

  if grep -Fq "$marker" "$file"; then
    return 0
  fi

  if [[ "$(grep -Foc "$condition" "$file" || true)" -ne 1 ]]; then
    echo "Unable to locate the Bazzite privileged user setup call in $file" >&2
    exit 1
  fi

  patched_file="$(mktemp)"
  awk -v condition="$condition" '
    !inserted && $0 == condition {
      print
      print ""
      print "  # AeroCore desktop branding migration"
      print "  # The image contains the correct defaults. This only repairs values"
      print "  # persisted by an older Bazzite/AeroCore deployment."
      print "  if [[ -f /etc/xdg/kcm-about-distrorc ]] && grep -Fxq \"Name=Bazzite\" /etc/xdg/kcm-about-distrorc; then"
      print "    pkexec /usr/libexec/aerocore-desktop-branding-privileged"
      print "  fi"
      print "  /usr/libexec/aerocore-desktop-branding"
      inserted=1
      next
    }
    { print }
    END { if (!inserted) exit 1 }
  ' "$file" > "$patched_file"

  cp "$patched_file" "$file"
  rm -f "$patched_file"

  if ! grep -Fq "$marker" "$file"; then
    echo "Failed to add the AeroCore desktop branding migration to $file" >&2
    exit 1
  fi
}

# Keep AeroCore's public image-name, while restoring the Deck capability checks
# used by Bazzite's runtime scripts. These are literal source lines; variable
# expansion must happen later, when the scripts run on the installed system.
# shellcheck disable=SC2016
patch_condition \
  "${1:-$(target_path /usr/libexec/bazzite-hardware-setup)}" \
  'if [[ "$IMAGE_NAME" != *deck* && "$IMAGE_NAME" != *dx* ]]; then' \
  'if [[ "$IMAGE_NAME" != aerocore-os && "$IMAGE_NAME" != *deck* && "$IMAGE_NAME" != *dx* ]]; then' \
  1

# The hardware setup has a second Deck-only block after the top-level image
# guard. Keep it active for AeroCore as well.
# shellcheck disable=SC2016
patch_condition \
  "${1:-$(target_path /usr/libexec/bazzite-hardware-setup)}" \
  'if [[ $IMAGE_NAME =~ "deck" ]]; then' \
  'if [[ $IMAGE_NAME =~ "deck" || $IMAGE_NAME == aerocore-os ]]; then' \
  1

# bazzite-steam gates the Steam bootstrap archive and -steamdeck flag on the
# image name. The user setup script has the same Deck-only gating in three
# places.
# shellcheck disable=SC2016
patch_condition \
  "$(target_path /usr/bin/bazzite-steam)" \
  'if [[ $IMAGE_NAME =~ "deck" ]]; then' \
  'if [[ $IMAGE_NAME =~ "deck" || $IMAGE_NAME == aerocore-os ]]; then' \
  1

patch_steam_state_migration "$(target_path /usr/bin/bazzite-steam)"

# shellcheck disable=SC2016
patch_condition \
  "$(target_path /usr/libexec/bazzite-user-setup)" \
  'if [[ $IMAGE_NAME =~ "deck" ]]; then' \
  'if [[ $IMAGE_NAME =~ "deck" || $IMAGE_NAME == aerocore-os ]]; then' \
  3

patch_desktop_branding_migration "$(target_path /usr/libexec/bazzite-user-setup)"