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

# Stop Umbrel

if [[ $UID != 0 ]]; then
    echo "Umbrel must be stopped as root"
    echo "Please re-run this script as"
    echo "  sudo ./scripts/stop"
    exit 1
fi

UMBREL_ROOT="$(dirname $(readlink -f "${BASH_SOURCE[0]}"))/.."

if [[ ! -d "$UMBREL_ROOT" ]]; then
  echo "Root dir does not exist '$UMBREL_ROOT'"
  exit 1
fi

cd "$UMBREL_ROOT"

# Increase default Docker and Compose timeouts to 240s
# As bitcoin can take a long while to respond
export DOCKER_CLIENT_TIMEOUT=240
export COMPOSE_HTTP_TIMEOUT=240

echo "Stopping installed apps..."
echo
"${UMBREL_ROOT}/scripts/app" stop installed
echo

echo "Stopping Docker services..."
echo
docker-compose down