#!/bin/bash

###
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License Version 2.0 (the 'License').
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
#         http://www.apache.org/licenses/
#
# or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
##

DIST_DIR="./.dist"
# Location of the voicemail serverless module
AWS_CONNECT_VM_SERVERLESS="aws-connect-vm-serverless"

# Location of the voicemail portal module (reactjs app)
AWS_CONNECT_VM_PORTAL="aws-connect-vm-portal"

# Location of the distribution directory
AWS_CONNECT_VM_SERVERLESS_DIST="$DIST_DIR/$AWS_CONNECT_VM_SERVERLESS"
AWS_CONNECT_VM_PORTAL_DIST="$DIST_DIR/$AWS_CONNECT_VM_PORTAL"

COMMAND=()
while [[ $# -gt 0 ]]; do
  key="$1"

  case $key in
  -s | --stage)
    STAGE="$2"
    shift # past argument
    shift # past value
    ;;
  -r | --region)
    REGION="$2"
    shift # past argument
    shift # past value
    ;;
  -b | --bucket)
    HOSTING_BUCKET="$2"
    shift # past argument
    shift # past value
    ;;
  *) # unknown option
    COMMAND+=("$1") # save it in an array for later
    shift           # past argument
    ;;
  esac
done
set -- "${COMMAND[@]}" # restore positional parameters

echo "STAGE  = ${STAGE}"
echo "REGION = ${REGION}"
echo "HOSTING_BUCKET = ${HOSTING_BUCKET}"

function build() {
  echo "Building in progress stage: $1 region: $2"

  if [[ "$1" =~ [^a-zA-Z0-9\ ] ]]; then
    echo "Invalid stage."
    exit 1
  fi

  if ! [[ "$2" =~ ^(us(-gov)?|ap|ca|cn|eu|sa)-(central|(north|south)?(east|west)?)-[0-9]$ ]]; then
    echo "Invalid region."
    exit 1
  fi

  mkdir -p $DIST_DIR
  mkdir -p $DIST_DIR/$AWS_CONNECT_VM_SERVERLESS
  mkdir -p $DIST_DIR/$AWS_CONNECT_VM_PORTAL
  mkdir -p $DIST_DIR/$AWS_CONNECT_VM_SERVERLESS/templates

  # Execute the build process for the serverless project
  # Uses webpack to transpile the javascript and maven to package java
  cd ./$AWS_CONNECT_VM_SERVERLESS || exit
  eval "npx webpack && mvn clean package && npx serverless package --region $2 --stage $1"
  cd ..

  # Copy the generated lambda zip and jar files to the .dist directory
  cp "./$AWS_CONNECT_VM_SERVERLESS/target/aws-connect-vm-java.jar" "$AWS_CONNECT_VM_SERVERLESS_DIST/aws-connect-vm-java.jar"
  cp "./$AWS_CONNECT_VM_SERVERLESS/.serverless/aws-connect-vm.zip" "$AWS_CONNECT_VM_SERVERLESS_DIST/aws-connect-vm.zip"

  # Modifies the cloudformation template generated by the Serverless Framework
  cp "./$AWS_CONNECT_VM_SERVERLESS/.serverless/cloudformation-template-update-stack.json" "../deployment/aws-connect-vm.yaml"
  eval "python ./tools/transform.py --template ../deployment/aws-connect-vm.yaml --save ../deployment/aws-connect-vm.yaml --zip $AWS_CONNECT_VM_SERVERLESS/aws-connect-vm.zip --jar $AWS_CONNECT_VM_SERVERLESS/aws-connect-vm-java.jar"

  # Build the frontend portal
  cd "./$AWS_CONNECT_VM_PORTAL" || exit
  eval "npm run build"
  cd ..

  # Copy all the files from the frontend build into the .dist directory
  cp -rf "./$AWS_CONNECT_VM_PORTAL/build" "$AWS_CONNECT_VM_PORTAL_DIST"
}

echo "Running Command: ${COMMAND[0]}"

case $COMMAND in
build) build $STAGE $REGION ;;
*) ;;
esac
