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

# Create a new device model
# can add system attribtues, custom attributes, description, etc. as needed
if [[ "$1" == '--help' || "$1" == '-h' ]]
	then
	echo "create-device-model usage: ./create-device-model URN FORMAT_URN DEVICE_MODEL_NAME IOT_USERNAME IOT_PASSWORD IOTSERVER PORT"
	exit 0
fi

echo "============================================================================================"
echo "Step 1: Register your device model"
echo "Generating a new device model...You can find your device model either in json folder named with 'device_model.json' or iot dashboard->Devices->Model"
if [ -n "$URN" ] && [ -n "$FORMAT_URN" ] && [ -n "$DEVICE_MODEL_NAME" ] && [ -n "$IOT_USERNAME" ] && [ -n "$IOT_PASSWORD" ] && [ -n "$IOTSERVER" ] && [ -n "$PORT" ]
then
	echo -e '\nPayload: '
	echo '{
			"urn":"'"$URN"'",
			"name":"'"$DEVICE_MODEL_NAME"'",
			"formats" : [ {
			    "urn" : "'"$FORMAT_URN"'",
			    "name" : "Unique Format",
			    "description" : "Format Description",
			    "type" : "DATA",
			    "deviceModel" : "'"$URN"'",
			    "value" : {
			      "fields" : [ {
			        "name" : "field1",
			        "type" : "NUMBER",
			        "optional" : true
		      } ]
		    }
		  } ]
		}' | tee ../json/device_model_payload.json
	echo -e '\ncURL command: '
	echo -e "curl -s -X POST -H \"Accept:application/json\" -H \"Content-Type: application/json\" -u $IOT_USERNAME:$IOT_PASSWORD --data @../json/device_model_payload.json \"$IOTSERVER:$PORT/iot/api/v2/deviceModels/\" | tee ../json/device_model.json"
	echo -e '\nResponse: '
	curl -s -X POST -H "Accept:application/json" -H "Content-Type: application/json" -u $IOT_USERNAME:$IOT_PASSWORD --data @../json/device_model_payload.json "$IOTSERVER:$PORT/iot/api/v2/deviceModels/" | tee ../json/device_model.json
elif [ $# -eq 7 ]
then
echo  '{
			"urn":"'"$1"'",
			"name":"'"$3"'",
			"formats" : [ {
			    "urn" : "'"$2"'",
			    "name" : "Unique Format",
			    "description" : "Format Description",
			    "type" : "DATA",
			    "deviceModel" : "'"$1"'",
			    "value" : {
			      "fields" : [ {
			        "name" : "field1",
			        "type" : "NUMBER",
			        "optional" : true
		      } ]
		    }
		  } ]
		}' | tee ../json/device_model_payload.json
		echo -e '\ncURL command: '
		echo -e "curl -s -X POST -H \"Accept:application/json\" -H \"Content-Type: application/json\" -u $4:$5 --data @../json/device_model_payload.json \"$6:$7/iot/api/v2/deviceModels/\" | tee ../json/device_model.json
		"
		echo -e '\nResponse: '
		curl -s -X POST -H "Accept:application/json" -H "Content-Type: application/json" -u $4:$5 --data @../json/device_model_payload.json "$6:$7/iot/api/v2/deviceModels/" | tee ../json/device_model.json
else
	echo "create-device-model usage: ./create-device-model URN FORMAT_URN DEVICE_MODEL_NAME IOT_USERNAME IOT_PASSWORD IOTSERVER PORT"
	exit 1
fi
echo

