#!/usr/bin/env bash
#
# Fairlead Panel — what https://install.fairlead-panel.com/install.sh serves.
#
#   curl -fsSL https://install.fairlead-panel.com/install.sh | sudo bash
#   curl -fsSL https://install.fairlead-panel.com/install.sh | sudo bash -s -- --version 0.7.9
#   curl -fsSL https://install.fairlead-panel.com/install.sh | sudo bash -s -- --help
#
# This file stays the same from release to release, so the address keeps one copy of it. The
# installer itself (scripts/get.sh) is in the public package registry, where every release since
# 0.7.9 publishes its own with its SHA-256. This fetches the installer of the version given with
# --version — the latest release's without one; for an older version, which has none, the latest,
# which installs the version asked for — checks the checksum and runs it with every option given
# here. The installer's --help lists them.
#
# Env: FAIRLEAD_INSTALLER_BASE — the installer's registry address (a mirror, a test server).
set -euo pipefail
umask 022

BASE="${FAIRLEAD_INSTALLER_BASE:-https://git.zen-dns.net/api/packages/velvon/generic/fairlead-installer}"
WORK=""
trap '[ -z "$WORK" ] || rm -rf "$WORK"' EXIT

say() { printf '==> %s\n' "$*" >&2; }
die() { printf 'install.sh: %s\n' "$*" >&2; exit 1; }

# fetch <url> <file>: 0 = downloaded, 2 = not there (HTTP 404); anything else stops here.
fetch() {
  if command -v curl >/dev/null 2>&1; then
    local code
    code="$(curl -sSL --retry 3 --connect-timeout 20 -o "$2" -w '%{http_code}' "$1")" || die "could not download $1"
    case "$code" in
      200) return 0 ;;
      404) return 2 ;;
      *) die "could not download $1 (HTTP $code)" ;;
    esac
  fi
  command -v wget >/dev/null 2>&1 || die "neither curl nor wget is installed"
  local rc=0
  wget -q -O "$2" "$1" || rc=$?
  case "$rc" in
    0) return 0 ;;
    8) return 2 ;; # the server answered with an error: the file is not there
    *) die "could not download $1" ;;
  esac
}

# get <version>: the installer of that version (or "latest") and its checksum into $WORK; 2 = none.
get() {
  local rc=0
  fetch "$BASE/$1/get.sh" "$WORK/get.sh" || rc=$?
  [ "$rc" = 0 ] || return "$rc"
  fetch "$BASE/$1/get.sh.sha256" "$WORK/get.sh.sha256" || die "the installer $1 has no checksum file"
}

main() {
  # The version asked for, if any (get.sh reads the same option and installs that version).
  local ver="" prev="" a
  for a in "$@"; do
    case "$prev" in --version | -V) ver="$a" ;; esac
    case "$a" in --version=*) ver="${a#*=}" ;; esac
    prev="$a"
  done
  if [ -n "$ver" ] && ! printf '%s' "$ver" | grep -qE '^[0-9]+\.[0-9]+\.[0-9]+(-[0-9]+)?$'; then
    die "invalid --version '$ver' (expected e.g. 0.7.9 or 0.7.9-340)"
  fi

  WORK="$(mktemp -d)"
  local rel="${ver%%-*}" from="" rc i
  if [ -n "$rel" ]; then
    rc=0
    get "$rel" || rc=$?
    if [ "$rc" = 0 ]; then
      from="$rel"
    else
      say "no installer of $rel in the registry (releases before 0.7.9 have none): the latest one installs $ver"
    fi
  fi
  if [ -z "$from" ]; then
    # A release replaces "latest" — for a few seconds there is none.
    for i in 1 2 3 4 5 6; do
      rc=0
      get latest || rc=$?
      [ "$rc" = 0 ] && break
      [ "$i" = 6 ] && die "the registry has no latest installer ($BASE/latest/get.sh)"
      sleep 5
    done
    from="latest"
  fi

  grep -qE '^[0-9a-f]{64}  get\.sh$' "$WORK/get.sh.sha256" || die "the installer $from has a malformed checksum file"
  (cd "$WORK" && sha256sum -c get.sh.sha256 >/dev/null 2>&1) || die "the installer $from does not match its checksum — not running it"
  say "Fairlead Panel installer: $from ($(cut -c1-12 "$WORK/get.sh.sha256"), checksum OK)"

  rc=0
  bash "$WORK/get.sh" "$@" || rc=$?
  return "$rc"
}

main "$@"
