#!/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.
#  

# Get message token
if [[ "$1" == '--help' || "$1" == '-h' ]]
	then
	echo "get-message-token usage: ./get-message-token ENDPOINT_ID IOTSERVER PORT"
	exit 0
fi

echo "============================================================================================"
echo "Step 7: Get message token"
echo "Generating curl command for acquiring message access token..."
# Execute jar/getToken.jar which generate get_activation_token.sh
if [ -n "$ENDPOINT_ID" ] && [ -n "$IOTSERVER" ] && [ -n "$PORT" ]
then
	../build/distributions/rest/bin/rest token messageToken private_key.der $ENDPOINT_ID $IOTSERVER:$PORT/iot/api/v2/oauth2/token/
elif [ $# -eq 3 ]; then
	../build/distributions/rest/bin/rest token messageToken private_key.der $1 $2:$3/iot/api/v2/oauth2/token/
else
	echo "get-message-token usage: ./get-message-token ENDPOINT_ID IOTSERVER PORT"
	exit 1
fi

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