#!/bin/sh
# alpha-update - the PUBLIC ALPHA update channel hook.
# This edition updates ONLY from the public update sandbox: the one
# address YOU set in update.conf. There is NO built-in address in
# this script - no server is contacted until you set one.
_HOME="${ALPHA_HOME:-$HOME/.alpha-shell}"
CONF="$_HOME/update.conf"
CUR="?"; [ -f "$_HOME/VERSION" ] && CUR=$(cat "$_HOME/VERSION")
echo "ALPHA UPDATE - public channel"
echo "Installed version: $CUR"
echo
URL=""
if [ -f "$CONF" ]; then
  URL=$(grep '^MANIFEST_URL=' "$CONF" 2>/dev/null | head -1 | cut -d= -f2-)
fi
if [ -z "$URL" ]; then
  echo "No public update source is set yet, so nothing was checked"
  echo "and nothing was downloaded."
  echo
  echo "When the public update channel opens, its manifest address"
  echo "will ship with the release notes. To use it, put one line in:"
  echo "  $CONF"
  echo "    MANIFEST_URL=<the public manifest address>"
  echo "This shell never phones anywhere else. Ever."
  exit 0
fi
# A source is set: fetch the manifest, compare, and only ever
# download something whose sha256 checks out.
TMP=$(mktemp -d) || exit 1
trap 'rm -rf "$TMP"' EXIT INT TERM
echo "Checking the public manifest you configured..."
if command -v curl >/dev/null 2>&1; then
  curl -fsSL -o "$TMP/manifest.txt" "$URL" || { echo "The manifest did not answer. Nothing changed."; exit 1; }
elif command -v wget >/dev/null 2>&1; then
  wget -q -O "$TMP/manifest.txt" "$URL" || { echo "The manifest did not answer. Nothing changed."; exit 1; }
else
  echo "No curl or wget in this shell - cannot fetch the manifest."
  echo "Nothing changed."
  exit 1
fi
NEWV=$(grep '^version=' "$TMP/manifest.txt" | head -1 | cut -d= -f2-)
TURL=$(grep '^tarball=' "$TMP/manifest.txt" | head -1 | cut -d= -f2-)
TSHA=$(grep '^sha256=' "$TMP/manifest.txt" | head -1 | cut -d= -f2-)
if [ -z "$NEWV" ] || [ -z "$TURL" ] || [ -z "$TSHA" ]; then
  echo "The manifest is missing version/tarball/sha256 lines."
  echo "Nothing changed - an incomplete manifest is never trusted."
  exit 1
fi
if [ "$NEWV" = "$CUR" ]; then
  echo "You are current: version $CUR is the latest on the channel."
  exit 0
fi
echo "New version available: $NEWV (you have $CUR). Downloading..."
if command -v curl >/dev/null 2>&1; then
  curl -fsSL -o "$TMP/pkg.tar.gz" "$TURL" || { echo "Download failed. Nothing changed."; exit 1; }
else
  wget -q -O "$TMP/pkg.tar.gz" "$TURL" || { echo "Download failed. Nothing changed."; exit 1; }
fi
echo "$TSHA  $TMP/pkg.tar.gz" > "$TMP/pkg.sha"
if ! sha256sum -c "$TMP/pkg.sha" >/dev/null 2>&1; then
  echo "VERDICT: REFUSED - the download's sha256 does NOT match the"
  echo "manifest. It was deleted, not installed. Nothing changed."
  exit 1
fi
echo "sha256 verified. Unpacking to $TMP/pkg ..."
mkdir -p "$TMP/pkg" && tar xzf "$TMP/pkg.tar.gz" -C "$TMP/pkg" || { echo "Unpack failed. Nothing changed."; exit 1; }
INST=$(find "$TMP/pkg" -maxdepth 2 -name install.sh | head -1)
echo
echo "Verified and ready. To finish the update, run:"
echo "  sh \"$INST\""
echo "Then restart your shell."
