#!/usr/bin/env bash
# Upgrades Calisto to the latest release. Run in Azure Cloud Shell (Bash), in the subscription
# Calisto is installed in:
#
#   curl -sL https://calisto.run/upgrade.sh | bash
#
# Only the container image changes. The database, its data and the network settings of the
# existing install are kept, and the database schema is migrated on start-up.
set -euo pipefail
BASE="${CALISTO_BASE:-https://raw.githubusercontent.com/abhijitsghosh/calisto-install/main/deploy}"
RG="rg-calisto"
SUB=()
ARGS=("$@")
for ((i = 0; i < ${#ARGS[@]}; i++)); do
  [[ "${ARGS[i]}" == "--resource-group" ]] && RG="${ARGS[i+1]:-$RG}"
  [[ "${ARGS[i]}" == "--subscription" ]] && SUB=(--subscription "${ARGS[i+1]:-}")
done
command -v az >/dev/null 2>&1 || { echo "ERROR: Azure CLI not found. Run this in Azure Cloud Shell: https://shell.azure.com"; exit 1; }
[[ "$(az group exists -n "$RG" "${SUB[@]}" 2>/dev/null)" == "true" ]] || { echo "ERROR: no Calisto install found in resource group $RG. To install, run: curl -sL https://calisto.run/install.sh | bash"; exit 1; }
INSTALLER="$(mktemp)"
trap 'rm -f "$INSTALLER"' EXIT
curl -fsSL "$BASE/install.sh" -o "$INSTALLER" || { echo "ERROR: could not download the installer from $BASE"; exit 1; }
CALISTO_BASE="$BASE" bash "$INSTALLER" "$@"
