#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -eo pipefail

# Absolute path to this script, e.g. ~/ktfmt/online_formatter/build_and_deploy.sh
SCRIPT=$(readlink -f "$0")
# Absolute path this script is in, thus ~/ktfmt/online_formatter/
SCRIPT_DIR=$(dirname "${SCRIPT}")

cd "$SCRIPT_DIR"

../gradlew -q packageLibs
mv build/distributions/lambda.zip build/lambda-lib.zip

../gradlew :lambda:build

ARTIFACT_BUCKET=ktfmt-online-formatter-lambda
TEMPLATE=format.yaml
aws cloudformation package --template-file $TEMPLATE --s3-bucket $ARTIFACT_BUCKET --output-template-file out.yml
aws cloudformation deploy --template-file out.yml --stack-name ktfmt --capabilities CAPABILITY_NAMED_IAM --region=us-east-2
