#!/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.
#  
 
# Clean old files
./prepare
read -rsp $'Press any key to continue...\n' -n1 key

clear
echo "============================================================================================"
printf "\nOkay, let's get started! First off, we'll need to know how to access your IoT server.\n\n"

printf "What would be the URL to your server? (please include https:// for external use or http:// for internal use) "
read IOTSERVER
until [ -n "$IOTSERVER" ]
do
	printf "Server URL can't be empty.\n"
	printf "What would be the URL to your server? (please include https:// for external use or http:// for internal use) "
	read IOTSERVER
done

printf "And the port number? "
read PORT
until [ -n "$PORT" ]
do
	printf "Port number can't be empty."
	printf "And the port number? \n"
	read PORT
done

printf "\nGreat! Next off, we'll need to know some basic user information.\n\n"

printf "What would be the username to access IoT server? "
read IOT_USERNAME
until [ -n "$IOT_USERNAME" ]
do
	printf "Username can't be empty.\n"
	printf "What would be the username to access IoT server? "
	read IOT_USERNAME
done

printf "Thanks! And the password? (Notice: you will not see the password when you type it) "
read -s IOT_PASSWORD
until [ -n "$IOT_PASSWORD" ]
do
	printf "Password can't be empty.\n"
	printf "Thanks! And the password? (Notice: you will not see the password when you type it) "
	read -s IOT_PASSWORD
done

printf "\n\nNow that we know how to talk to your server, let’s get started with device registration. \n"

printf "We would need a unique, non-empty id for your device. What would you like that to be? "
read ACTIVATION_ID
until [ -n "$ACTIVATION_ID" ]
do
	printf "This activation id can't be empty.\n"
	printf "We would need a unique, non-empty id for your device. What would you like that to be? "
	read ACTIVATION_ID
done

printf "Awesome! Now, very quietly, please also tell us a shared secret that we can use: "
read SHARED_SECRET
until [ -n "$SHARED_SECRET" ]
do
	printf "Shared secret variable can't be empty.\n"
	printf "Awesome! Now, very quietly, please also tell us a shared secret that we can use: "
	read SHARED_SECRET
done

printf "What would you like to call your device? "
read DEVICE_NAME
until [ -n "$DEVICE_NAME" ]
do
	printf "Device name can't be empty.\n"
	printf "What would you like to call your device? "
	read DEVICE_NAME
done

printf "\nThe device that we're about to register would need to work with a device model.\nWhat would you like to name this model? "
read DEVICE_MODEL_NAME
until [ -n "$DEVICE_MODEL_NAME" ]
do
	printf "The device model name can't be empty.\n"
	printf "The device that we're about to register would need to work with a device model.\nWhat would you like to name this model? "
	read DEVICE_MODEL_NAME
done

printf "Okay! What unique, device model URN do you have in mind (Hint: Consider using something like urn:my:unique:dm)? "
read URN
until [ -n "$URN" ]
do
	printf "URN can't be empty and must be unique.\n"
	printf "Okay! What unique, device model URN do you have in mind (Hint: Consider using something like urn:my:unique:dm)? "
	read URN
done

printf "When we send messages from the device, it needs a message format.\nFor this particular device, what do you want the unique, message format URN to be? (Hint: urn:my:unique:format) "
read FORMAT_URN
until [ -n "$FORMAT_URN" ]
do
	printf "Message format can't be empty and must be unique.\n"
	printf "When we send messages from the device, it needs a message format.\nFor this particular device, what do you want the unique, message format URN to be? (Hint: urn:my:unique:format) "
	read FORMAT_URN
done

export IOTSERVER
export PORT
export IOT_USERNAME
export IOT_PASSWORD
# Shared secret you want to use to register & activate your device
export SHARED_SECRET
# Activation ID you want to use to register & activate your device
export ACTIVATION_ID
export DEVICE_NAME
# URN, FORMAT_URN and device model name for new device model, make sure they are unique
export URN
export FORMAT_URN
export DEVICE_MODEL_NAME
export SHARED_SECRET_BASE64=$(echo -n $SHARED_SECRET | base64)

# If the server has self signed certificate, curl commands get certificate check failure.
# So to overcome this, the CURL_CA_BUNDLE should be set to PEM certificate of the server.
# Check if the given IOTSERVER has https:// in it. For http, there are no certificate issues.
if [[ $IOTSERVER =~ https://.* ]]; then
  # Remove the https:// prefix to get server name
  IOTSERVERNAME="${IOTSERVER/https:\/\//}"
  #Check if the IoT Server has self signed certificate
  SELF_SIGNED_CERT_STR=$(echo "Q" | openssl s_client -showcerts -connect $IOTSERVERNAME:$PORT 2> /dev/null |grep -i "self signed certificate")
  if [ "$SELF_SIGNED_CERT_STR" != "" ]; then
    #This IOTSERVER has self signed certificate,so get the certificate in PEM format export it in CURL_CA_BUNDLE env variable.
    echo "Q" | openssl s_client -showcerts -connect $IOTSERVERNAME:$PORT </dev/null 2>/dev/null|openssl x509 -outform PEM > iotserver.pem
    export CURL_CA_BUNDLE=iotserver.pem  
  fi
fi

printf "\nNow we are good to go, do you want to run through the whole process (enter 1) or want to do each step independently (enter 2)? "
read option
until [[ "$option" == 1 || "$option" == 2 ]]
do
    printf "\nDo you want to run through the whole process (enter 1) or want to do each step independently (enter 2)? "
    read option
done
if [[ "$option" == 1 ]]; then
	printf "\nStart the whole quick start process...\n\n"
    ./quick-start
    # clean up env vars
    export IOTSERVER=
    export PORT=
    export IOT_USERNAME=
    export IOT_PASSWORD=
    export SHARED_SECRET=
    export ACTIVATION_ID=
    export DEVICE_NAME=
    export URN=
    export FORMAT_URN=
    export DEVICE_MODEL_NAME=
    export SHARED_SECRET_BASE64=
elif [[ "$option" == 2 ]]; then
    printf "Now you can run script independently.\n"
fi
