#!/bin/bash
# LibreVault updates itself. Installed as /usr/lib/librevault/self-update and run as root by pkexec (polkit action org.libreportal.update.librevault), with no arguments:
# there is nothing the caller can choose. It does exactly one thing, and refuses at the first thing that is not right:
#   1. reads https://libreportal.org/download/librevault-linux.json (version, sha256, size)
#   2. downloads https://libreportal.org/download/librevault-linux.deb into a private folder
#   3. refuses unless the download's sha256 is the one in that file
#   4. refuses unless the package is called librevault, is newer than the installed one, and is the version the file named
#   5. installs it. Nothing else is ever installed, removed or run from here.
# The last line it prints on a refusal starts with "LPERR: " and is in plain words, for the app to show.
set -euo pipefail
PKG='librevault'; STEM='librevault-linux'; BASE='https://libreportal.org/download'

fail() { echo "LPERR: $1" >&2; exit 3; }

if [ "$(id -u)" -eq 0 ]; then
  # As root nothing from the caller's environment is used: a fixed PATH and the real address.
  export PATH=/usr/sbin:/usr/bin:/sbin:/bin
  unset LD_PRELOAD LD_LIBRARY_PATH
else
  # Not root: it cannot install anything. This only lets the tests point it at fake tools and a fake address.
  BASE="${LP_UPDATE_BASE:-$BASE}"
  [ -z "${LP_UPDATE_BIN:-}" ] || export PATH="$LP_UPDATE_BIN:$PATH"
fi
umask 077

lock="${LP_UPDATE_LOCK:-/run/lock/$PKG-self-update.lock}"
{ exec 9>"$lock"; } 2>/dev/null || lock=""
if [ -n "$lock" ]; then flock -n 9 || fail "An update is already running."; fi

work="$(mktemp -d)"
trap 'rm -rf "$work"' EXIT
get() { curl --proto '=https,http' --tlsv1.2 -fsSL --max-time "$1" --max-filesize "$2" -o "$3" "$4"; }
[ "$(id -u)" -ne 0 ] || get() { curl --proto '=https' --tlsv1.2 -fsSL --max-time "$1" --max-filesize "$2" -o "$3" "$4"; }

dpkg-query -W -f='${Status}' "$PKG" 2>/dev/null | grep -q 'install ok installed' || fail "LibreVault is not installed from its package."

get 30 16384 "$work/v.json" "$BASE/$STEM.json" || fail "The update could not be found. Check your connection and try again."
flat="$(tr -d '\n\r' < "$work/v.json")"
field() { printf '%s' "$flat" | sed -n "s/.*\"$1\"[[:space:]]*:[[:space:]]*\"\{0,1\}\([^\",}]*\)\"\{0,1\}.*/\1/p"; }
ver="$(field version)"; sha="$(field sha256)"; size="$(field size)"
[[ "$ver" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]] || fail "The update information was not right, so nothing was installed."
[[ "$sha" =~ ^[0-9a-f]{64}$ ]] || fail "The update information was not right, so nothing was installed."
[[ "$size" =~ ^[0-9]{1,9}$ && "$size" -gt 0 && "$size" -le 419430400 ]] || fail "The update information was not right, so nothing was installed."

deb="$work/$PKG.deb"
get 1800 "$size" "$deb" "$BASE/$STEM.deb" || fail "The update could not be downloaded. Check your connection and try again."
[ "$(sha256sum "$deb" | cut -d' ' -f1)" = "$sha" ] || fail "The update did not arrive whole, so it was not installed. Please try again."
[ "$(dpkg-deb -f "$deb" Package 2>/dev/null)" = "$PKG" ] || fail "This download is not a genuine LibreVault update, so it was not installed."
debver="$(dpkg-deb -f "$deb" Version)"
[ "${debver%%-*}" = "$ver" ] || fail "The update information did not match the package, so it was not installed."
cur="$(dpkg-query -W -f='${Version}' "$PKG")"
dpkg --compare-versions "$debver" gt "$cur" || fail "LibreVault is already the newest version."

DEBIAN_FRONTEND=noninteractive apt-get install -y -o Dpkg::Options::=--force-confold "$deb" >"$work/apt.log" 2>&1 || {
  tail -n 5 "$work/apt.log" >&2 || true
  fail "The update could not be installed. Please try again."
}
echo "Updated LibreVault to $ver"
