#!/bin/bash
#
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
#

C_YELLOW='\033[1;33m'
C_RESET='\033[0m'
set -eux

PREVIOUS_RELEASE_VERSIONS_ARG="$(pwd)/aws-sdk-rust/versions.toml"
if [[ ! -f "${PREVIOUS_RELEASE_VERSIONS_ARG}" ]]; then
    echo "Failed to find versions.toml file at ${PREVIOUS_RELEASE_VERSIONS_ARG}"
    exit 1
fi

echo -e "${C_YELLOW}Taking models from 'awslabs/aws-sdk-rust'...${C_RESET}"
AWS_SDK_MODELS_PATH="$(pwd)/aws-sdk-rust/aws-models"
if [[ ! -d "${AWS_SDK_MODELS_PATH}" ]]; then
  echo "Failed to find AWS models in ${AWS_SDK_MODELS_PATH}"
  exit 1
fi

echo -e "${C_YELLOW}Taking examples from 'awsdocs/aws-doc-sdk-examples'...${C_RESET}"
examples_revision=$(cd aws-doc-sdk-examples; git rev-parse HEAD)
mv aws-doc-sdk-examples/rustv1 smithy-rs/aws/sdk/examples
rm -rf smithy-rs/aws/sdk/examples/.cargo

echo -e "${C_YELLOW}Creating empty model metadata file since we don't have model update information...${C_RESET}"
MODEL_METADATA_PATH="$(pwd)/model-metadata.toml"
echo > "${MODEL_METADATA_PATH}"

echo -e "${C_YELLOW}Generating services...${C_RESET}"
cd smithy-rs

./gradlew \
    -Paws.sdk.models.path="${AWS_SDK_MODELS_PATH}" \
    -Paws.sdk.examples.revision="${examples_revision}" \
    -Paws.sdk.previous.release.versions.manifest="${PREVIOUS_RELEASE_VERSIONS_ARG}" \
    -Paws.sdk.model.metadata="${MODEL_METADATA_PATH}" \
    aws:sdk:assemble
mv aws/sdk/build/aws-sdk ../artifacts/
