#!/usr/bin/env bash
# Public entry point: curl -fsSL <release-url>/install.sh | sudo bash
# A complete function is parsed before any installation can begin.
after2am_install() (
  set -euo pipefail
  umask 077
  [[ "$(uname -s)" == Linux ]] || { echo 'Run this command in your Linux VPS SSH terminal.' >&2; exit 1; }
  [[ $EUID -eq 0 ]] || { echo 'Run this installer with sudo bash.' >&2; exit 1; }
  local work
  work="$(mktemp -d)"
  trap 'rm -rf "$work"' EXIT
  trap 'exit 130' INT
  trap 'exit 143' TERM
  echo 'Downloading After2AM Remote Cache 0.3.0…'
  curl --fail --silent --show-error --location --proto '=https' --proto-redir '=https' \
    --retry 3 --connect-timeout 30 --max-time 900 \
    'https://git.davidrelich.cz/api/packages/davidrelich/generic/after2am-remote-cache/0.3.0/After2AM-Remote-Cache-0.3.0-Linux.run' --output "$work/installer.run"
  local actual
  actual="$(sha256sum "$work/installer.run")"
  [[ "${actual%% *}" == 'c9b9748dc3aa29ce5368708c16bba490f083b101b24750e8e3eaf2c64a1030d6' ]] || { echo 'Installer checksum verification failed; nothing installed.' >&2; exit 1; }
  bash "$work/installer.run" "$@"
)
after2am_install "$@"
