#!/bin/sh

#
# Quick script to iterate through each sdk and report back which succeeds/fails building
#

# Note: Instead of running this script it may be quicker to:
#   * Run `releaseSDK` for each of the desired SDKs
#   * Run `swift build` at the top level of aws-sdk-swift
#   The top level aws-sdk-swift Package.swift file will create a dependency graph with all
#   of the sdks and avoid re-compiling dependencies across all SDKs

usage() {
    echo "Usage:"
    echo "  buildSDK [pathToBuildDir]"
    echo "Example:"
    echo "  ./buildSDK ../build"    
}


if [ $# -eq 1 ]; then
    BDIR=$1
else
    usage
    exit 1
fi

BDIR=$1

if [ ! -d ${BDIR} ]; then
    echo "Unable to find build dir: ${BDIR}"
    exit 1
fi

BUILD_DIR=${BDIR}/smithyprojections/sdk-codegen

if [ ! -d ${BUILD_DIR} ]; then
    echo "Unable to find artifacts in ${BUILD_DIR}"
    exit 1
fi

ARTIFACTS=`ls ${BUILD_DIR}`
LOGOUTPUT=`mktemp`

logOutputToTemp() {
    echo $1 >> ${LOGOUTPUT}
}
echo "Summary Output:"
echo "${LOGOUTPUT}"
for artifact in ${ARTIFACTS}; do
    pushd ${BUILD_DIR}/${artifact}/swift-codegen
    swift build
    if [ $? -eq 0 ]; then
	logOutputToTemp "Succeeded compilation: ${artifact}"
    else
	logOutputToTemp "Failed compilation: ${artifact}"
    fi
    popd
done
echo ""
echo ""
echo "Summary:"
cat ${LOGOUTPUT}
echo "Summary Output:"
echo "${LOGOUTPUT}"
