#!/bin/bash
set -eu
cdn_list="https://client-update.steamstatic.com https://client-update.akamai.steamstatic.com https://client-update.fastly.steamstatic.com"
beta_channel=steamdeck_publicbeta
stable_channel=steamdeck_stable
steam_root=${XDG_DATA_HOME:-$HOME/.local/share}/Steam
channel_state=$steam_root/package/droiddeck-channel
pinned=1
wanted=${DD_STEAM_CHANNEL:-$(cat "$HOME/.config/droiddeck/steam-channel" 2>/dev/null || true)}
case $wanted in
  stable|"$stable_channel") steam_channel=$stable_channel ;;
  beta|"$beta_channel") steam_channel=$beta_channel ;;
  *)
    pinned=
    steam_channel=$beta_channel
    [ "$(cat "$channel_state" 2>/dev/null)" = "$stable_channel" ] && steam_channel=$stable_channel
    ;;
esac
if [ "${1:-}" = --channel ]; then
  echo "$steam_channel"
  exit 0
fi
manifest=steam_client_${steam_channel}_linuxarm64
stamp=$steam_root/package/droiddeck-installed
legacy_stamp=$steam_root/package/bannerlator-installed
unconfirmed=$steam_root/package/droiddeck-unconfirmed
repair_request=$HOME/.dd-steam-repair
parts=$steam_root/package/droiddeck-download
if [ -f "$legacy_stamp" ] && [ ! -f "$stamp" ]; then
  mv -f "$legacy_stamp" "$stamp" || true
fi
repair=""
fallback=""
starts=0
[ -f "$unconfirmed" ] && starts=$(grep -cx start "$unconfirmed" || true)
if [ -e "$repair_request" ]; then
  repair="requested"
elif [ "$starts" -ge 2 ] && ! grep -qx repaired "$unconfirmed"; then
  repair="the client was started $starts times without showing anything"
elif [ "$starts" -ge 2 ] && [ -z "$pinned" ] && [ "$steam_channel" = "$beta_channel" ] && [ "${1:-}" != --sync ]; then
  repair="the repaired $beta_channel client was started $starts more times without showing anything"
  fallback=1
fi
if [ -n "$fallback" ]; then
  echo "== STEP $(date -u +%H:%M:%S) falling back to Valve's $stable_channel Steam client: $repair"
  steam_channel=$stable_channel
  manifest=steam_client_${steam_channel}_linuxarm64
fi
if [ -n "$repair" ]; then
  echo "== STEP $(date -u +%H:%M:%S) repairing the Steam client ($repair)"
  if [ ! -f "$parts/.repair" ]; then
    rm -rf "$parts"
    mkdir -p "$parts" && : > "$parts/.repair"
  fi
  rm -f "$stamp" "$legacy_stamp" "$steam_root/.crash" "$steam_root/steam.pid" \
    "$steam_root"/package/steam_client_*.manifest "$steam_root"/package/steam_client_*.installed
  for part in "$steam_root"/package/*.zip.*; do
    [ -f "$part" ] || continue
    sum=${part##*.}
    sum=${sum%%_*}
    case $sum in
      *[!0-9a-f]*|"") continue ;;
    esac
    [ ${#sum} -eq 40 ] || continue
    [ "$(sha1sum "$part" | cut -d' ' -f1)" = "$sum" ] || rm -f "$part"
  done
fi
cache_only=
applied=
[ "${1:-}" = --cache ] && cache_only=1
sync=
if [ "${1:-}" = --sync ]; then
  case "${2:-}" in publicbeta|steamdeck_publicbeta) sync=$2 ;; *) sync=$steam_channel ;; esac
  manifest=steam_client_${sync}_linuxarm64
fi
[ -z "$sync" ] && [ -z "$cache_only" ] && [ -x "$steam_root/steamrtarm64/steam" ] && [ -f "$stamp" ] && exit 0

work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT

nap() {
  [ -n "${nap_fd:-}" ] || exec {nap_fd}<> <(:)
  read -r -t "$1" -u "$nap_fd" _ 2>/dev/null || :
}
size_of() {
  local n=0
  [ -f "$1" ] && n=$(wc -c < "$1")
  echo $((n))
}
limits=(--connect-timeout 30 --speed-limit 4096 --speed-time 60)
read -r -a delays <<< "${DD_STEAM_RETRY_DELAYS:-5 10 20 30 30}"

step() { echo "== STEP $(date -u +%H:%M:%S) $*"; }
progress() {
  local mb=$(( (done_bytes + $(size_of "$1")) / 1000000 ))
  [ -z "${3:-}" ] && [ "$mb" = "${shown_mb:-}" ] && [ "$2" = "${shown_part:-}" ] && return 0
  shown_mb=$mb shown_part=$2
  step "downloading Steam: $2 ($i/$total), $mb of $((total_bytes / 1000000)) MB${3:+ - $3}"
}

fetch() {
  local url=$1 dest=$2 size=$3 sum=$4 part=$5 failures=0 bad=0 finished=0 before rc fd wait
  while :; do
    if [ -f "$dest" ] && { [ "$finished" = 1 ] || { [ "$size" -gt 0 ] && [ "$(size_of "$dest")" -ge "$size" ]; }; }; then
      echo "$sum  $dest" | sha256sum -c --quiet 2>/dev/null && return 0
      rm -f "$dest"
      finished=0 bad=$((bad + 1))
      [ "$bad" -lt 3 ] || { echo "droiddeck-steam-install: $part does not match its checksum" >&2; return 1; }
    fi
    before=$(size_of "$dest")
    exec {fd}< <(code=$(curl -fsSL "${limits[@]}" -w '%{http_code}' -C - -o "$dest" "$url") && echo "0 $code" || echo "$? $code")
    until read -r -t 5 -u "$fd" rc http; do
      if [ $? -le 128 ]; then rc=1; break; fi
      progress "$dest" "$part"
    done
    exec {fd}<&-
    if [ "$rc" = 0 ]; then finished=1; continue; fi
    case "$rc:${http:-}" in
      22:416 | 33:*) rm -f "$dest" ;;
      22:4??)
        echo "droiddeck-steam-install: $part: HTTP $http from ${url%/*}" >&2
        return 1
        ;;
    esac
    if [ "$(size_of "$dest")" -gt "$before" ]; then failures=0; else failures=$((failures + 1)); fi
    if [ "$failures" -gt "${#delays[@]}" ]; then
      echo "droiddeck-steam-install: could not download $part (curl exit $rc); what arrived is kept for the next try" >&2
      return 1
    fi
    wait=${delays[$((failures > 0 ? failures - 1 : 0))]}
    progress "$dest" "$part" "interrupted (curl exit $rc), trying again in ${wait} s"
    nap "$wait"
  done
}

# The same decisions as fetch for the parallel workers, with curl in the foreground and a plain sleep between tries:
# the shell here cannot open /dev/fd for process substitution from a background job.
fetch_one() {
  local url=$1 dest=$2 size=$3 sum=$4 part=$5 failures=0 bad=0 finished=0 before rc http wait
  while :; do
    if [ -f "$dest" ] && { [ "$finished" = 1 ] || { [ "$size" -gt 0 ] && [ "$(size_of "$dest")" -ge "$size" ]; }; }; then
      echo "$sum  $dest" | sha256sum -c --quiet 2>/dev/null && return 0
      rm -f "$dest"
      finished=0 bad=$((bad + 1))
      [ "$bad" -lt 3 ] || { echo "droiddeck-steam-install: $part does not match its checksum" >&2; return 1; }
    fi
    before=$(size_of "$dest")
    http=$(curl -fsSL "${limits[@]}" -w '%{http_code}' -C - -o "$dest" "$url") && rc=0 || rc=$?
    if [ "$rc" = 0 ]; then finished=1; continue; fi
    case "$rc:${http:-}" in
      22:416 | 33:*) rm -f "$dest" ;;
      22:4??)
        echo "droiddeck-steam-install: $part: HTTP $http from ${url%/*}" >&2
        return 1
        ;;
    esac
    if [ "$(size_of "$dest")" -gt "$before" ]; then failures=0; else failures=$((failures + 1)); fi
    if [ "$failures" -gt "${#delays[@]}" ]; then
      echo "droiddeck-steam-install: could not download $part (curl exit $rc); what arrived is kept for the next try" >&2
      return 1
    fi
    wait=${delays[$((failures > 0 ? failures - 1 : 0))]}
    step "downloading Steam: $part - interrupted (curl exit $rc), trying again in ${wait} s"
    sleep "$wait"
  done
}

parallel=${DD_STEAM_PARALLEL:-4}
case $parallel in ''|*[!0-9]*|0) parallel=1 ;; esac
flock_command=${DD_STEAM_FLOCK:-flock}
command -v "$flock_command" > /dev/null 2>&1 || parallel=1

# unzip creates parent directories without guarding against another unzip doing the same, so two packages unpacked into
# one tree at once fail now and then. Unpacking is quick next to downloading, so the workers take turns on a flock: the
# kernel hands it on when its holder ends, however it ends, so there is no stale lock to recover.
unzip_alone() {
  local rc
  ( "$flock_command" 9 && unzip -qo "$1" -d "$steam_root" ) 9> "$state/unzip.lock" && rc=0 || rc=$?
  return $rc
}

# Takes the next unclaimed line number from a counter under a flock, and prints 0 when none is left. mkdir was tried for this
# and handed the same line to two workers now and then in the app's guest shell, so only the kernel's lock is trusted here.
claim_next() {
  local n
  while :; do
    # A counter that cannot be read or written must fail the worker: a stale or empty number would give a package to two workers or to none.
    n=$( { "$flock_command" 8 || exit 1
           n=$(cat "$state/next") || exit 1
           case $n in ''|*[!0-9]*) exit 1 ;; esac
           echo $((n + 1)) > "$state/next" || exit 1
           echo "$n"; } 8> "$state/claim.lock" ) || return 1
    [ "$n" -le "$count" ] || { echo 0; return; }
    if [ ! -e "$state/$n.done" ]; then
      : > "$state/claim.$n"
      echo "$n"
      return
    fi
  done
}

# Fetches every line of a list ("block file sum size") into $dir with up to $parallel connections. With a third
# argument each package is also unpacked into the client as it lands. One connection tops out near half of what a
# Wi-Fi link carries, so the packages arrive about twice as fast. Workers take the next unclaimed line themselves
# (claim_next) and only plain background jobs and wait on a pid are used, so nothing depends on job control.
run_parallel() {
  local list=$1 dir=$2 unpack=${3:-} state index=0 block file sum size
  local -a blocks=() files=() sums=() sizes=() pids=()
  state=$work/parallel
  rm -rf "$state"
  mkdir -p "$state"
  echo 1 > "$state/next"
  while read -r block file sum size; do
    index=$((index + 1))
    blocks[index]=$block files[index]=$file sums[index]=$sum sizes[index]=$size
    if [ -n "$unpack" ] && [ -f "$dir/$file.unpacked" ]; then
      : > "$state/$index.done"
    fi
  done < "$list"
  local count=$index total_bytes=0
  for index in $(seq 1 "$count"); do total_bytes=$((total_bytes + sizes[index])); done
  local slot
  for slot in $(seq 1 "$parallel"); do
    (
      trap ': > "$state/exit.$slot"' EXIT
      while [ ! -e "$state/failed" ]; do
        # run_parallel is called in an || list, which switches set -e off in here: a failed claim has to be checked by hand.
        index=$(claim_next) || { : > "$state/failed"; exit 1; }
        [ "$index" -gt 0 ] || exit 0
        file=${files[index]}
        fetched=""
        for host in "${cdn_order[@]}"; do
          if fetch_one "$host/$file" "$dir/$file" "${sizes[index]}" "${sums[index]}" "${blocks[index]}"; then
            fetched=$host
            break
          fi
        done
        if [ -z "$fetched" ]; then
          echo "droiddeck-steam-install: $file failed on every CDN" >&2
          : > "$state/failed"
          exit 1
        fi
        if [ -n "$unpack" ]; then
          unzip_alone "$dir/$file" || { : > "$state/failed"; exit 1; }
          : > "$dir/$file.unpacked"
          rm -f "$dir/$file"
        fi
        : > "$state/$index.done"
      done
    ) &
    pids+=($!)
  done
  local shown="" got n alive mark current last="" since=$SECONDS stall=${DD_STEAM_STALL:-90}
  while :; do
    alive=0
    for slot in $(seq 1 "$parallel"); do [ -e "$state/exit.$slot" ] || alive=1; done
    got=0 n=0 current=""
    for index in $(seq 1 "$count"); do
      if [ -e "$state/$index.done" ]; then
        got=$((got + sizes[index])) n=$((n + 1))
      elif [ -e "$state/claim.$index" ]; then
        current=$index
        [ -f "$dir/${files[index]}" ] && got=$((got + $(size_of "$dir/${files[index]}")))
      fi
    done
    mark=$((got / 1000000))
    if [ -n "$current" ] && [ "$mark" != "$shown" ]; then
      shown=$mark
      step "downloading Steam: ${blocks[current]} ($((n + 1))/$count), $mark of $((total_bytes / 1000000)) MB"
    fi
    [ "$n" -lt "$count" ] || alive=0
    if [ "$got" != "$last" ]; then last=$got since=$SECONDS; fi
    # A worker killed from outside never writes its exit marker. Stop reporting once nothing has moved for a while
    # and wait on the workers: wait returns the killed one's status, so the install fails instead of hanging.
    [ $((SECONDS - since)) -lt "$stall" ] || alive=0
    [ "$alive" = 1 ] || break
    sleep 1
  done
  local failed=""
  for slot in "${pids[@]}"; do wait "$slot" || failed=1; done
  [ ! -e "$state/failed" ] || failed=1
  rm -rf "$state"
  [ -z "$failed" ]
}

fetch_manifest() {
  cdn=""
  for candidate in $cdn_list; do
    if curl -fsSL --connect-timeout 10 --max-time 30 --retry 2 --retry-connrefused --retry-delay 3 \
        "$candidate/$manifest" -o "$work/manifest"; then
      cdn=$candidate
      break
    fi
  done
  [ -n "$cdn" ] || { echo "droiddeck-steam-install: no Steam client CDN answered for $manifest" >&2; exit 1; }
  cdn_order=("$cdn")
  for candidate in $cdn_list; do
    [ "$candidate" = "$cdn" ] || cdn_order+=("$candidate")
  done
  version=$(awk -F'"' '$2 == "version" { print $4; exit }' "$work/manifest")
  awk -F'"' '
    /^\t"/ && NF >= 3 && $4 == "" { block = $2 }
    $2 == "file" { file[block] = $4 }
    $2 == "sha2" { sha[block] = $4 }
    $2 == "size" { size[block] = $4 }
    END { for (b in file) if (b ~ /_all$/ || b ~ /_linuxarm64_linuxarm64$/) print b, file[b], sha[b], size[b] + 0 }
  ' "$work/manifest" | sort > "$work/parts"
  [ -s "$work/parts" ] && return 0
  echo "droiddeck-steam-install: no client components in $manifest; its blocks are:" \
    "$(awk -F'"' '/^\t"/ && NF >= 3 && $4 == "" { printf "%s ", $2 }' "$work/manifest")" >&2
  return 1
}
if ! fetch_manifest; then
  if [ -n "$sync" ] || [ -n "$pinned" ] || [ "$steam_channel" = "$stable_channel" ]; then
    exit 1
  fi
  echo "== STEP $(date -u +%H:%M:%S) falling back to Valve's $stable_channel Steam client: $manifest has no arm64 client"
  steam_channel=$stable_channel
  manifest=steam_client_${steam_channel}_linuxarm64
  fallback=1
  fetch_manifest || exit 1
fi
if [ -n "$cache_only" ]; then
  cache=$steam_root/package
  cached=$cache/droiddeck-cached
  [ "$(cat "$cached" 2>/dev/null)" = "$version" ] && exit 0
  awk -F'"' '
    /^\t"/ && NF >= 3 && $4 == "" { block = $2 }
    $2 == "file" { file[block] = $4 }
    $2 == "sha2" { sha[block] = $4 }
    $2 == "size" { size[block] = $4 }
    $2 == "zipvz" { vz[block] = $4 }
    $2 == "sha2vz" { vzsha[block] = $4 }
    END {
      for (b in file) {
        f = file[b]; h = sha[b]; n = size[b] + 0
        if (b in vz) { f = vz[b]; h = vzsha[b]; k = split(f, bits, "_"); n = bits[k] + 0 }
        print b, f, h, n
      }
    }
  ' "$work/manifest" | sort > "$work/cache"
  mkdir -p "$cache"
  total=$(grep -c . "$work/cache" 2>/dev/null || echo 0)
  total_bytes=$(awk '{ n += $4 } END { print n + 0 }' "$work/cache")
  done_bytes=0
  i=0
  if [ "$parallel" -gt 1 ]; then
    run_parallel "$work/cache" "$cache" || exit 1
  else
  while read -r block file sum size; do
    i=$((i + 1))
    progress "$cache/$file" "$block"
    fetched=""
    for host in "${cdn_order[@]}"; do
      if fetch "$host/$file" "$cache/$file" "$size" "$sum" "$block"; then
        fetched=$host
        break
      fi
    done
    [ -n "$fetched" ] || { echo "droiddeck-steam-install: $file failed on every CDN" >&2; exit 1; }
    done_bytes=$((done_bytes + size))
  done < "$work/cache"
  fi
  applier=
  trap 'kill "$applier" 2>/dev/null || :; wait "$applier" 2>/dev/null || :; rm -f "$cache/$manifest.installed" "$cached"; exit 143' TERM INT HUP
  echo "$version" > "$cached"
  preapply=${DD_STEAM_PREAPPLY:-/usr/local/bin/droiddeck-steam-preapply}
  if [ -x "$preapply" ] && command -v python3 >/dev/null 2>&1; then
    python3 "$preapply" "$cache" "$steam_root" "$work/manifest" "$manifest" &
    applier=$!
    if wait "$applier"; then
      cp -f "$work/manifest" "$cache/$manifest.manifest"
      chmod -R a+x "$steam_root/steamrtarm64" "$steam_root/linuxarm64" 2>/dev/null || true
      while read -r _ file _; do rm -f "$cache/$file"; done < "$work/cache"
      [ -f "$stamp" ] || applied=1
    else
      rm -f "$cache/$manifest.installed" "$cached"
    fi
  fi
  trap - TERM INT HUP
  [ -n "$applied" ] || exit 0
fi
if [ -n "$sync" ]; then
  installed=$(awk -F'"' '$2 == "version" { print $4; exit }' "$steam_root/package/$manifest.manifest" 2>/dev/null || true)
  if [ -x "$steam_root/steamrtarm64/steam" ] && [ "$installed" = "$version" ]; then
    chmod -R a+x "$steam_root/steamrtarm64" "$steam_root/linuxarm64" 2>/dev/null || true
    exit 0
  fi
  echo "== STEP $(date -u +%H:%M:%S) updating the arm64 Steam client to $version (was ${installed:-none}) for the x86-64 client"
fi
if [ -z "$applied" ]; then
total=$(grep -c . "$work/parts" 2>/dev/null || echo 0)
total_bytes=$(awk '{ n += $4 } END { print n + 0 }' "$work/parts")

mkdir -p "$parts"
for left in "$parts"/*; do
  [ -e "$left" ] || continue
  name=${left##*/}
  name=${name%.unpacked}
  grep -qF " $name " "$work/parts" || rm -rf "$left"
done
done_bytes=0
if [ "$parallel" -gt 1 ]; then
  run_parallel "$work/parts" "$parts" unpack || exit 1
else
while read -r block file sum size; do
  i=$((${i:-0} + 1))
  if [ -f "$parts/$file.unpacked" ]; then
    done_bytes=$((done_bytes + size))
    continue
  fi
  progress "$parts/$file" "$block"
  fetched=""
  for host in "${cdn_order[@]}"; do
    if fetch "$host/$file" "$parts/$file" "$size" "$sum" "$block"; then
      fetched=$host
      break
    fi
  done
  [ -n "$fetched" ] || { echo "droiddeck-steam-install: $file failed on every CDN" >&2; exit 1; }
  unzip -qo "$parts/$file" -d "$steam_root"
  : > "$parts/$file.unpacked"
  rm -f "$parts/$file"
  done_bytes=$((done_bytes + size))
done < "$work/parts"
fi
fi
find "$steam_root" -depth -name '*\\*' | while read -r odd; do
  fixed=${odd//\\//}
  mkdir -p "$(dirname "$fixed")"
  if [ -d "$odd" ]; then
    cp -a "$odd/." "$fixed/" && rm -rf "$odd"
  else
    mv -f "$odd" "$fixed"
  fi
done
chmod -R a+x "$steam_root/steamrtarm64" "$steam_root/linuxarm64" 2>/dev/null || true
if [ -n "$sync" ]; then
  cp -f "$work/manifest" "$steam_root/package/$manifest.manifest"
else
  echo "$steam_channel" > "$steam_root/package/beta"
fi
ln -sfn "$steam_root/steamrtarm64" "$steam_root/steamrtarm32"
chmod +x "$steam_root/steamrtarm64/steam" 2>/dev/null || true
mkdir -p "$HOME/.steam"
ln -sfn "$steam_root" "$HOME/.steam/root"
ln -sfn "$steam_root" "$HOME/.steam/steam"
ln -sfn "$steam_root/steamrtarm64" "$HOME/.steam/bin64"
ln -sfn "$steam_root/linux64" "$HOME/.steam/sdk64"
ln -sfn "$steam_root/linuxarm64" "$HOME/.steam/sdkarm64"
ln -sfn "$steam_root/steamrtarm64" "$HOME/.steam/binarm64"
ln -sfn "$steam_root/linux32" "$HOME/.steam/sdk32"
echo "$version" > "$stamp"
rm -rf "$parts"
if [ -n "$fallback" ]; then
  echo "$stable_channel" > "$channel_state"
  echo "== STEP $(date -u +%H:%M:%S) the Steam client now follows $stable_channel (beta in ~/.config/droiddeck/steam-channel returns to $beta_channel)"
fi
if [ -n "$repair" ]; then
  echo repaired > "$unconfirmed"
  rm -f "$repair_request"
fi
