#!/bin/bash

PROVIDER="MySql"
JSON_FILE="appettings.Production.json"
HES_PORT="5000"

SMTP_HOST="smp.host.com"
SMTP_PORT=587
SMTP_USER_NAME="smtpuser"
SMTP_PASSWORD="smtppassword"
ENABLESSL="true"

CERTTHUMBPRINT="" 
HIDEEZCLOUD="true"


while [ -n "$1" ]
do
        if [ -z "$2" ]  
        then
            echo "ERROR! no values specified after "$1" !" | tee -a $LOG_FILE
            echo "$USAGE"
            exit 1
        fi
        param="$2"

        case "$1" in
                --provider)
                     PROVIDER=$param
                        shift ;;
                --domain_name)
                     DOMAIN_NAME=$param
                        shift ;;
                --database_name)
                     DATABASE_NAME=$param
                        shift ;;
                --database_user)
                     DATABASE_USER=$param
                        shift ;;
                --json_file)
                     JSON_FILE=$param
                        shift ;;
                --hes_port)
                        HES_PORT=$param
                        shift ;;
                --smtp_host)
                        SMTP_HOST=$param
                        shift ;;
                --smtp_port)
                        SMTP_PORT=$param
                        shift ;;
                --smtp_user_name)
                        SMTP_USER_NAME=$param
                        shift ;;
                --smtp_password)
                        SMTP_PASSWORD=$param
                        shift ;;

                --smtp_password)
                        SMTP_PASSWORD=$param
                        shift ;;
                --enablessl)
                        ENABLESSL=$param
                        shift ;;
                --certthumbprint)
                        CERTTHUMBPRINT=$param
                        shift ;;
                --hideezcloud)
                        HIDEEZCLOUD=$param
                        shift ;;
                --) shift
                break ;;
                *) echo "ERROR! $1 is not an option"
                   echo "$USAGE"
                   exit 1
        esac
        shift
done


DefaultConnection="server=127.0.0.1;port=3306;database=$DATABASE_NAME;uid=$DATABASE_USER"
Provider="MySql"

ConnectionStrings=$(jq -n \
             --arg DefaultConnection "$DefaultConnection" \
             --arg Provider "$Provider" \
              '$ARGS.named'
)
#echo "ConnectionStrings = $ConnectionStrings"


EmailSender=$(jq -n \
             --arg Host "$SMTP_HOST" \
             --arg Port "$SMTP_PORT" \
             --arg EnableSSL "$ENABLESSL" \
             --arg UserName "$SMTP_USER_NAME" \
             --arg Password "$SMTP_PASSWORD" \
              '$ARGS.named'
)
#echo "$EmailSender"

SERVERURL="https://$DOMAIN_NAME"


ServerSettings=$(jq -n \
             --arg ServerUrl "$SERVERURL" \
             --arg CertThumbprint "$CERTTHUMBPRINT" \
             --arg HideezCloud "$HIDEEZCLOUD" \
              '$ARGS.named'
)

#echo "ServerSettings = $ServerSettings"

Url="http://localhost:$HES_PORT"
Http=$(jq -n \
             --arg Url "$Url" \
              '$ARGS.named'
)
#echo "$Http"

Endpoints=$(jq -n \
  --argjson Http "$Http" \
  '$ARGS.named'
)
#echo $Endpoints

Kestrel=$(jq -n \
  --argjson Endpoints "$Endpoints" \
  '$ARGS.named'
)
#echo $Kestrel


json=$(jq -n \
              --argjson ConnectionStrings "$ConnectionStrings" \
              --argjson EmailSender "$EmailSender"  \
              --argjson ServerSettings "$ServerSettings"  \
              --argjson Kestrel "$Kestrel"  \
              '$ARGS.named'
)

# echo "$json"
echo "$json" > $JSON_FILE
