#!/usr/bin/env bash
set -e

# Setup:
# 1. Install AWS CLI (http://docs.aws.amazon.com/cli/latest/userguide/tutorial-ec2-ubuntu.html#install-cli)
# 2. Install jq using `brew install jq`
# 3. Ensure you have active AWS credentials in environment vars for `twitch-extensions-configuration-dev`
#    or `twitch-extensions-configuration-prod`.

ENV="$1"
INPUT_IMAGE="$2"

# Premable
if [[ ! $1 =~ ^(dev|prod)$ ]] || [ -z "$2" ]; then
  echo "Usage: $0 [dev|prod] [image]"
  exit 1
fi

cd "$(dirname $0)"

if [[ "$ENV" == "dev" ]]; then
  DEPLOY_IMAGE=925711164288.dkr.ecr.us-west-2.amazonaws.com/devco-repos-1dh9idzjzlq3r:latest
  CLUSTER=DevConfigService-ClusterEB0386A7-1UN53QWNROSO
  SERVICE=DevConfigService-ServiceD69D759B-XHK2A9HT8U8L
elif [[ "$ENV" == "prod" ]]; then
  echo "You shall not prod (yet)."
  exit 1;
fi

`aws ecr get-login --no-include-email`
docker tag "$INPUT_IMAGE" "$DEPLOY_IMAGE"
docker push "$DEPLOY_IMAGE"
vendor/ecs-deploy --cluster "$CLUSTER" --service-name "$SERVICE" --image "$DEPLOY_IMAGE" --timeout 600 --max-definitions 10
