#!/bin/bash

abort() {
    echo "$@"
    exit 1
}

CONFFILE="$1"

if [ ! -z "$CONFFILE"] && [ ! -f "$CONFFILE" ] ; then
    echo "Config file $CONFFILE not found!"
fi

if [ -z "$CONFFILE" ] ; then
    CONFFILE="sneks.json"
fi

if [ -f "$CONFFILE" ] ; then
    eval `sneks get-sam-args -f $CONFFILE`
else
    eval `sneks get-sam-args`
fi

sneks setup-build-dir || abort "'setup_build_dir' failed"
echo "$BUILD_PARAMS" > build/extra_params.json
sneks bundle-lambda || abort "'bundle_lambda' failed"

aws --region $AWS_REGION --profile $AWS_PROFILE \
    cloudformation deploy \
    --template-file NewSamTemplate.json \
    --capabilities CAPABILITY_IAM \
    --stack-name $STACK_NAME || \
    abort "'aws cloudformation deploy' failed.  You can view the events with 'aws --region $AWS_REGION --profile $AWS_PROFILE cloudformation describe-stack-events --stack-name $STACK_NAME | less'"
