#!/bin/bash

set -e

AWS_ACCOUNT_NUMBER="$1"
BUCKET_ID="$2"
AUTO_DEPLOY_ROLE_ARN="arn:aws:iam::${AWS_ACCOUNT_NUMBER}:role/allow-auto-deploy-from-other-accounts"

response=$(aws sts assume-role --role-arn "${AUTO_DEPLOY_ROLE_ARN}" --role-session-name "$USER")
export AWS_ACCESS_KEY_ID=$(echo "$response" | jq '.Credentials.AccessKeyId' | cut -f2 -d\")
export AWS_SECRET_ACCESS_KEY=$(echo "$response" | jq '.Credentials.SecretAccessKey' | cut -f2 -d\")
export AWS_SESSION_TOKEN=$(echo "$response" | jq '.Credentials.SessionToken' | cut -f2 -d\")
aws s3 cp dist/ "s3://${BUCKET_ID}" --recursive
