#!/bin/bash

#  Copyright (c) 2016, Oracle and/or its affiliates.  All rights reserved.
#  
#  This software is dual-licensed to you under the MIT License (MIT) and
#  the Universal Permissive License (UPL).  See the LICENSE file in the root
#  directory for license terms.  You may choose either license, or both.
#  

#Run getToken.jar to get access_token
if [[ "$1" == '--help' || "$1" == '-h' ]]
	then
	echo "get-activation-token usage: ./get-activation-token SHARED_SECRET ACTIVATION_ID IOTSERVER PORT"
	exit 0
fi

echo "============================================================================================"
echo "Step 3: Get activation token"
echo "Generating curl command for acquiring activation access token..."
# Execute jar/getToken.jar which generate activation_token_curl.sh
if [ -n "$SHARED_SECRET" ] && [ -n "$ACTIVATION_ID" ] && [ -n "$IOTSERVER" ] && [ -n "$PORT" ]
then
	../build/distributions/rest/bin/rest token activationToken $SHARED_SECRET $ACTIVATION_ID $IOTSERVER:$PORT/iot/api/v2/oauth2/token/
elif [ $# -eq 4 ]
then
	../build/distributions/rest/bin/rest token activationToken $1 $2 $3:$4/iot/api/v2/oauth2/token/
else
	echo "get-activation-token usage: ./get-activation-token SHARED_SECRET ACTIVATION_ID IOTSERVER PORT"
	exit 1
fi

if [ ! -f activation_token_curl.sh ]; then
    echo "File not found!"
    exit 1
fi
echo "Getting activation token..."
echo "Response: "
chmod +x activation_token_curl.sh
./activation_token_curl.sh
echo