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

if [[ "$1" == '--help' || "$1" == '-h' ]]
   then
   echo "send-messages usage: ./send-messages ENDPOINT_ID FORMAT_URN MESSAGE_TOKEN IOTSERVER PORT"
   exit 0
fi

echo "============================================================================================"
echo "Step 8: Sending messages to device $ENDPOINT_ID"
if [ -n "$ENDPOINT_ID" ] && [ -n "$FORMAT_URN" ] && [ -n "$MESSAGE_TOKEN" ] && [ -n "$IOTSERVER" ] && [ -n "$PORT" ]; then
   echo "Payload: "
   echo "[
      {
         \"clientId\":\"c839d086-bb23-4cd3-8405-130f5fc92b5b\",
         \"source\":\"$ENDPOINT_ID\",
         \"destination\":\"\",
         \"priority\":\"LOW\",
         \"reliability\":\"BEST_EFFORT\",
         \"eventTime\":1453427902124,
         \"sender\":\"\",
         \"type\":\"DATA\",
         \"properties\":{

         },
         \"payload\":{
            \"format\":\"$FORMAT_URN\",
            \"data\":{   
               \"field1\":100
            }
         }
      }
   ]" | tee ../json/message_payload.json
   echo "cURL command: "
   echo -e "curl -is -X POST -H \"Content-Type:application/json\" -H \"Accept:application/json\" -H \"Authorization: Bearer $MESSAGE_TOKEN\" -H \"X-EndpointId: $ENDPOINT_ID\" --data @../json/message_payload.json \"$IOTSERVER:$PORT/iot/api/v2/messages\""
   echo -e "\nResponse: "
   curl -is -X POST -H "Content-Type:application/json" -H "Accept:application/json" -H "Authorization: Bearer $MESSAGE_TOKEN" -H "X-EndpointId: $ENDPOINT_ID" --data @../json/message_payload.json "$IOTSERVER:$PORT/iot/api/v2/messages"
elif [ $# -eq 5 ]; then
   echo "Payload: "
   echo "[
      {
         \"clientId\":\"c839d086-bb23-4cd3-8405-130f5fc92b5b\",
         \"source\":\"$1\",
         \"destination\":\"\",
         \"priority\":\"LOW\",
         \"reliability\":\"BEST_EFFORT\",
         \"eventTime\":1453427902124,
         \"sender\":\"\",
         \"type\":\"DATA\",
         \"properties\":{

         },
         \"payload\":{
            \"format\":\"$2\",
            \"data\":{   
               \"field1\":100
            }
         }
      }
   ]" | tee ../json/message_payload.json
   echo "cURL command: "
   echo -e "curl -is -X POST -H \"Content-Type:application/json\" -H \"Accept:application/json\" -H \"Authorization: Bearer $3\" -H \"X-EndpointId: $1\" --data @../json/message_payload.json \"$4:$5/iot/api/v2/messages\""
   echo -e "\nResponse: "
   curl -is -X POST -H "Content-Type:application/json" -H "Accept:application/json" -H "Authorization: Bearer $3" -H "X-EndpointId: $1" --data @../json/message_payload.json "$4:$5/iot/api/v2/messages"
else
   echo "send-messages usage: ./send-messages ENDPOINT_ID FORMAT_URN MESSAGE_TOKEN IOTSERVER PORT"
   exit 1
fi
echo