#!/bin/bash

if [ $# -eq 0 ]; then
  echo "No lambda specified"
  exit 1
fi

if [ -z "$2" ]; then
  echo "Defaulting to function mode"
  $2 = 'function'
fi

if [ $# -eq 2  ] && [ ! -d "lambda/$1" ]; then
  echo "Folder lambda/$1 does not exist!"
  exit 1
elif [ $# -gt 2  ] && [ ! -d "$2" ]; then
  echo "Folder $2 does not exist!"
  exit 1
fi

if [ $# -eq 2  ] && [ -d "./common" ]; then
  echo "Copying common folder to lambda/$1/..."
  cp -R common ./lambda/$1/common
elif  [ $# -gt 2  ] && [ -d "./common" ]; then
  echo "Copying common folder to /$2/..."
  cp -R common ./$2/common
fi

if [ $# -eq 2  ]; then
  echo "Running Docker Simple"
  docker-compose --project-directory lambda/$1 up --build $2
else
  echo "Runnning Docker Complex"
  docker-compose $@
fi

if [ $# -eq 2  ] && [ -d "./common" ]; then
  echo "Removing common folder from lambda/$1/..."
  rm -r ./lambda/$1/common
elif  [ $# -gt 2  ] && [ -d "./common" ]; then
  echo "Removing common folder from /$2/..."
  rm -r ./$2/common
fi

