#!/bin/bash

set -e

docker-compose build serverless-node

template=$1
template_folder="tmp/serverless-integration-test-$template"

rm -fr $template_folder
mkdir -p $template_folder

function serverless {
    docker-compose run -w /app/$template_folder serverless-node /app/bin/serverless $@
}
echo "Creating Application"
serverless create --template $template

echo "Overwriting Service Name"
sed -i.bak s/${template}/sls-test-$template-$RANDOM/g $template_folder/serverless.yml

echo "Running Compose build for Template"
docker-compose build $template

if [ ! -z "$2" ]
then
  echo "Running Build command for $template"
  command=${@:2}
  docker-compose run -w /app $template /bin/bash -c "$command"
else
  echo "No Language build commands specified, skipping building"
fi

echo "Deploying Service"
serverless deploy -v

echo "Invoking Service"
serverless invoke --function hello

if [ $template == "aws-go" ] || [ $template == "aws-go-dep" ] || [ $template == "aws-go-mod" ]
then
  serverless invoke --function world
fi

echo "Removing Service"
serverless remove -v
