#!/bin/sh
# Material Boot: download the latest release, verify it, and start its installer.
#
#   curl -fsSL https://lucacraft89.github.io/material-boot-core/install.sh | bash
#   curl -fsSL https://lucacraft89.github.io/material-boot-core/install.sh | bash -s -- --with plymouth
#
# Everything after `-s --` goes to the installer (`--help` lists it). Nothing is changed before the installer's own screen; it asks for sudo itself.
# MATERIAL_BOOT_VERSION=1.2.0 picks a version instead of the latest; MATERIAL_BOOT_BASE=URL downloads from somewhere else (tests, a mirror).
set -eu

REPO=LucaCraft89/material-boot-core
BASE=${MATERIAL_BOOT_BASE:-https://github.com/$REPO/releases}
say() { printf '%s\n' "$*"; }
die() { printf 'material-boot: %s\n' "$*" >&2; exit 1; }

for t in tar mktemp; do command -v "$t" >/dev/null 2>&1 || die "'$t' is needed and was not found"; done
if command -v curl >/dev/null 2>&1; then
  get() { curl -fsSL -o "$2" "$1"; }
  final_url() { curl -fsSLI -o /dev/null -w '%{url_effective}' "$1"; }
elif command -v wget >/dev/null 2>&1; then
  get() { wget -q -O "$2" "$1"; }
  final_url() { wget -q --server-response --spider "$1" 2>&1 | sed -n 's/^ *Location: *//p' | tail -n1; }
else
  die "curl or wget is needed and neither was found"
fi
if command -v sha256sum >/dev/null 2>&1; then sum() { sha256sum "$1" | cut -d' ' -f1; }
elif command -v shasum >/dev/null 2>&1; then sum() { shasum -a 256 "$1" | cut -d' ' -f1; }
else die "sha256sum or shasum is needed to verify the download and neither was found"; fi

VERSION=${MATERIAL_BOOT_VERSION:-}
if [ -z "$VERSION" ]; then
  url=$(final_url "$BASE/latest") || die "could not look up the latest release at $BASE/latest"
  VERSION=${url##*/}; VERSION=${VERSION#v}
  case $VERSION in [0-9]*.[0-9]*) ;; *) die "could not read a version from '$url' (is there a release yet?)" ;; esac
fi
case $VERSION in *[!0-9.]*|"") die "'$VERSION' is not a version number" ;; esac

TMP=$(mktemp -d "${TMPDIR:-/tmp}/material-boot.XXXXXX") || die "could not make a temporary directory"
trap 'rm -rf "$TMP"' EXIT INT TERM
NAME=material-boot-$VERSION
say "Material Boot $VERSION: downloading ..."
get "$BASE/download/v$VERSION/$NAME.tar.gz" "$TMP/$NAME.tar.gz" || die "could not download $BASE/download/v$VERSION/$NAME.tar.gz"
get "$BASE/download/v$VERSION/$NAME.tar.gz.sha256" "$TMP/$NAME.sha256" || die "could not download the checksum of $NAME.tar.gz"
want=$(cut -d' ' -f1 "$TMP/$NAME.sha256" | head -n1)
have=$(sum "$TMP/$NAME.tar.gz")
if [ -z "$want" ] || [ "$want" != "$have" ]; then
  die "the download does not match its checksum (wanted ${want:-nothing}, got $have): nothing was installed"
fi
say "checksum ok"
tar -xzf "$TMP/$NAME.tar.gz" -C "$TMP" || die "could not unpack the download"
[ -f "$TMP/$NAME/install.sh" ] || die "the download has no install.sh"

# The screen needs a keyboard, and stdin is the pipe: use the terminal. With none at all, ask nothing.
interactive=0
if (: </dev/tty) 2>/dev/null; then interactive=1; fi
cd "$TMP/$NAME"
if [ "$interactive" = 1 ]; then
  sh ./install.sh "$@" </dev/tty
else
  say "(no terminal: installing with the defaults, no questions)"
  sh ./install.sh --yes "$@" </dev/null
fi
