#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck source=common.sh
source "$SCRIPT_DIR/common.sh"

PURGE_DATA=0
while [[ $# -gt 0 ]]; do
  case "$1" in
    --purge-data)
      PURGE_DATA=1
      shift
      ;;
    -h|--help)
      echo "Usage: ./scripts/uninstall.sh [--purge-data]"
      exit 0
      ;;
    *)
      die "unknown option: $1"
      ;;
  esac
done

load_env
compose_cmd --profile core --profile sidecar --profile monitor down --remove-orphans || true
if [[ "$PURGE_DATA" == "1" ]]; then
  [[ "$HOST_DATA_DIR" == "/" || -z "$HOST_DATA_DIR" ]] && die "refuse to purge unsafe HOST_DATA_DIR=$HOST_DATA_DIR"
  rm -rf "$HOST_DATA_DIR"
  log "purged data dir $HOST_DATA_DIR"
else
  log "containers removed; data kept at $HOST_DATA_DIR"
fi
