#!/usr/bin/env bash
set -euo pipefail

: "${REGISTRY:?REGISTRY is required}"
: "${APP_BASE_URL:?APP_BASE_URL is required}"

if [[ ! -s .previous-image-tag ]]; then
  echo "No previous image tag is available for rollback" >&2
  exit 1
fi

IMAGE_TAG="$(cat .previous-image-tag)"
export IMAGE_TAG REGISTRY

docker compose pull edge api worker
docker compose up -d --remove-orphans --wait --wait-timeout 120
scripts/teamcity/smoke.sh
printf '%s\n' "$IMAGE_TAG" > .deployed-image-tag
