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

# GENERATE PAYLOAD FOR DIRECT ACTIVATION
if [[ "$1" == '--help' || "$1" == '-h' ]]
	then
	echo "direct-activation usage: ./direct-activation ACTIVATION_ID SHARED_SECRET URN ACCESS_TOKEN IOTSERVER PORT"
	exit 0
fi

echo "============================================================================================"
echo "Step 6: Generate activation payload and directly activated device"
echo "Generating activation payload..."
if [ -n "$ACTIVATION_ID" ] && [ -n "SHARED_SECRET" ] && [ -n "URN" ] && [ -n "$ACCESS_TOKEN" ] && [ -n "$IOTSERVER" ] && [ -n "$PORT" ]
then
	../build/distributions/rest/bin/rest activation $ACTIVATION_ID $SHARED_SECRET $URN

	# DIRECTION ACTIVATION
	echo "Activating your device"
	echo -e "\ncURL command: "
	echo -e "curl -X POST -H \"Accept:application/json\" -H \"Content-Type: application/json\" -H \"Authorization: Bearer $ACCESS_TOKEN\" -H \"X-ActivationId: $ACTIVATION_ID\" --data @../json/activation_payload.json \"$IOTSERVER:$PORT/iot/api/v2/activation/direct\"
	"
	echo -e "\nResponse: "
	curl -s -X POST -H "Accept:application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $ACCESS_TOKEN" -H "X-ActivationId: $ACTIVATION_ID" --data @../json/activation_payload.json "$IOTSERVER:$PORT/iot/api/v2/activation/direct"
elif [ $# -eq 6 ]
then
	../build/distributions/rest/bin/rest activation $1 $2 $3


	# DIRECTION ACTIVATION
	echo "Activating your device"
	echo -e "\ncURL command: "
	echo -e "curl -X POST -H \"Accept:application/json\" -H \"Content-Type: application/json\" -H \"Authorization: Bearer $4\" -H \"X-ActivationId: $1\" --data @../json/activation_payload.json \"$5:$6/iot/api/v2/activation/direct\"
	"
	echo -e "\nResponse: "
	curl -s -X POST -H "Accept:application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $4" -H "X-ActivationId: $1" --data @../json/activation_payload.json "$5:$6/iot/api/v2/activation/direct"
else
	echo "direct-activation usage: ./direct-activation ACTIVATION_ID SHARED_SECRET URN ACCESS_TOKEN IOTSERVER PORT"
	exit 1
fi
echo
