#!/bin/bash

set -e

cd "$(dirname "$0")"

show_services() {
  echo
  echo "Available services:"
  echo
  echo "bin-it"
  echo "cloudflared"
  echo "portainer"
  echo "all"
  echo
  echo "Usage examples:"
  echo "./start cloudflared"
  echo "./start cloudflared portainer"
  echo "./start all"
}

FILES=()

for arg in "$@"; do
  case "$arg" in
  bin-it)
    FILES+=("-f" "compose.bin-it.yaml")
    ;;
  cloudflared)
    FILES+=("-f" "compose.cloudflared.yaml")
    ;;
  portainer)
    FILES+=("-f" "compose.portainer.yaml")
    ;;
  all)
    FILES+=("-f" "compose.bin-it.yaml" "-f" "compose.cloudflared.yaml" "-f" "compose.portainer.yaml")
    ;;
  *)
    echo "$arg is an unknown service"
    show_services
    exit 1
    ;;
  esac
done

if [ ${#FILES[@]} -eq 0 ]; then
  echo "Error: no services specified"
  show_services
  exit 1
fi

if ! docker compose "${FILES[@]}" up -d; then
  echo "Error occurred while starting services"
  exit 1
fi
