#!/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 `get_sam_args -f $CONFFILE`
else
    eval `get_sam_args`
fi

setup_build_dir || abort "'setup_build_dir' failed"

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'"
