#!/bin/bash

function output()
{
	echo "$*" > /dev/stdout
}

function error ()
{
	CODE="$1"
	shift 1
	echo "ERROR: $*" > /dev/stderr
	exit $CODE
}

if [ "$1" == "help" ]; then
	echo "Syntax: gridlabd aws [options] <command> <subcommand> [parameters]"
	exit 0
fi

# check awscli installation
if [ -z "$(which aws)" ]; then
	error 1 "awscli is not installed -- run 'pip3 install awscli'"
fi

# check awscli version
VERSION=($(aws --version | cut -f1 -d' ' | cut -f2 -d'/' | tr '.' ' '))
MAJOR=${VERSION[0]}
MINOR=${VERSION[1]}
PATCH=${VERSION[2]}
if [ $MAJOR -lt 1 -o $MINOR -lt 16 ]; then
	error 2 "awscli is outdated -- run 'pip3 install awscli --upgrade'"
fi

aws $*
