#!/usr/bin/env bash

# Exit the script as soon as something fails.
set -e

#VARS
APPLICATION_PATH="$HOME/repos/synt_backend"
REGISTRY_HOST="635199656487.dkr.ecr.eu-west-1.amazonaws.com"
REPO_NAME="synt-backend"
IMAGE="synt-backend"

function auth () {
  aws ecr --profile synt get-login-password --region eu-west-1 | docker login --username AWS --password-stdin 635199656487.dkr.ecr.eu-west-1.amazonaws.com
}

function build {
    cd "${APPLICATION_PATH}" 

    docker build -t "${IMAGE}:latest" . 

    cd -  
}

function run {
    cd "${APPLICATION_PATH}" 

    docker run -p 8000:8000 "${IMAGE}:latest"

    cd -  
}

function push {
    cd "${APPLICATION_PATH}" 

    docker tag "${IMAGE}:latest" "${REGISTRY_HOST}/${REPO_NAME}:latest"
    docker push "${REGISTRY_HOST}/${REPO_NAME}"

    cd - 
}

# Deal with command line flags.
while [[ $# > 0 ]]
do
case "${1}" in
  -a)
  auth
  shift
  ;;
  -b)
  build
  shift
  ;;
  -r)
  run
  shift
  ;;
  -p)
  push
  shift
  ;;
  *)
  echo "${1} is not a valid flag, try running: ${0} --help"
  ;;
esac
shift
done