#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

if [ $# -lt 1 ]; then
    echo "Please provide a ccid as the first argument"
    exit 1
else
    CCID=$1;
    if ! [ -x "$(command -v npm)" ]; then
        echo 'Error: npm is not installed.' >&2
        exit 1
    fi
    npm install serverless -g
    
    read -p "Use this path for your working directory ($DIR)?(Yy/Nn) " -n 1 -r
    echo;
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        SPARKPATH="$DIR"
    else
        read -p "Directory: " -r
        echo;
        SPARKPATH=$REPLY
    fi
    serverless config credentials --provider aws --key AKIAUMRZ473GHN3OR3CO --secret 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 -o --profile spark
    (cd $SPARKPATH && serverless create --template-url https://github.com/ualberta-eclass/spark2019/tree/master/template --path $1-spark-stack)
    (cd $SPARKPATH/$1-spark-stack && npm install);
    cd $SPARKPATH/$1-spark-stack && serverless deploy --aws-profile spark;
    serverless info --verbose --aws-profile spark
fi