#!/bin/bash
#
#  Copyright (c) 2019, The OpenThread Authors.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#  3. Neither the name of the copyright holder nor the
#     names of its contributors may be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.
#

SRC_BASE="$(dirname "$0")/.."
CFG_FILE="${SRC_BASE}/include/google_cloud_iot/client_cfg.h"

[[ ! -e "${CFG_FILE}" ]] || rm "${CFG_FILE}"

echo \
'#ifndef OT_RTOS_CLIENT_CFG_HPP_
#define OT_RTOS_CLIENT_CFG_HPP_
#define CLOUDIOT_SERVER_ADDRESS "mqtt.googleapis.com"
#define CLOUDIOT_DEVICE_ID "your_device_id"
#define CLOUDIOT_REGISTRY_ID "your_registry_name"
#define CLOUDIOT_PROJECT_ID "your_project_id"
#define CLOUDIOT_REGION "your_region"
#define CLOUDIOT_CLIENT_ID                                                                                       \
    "projects/" CLOUDIOT_PROJECT_ID "/locations/" CLOUDIOT_REGION "/registries/" CLOUDIOT_REGISTRY_ID "/devices" \
    "/" CLOUDIOT_DEVICE_ID
#define CLOUDIOT_PRIV_KEY                                                \' > "${CFG_FILE}"

sed -i "s/your_device_id/$1/" "${CFG_FILE}"
sed -i "s/your_registry_name/$2/" "${CFG_FILE}"
sed -i "s/your_project_id/$3/" "${CFG_FILE}"
sed -i "s/your_region/$4/" "${CFG_FILE}"

if [[ -e "$5" ]]; then
    cat "$5" | sed 's/^/"/' | sed 's/$/\\n" \\/' | head --bytes -2 >> "${CFG_FILE}"
else
    echo '""' >> "${CFG_FILE}"
fi

echo \
'
#define CLOUDIOT_CERT                                                    \
    "-----BEGIN CERTIFICATE-----\n"                                      \
    "MIIEMjCCAxqgAwIBAgIBATANBgkqhkiG9w0BAQUFADB7MQswCQYDVQQGEwJHQjEb\n" \
    "MBkGA1UECAwSR3JlYXRlciBNYW5jaGVzdGVyMRAwDgYDVQQHDAdTYWxmb3JkMRow\n" \
    "GAYDVQQKDBFDb21vZG8gQ0EgTGltaXRlZDEhMB8GA1UEAwwYQUFBIENlcnRpZmlj\n" \
    "YXRlIFNlcnZpY2VzMB4XDTA0MDEwMTAwMDAwMFoXDTI4MTIzMTIzNTk1OVowezEL\n" \
    "MAkGA1UEBhMCR0IxGzAZBgNVBAgMEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UE\n" \
    "BwwHU2FsZm9yZDEaMBgGA1UECgwRQ29tb2RvIENBIExpbWl0ZWQxITAfBgNVBAMM\n" \
    "GEFBQSBDZXJ0aWZpY2F0ZSBTZXJ2aWNlczCCASIwDQYJKoZIhvcNAQEBBQADggEP\n" \
    "ADCCAQoCggEBAL5AnfRu4ep2hxxNRUSOvkb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Ybg==\n"                                 \
    "-----END CERTIFICATE-----"
#endif' >> "${CFG_FILE}"
