#!/bin/bash
#
# Federated Stop Script
. /federated/lib/functions.sh

usage() {
  printf -v SERVICES_JOINED '%s|' "${SERVICES[@]}"
  echo "$0: ${SERVICES_JOINED%|}"
  exit 2
}
stopservice() {
  echo "* Stopping $SERVICE.."
  cd /federated/apps/$SERVICE && docker-compose -f docker-compose.yml -p $SERVICE down
}
stopservice_all() {
  for i in "${SERVICES[@]}"; do
    echo "* Stopping $i.."
    cd /federated/apps/$i && docker-compose -f docker-compose.yml -p $i down
  done
}

get_installedapps
[ $# != 1 ] && usage
SERVICE=$1

[ "$SERVICE" = "all" ] && stopservice_all
if printf '%s\0' "${SERVICES[@]}" | grep -Fxqz -- "$SERVICE"; then
  stopservice
else
  usage
fi
