#!/bin/bash

# Backup original docker-compose file
cp docker-compose.yml docker-compose.yml.bak

# Use yq to remove old logging
yq e -i 'del(.[].[].logging)' docker-compose.yml

# Loop through each service in the docker-compose.yml
for service in $(yq eval '.services | keys | .[]' docker-compose.yml); do
  # Ensure the logging isn't already set
  echo "$service"
  sed "s/^[ ]*$service:/  $service:\n    logging:\n      driver: awslogs\n      options:\n          awslogs-region: eu-west-2\n          awslogs-group: madmc-harbor-$service\n          awslogs-create-group: 'true'\n          awslogs-stream: $INSTANCE_ID/" docker-compose.yml >/tmp/temp-docker-compose.yml
  cat /tmp/temp-docker-compose.yml >docker-compose.yml
done
