_ID=`uuidgen | tr "[:upper:]" "[:lower:]"`
    echo "Generated sdc.id "${SDC_ID}
    kubectl create secret generic sdc-id --from-literal=sdc.id=${SDC_ID}

# Get an SDC auth token from Control Hub
SDC_AUTH_TOKEN=$(curl -s -X PUT -d "{\"organization\": \"${SCH_ORG}\", \"componentType\" : \"dc\", \"numberOfComponents\" : 1, \"active\" : true}" ${SCH_URL}/security/rest/v1/organization/${SCH_ORG}/components --header "Content-Type:application/json" --header "X-Requested-By:SDC" --header "X-SS-REST-CALL:true" --header "X-SS-User-Auth-Token:${SCH_AUTH_TOKEN}" | jq '.[0].fullAuthToken')

  if [ -z "$SDC_AUTH_TOKEN" ]; then
    log "Failed to generate SDC token. Please ensure SCH credentials are correct and have correct permissions"
    exit 1
  fi
  echo "Generated an Auth Token for SDC"

# Store the SDC auth token in a secret
kubectl create secret generic sdc-auth-token --from-literal=application-token.txt=${SDC_AUTH_TOKEN}

if [ $PROVISIONING_AGENT == "y" ]; then

      ## Use the auth token to get a token for a Control Agent
      AGENT_TOKEN=$(curl -s -X PUT -d "{\"organization\": \"${SCH_ORG}\", \"componentType\" : \"provisioning-agent\", \"numberOfComponents\" : 1, \"active\" : true}" ${SCH_URL}/security/rest/v1/organization/${SCH_ORG}/components --header "Content-Type:application/json" --header "X-Requested-By:SDC" --header "X-SS-REST-CALL:true" --header "X-SS-User-Auth-Token:${SCH_AUTH_TOKEN}" | jq '.[0].fullAuthToken')

      if [ -z "$AGENT_TOKEN" ]; then
        echo "Error: Failed to generate control agent token."
        echo "Please verify you have Provisioning Operator permissions in SCH"
        exit 1
      fi

      ## Store the Control Agent token in a secret
      kubectl create secret generic control-agent-token \
          --from-literal=dpm_agent_token_string=${AGENT_TOKEN}

      ## Create a secret for the Control Agent to use
      kubectl create secret generic control-agent-secret

      ## Generate a UUID for the Control Agent
      AGENT_ID=$(uuidgen)

      ## Store connection properties in a configmap for the Control Agent
      kubectl create configmap control-agent-config \
          --from-literal=org=${SCH_ORG} \
          --from-literal=sch_url=${SCH_URL} \
          --from-literal=agent_id=${AGENT_ID}

      ## Create a Service Account to run the Control Agent
      kubectl create -f yaml/control-agent-rbac.yaml

      ## Deploy the Control Agent
      sed -i -e 's/sanju/'"$USER"'/' yaml/control-agent.yaml
      kubectl create -f yaml/control-agent.yaml
      sed -i -e 's/'"$USER"'/sanju/' yaml/control-agent.yaml
fi

# install NGINX ingress controller
helm install $KUBE_NAMESPACE stable/nginx-ingress --set rbac.create=true --set controller.publishService.enabled=true --namespace=$KUBE_NAMESPACE

if [[ "$INSTALL_TYPE" == "f" ]]
  then
      #  Create the Volume to store SDC stagelibs.
      kubectl apply -f yaml/sdc-pvc.yaml
      #  Run a Job to download all the stagelibs and populate the K8's volume
      kubectl apply -f yaml/sdcDownloadJob.yaml

      # Allow time to create Container
      while [[ $(kubectl get pods | awk 'FNR == 5 {print $3}') == "ContainerCreating" ]]; do
        spinner
      done
      printf "Please wait for the stage libs to download................"
      # Wait until the job is completed
      # stage lib download could fail sometimes potentially due https://github.com/kubernetes/kubernetes/issues/76790
      while [ $(kubectl get jobs sdc-stage-libs-job | awk 'FNR == 2 {print $2}') == "0/1" ]; do
        spinner
      done
      printf "\n"
      # update DPM specs with SCH URL and DC labels
      sed -i -e 's/sch-url/'"$SCH_HOST"'/' yaml/dpm_ConfigMap.yaml
      sed -i -e 's/auth-sdc/'"$SDC_LABEL"'/' yaml/dpm_ConfigMap.yaml

      #### Deploy ConfigMap for dpm.properties
      kubectl apply -f yaml/dpm_ConfigMap.yaml

      # reset DPM specs with dummy SCH URL and labels
      sed -i -e 's/'"$SCH_HOST"'/sch-url/' yaml/dpm_ConfigMap.yaml
      sed -i -e 's/'"$SDC_LABEL"'/auth-sdc/' yaml/dpm_ConfigMap.yaml

      # update SDC specs with $SDC_VERSION and to have SDC hostanme start with the username
      sed -i -e 's/latest/'"$SDC_VERSION"'/' yaml/sdcFullDeployment.yaml
      sed -i -e 's/sanju-auth-sdc/'"$SDC_HOSTNAME"'/' yaml/sdcFullDeployment.yaml

      # Deploy the SDC pod
      kubectl apply -f yaml/sdcFullDeployment.yaml &
      cmdWait

      # Reset the specs with the defaults for next run
      sed -i -e 's/'"$SDC_VERSION"'/latest/' yaml/sdcFullDeployment.yaml
      sed -i -e 's/'"$USER"'-sdc-latest/sanju-auth-sdc/' yaml/sdcFullDeployment.yaml
  else
      # update DPM specs with SCH URL and DC labels
      sed -i -e 's/sch-url/'"$SCH_HOST"'/' yaml/dpm_ConfigMap.yaml
      sed -i -e 's/auth-sdc/'"$SDC_LABEL"'/' yaml/dpm_ConfigMap.yaml

      #### Deploy ConfigMap for dpm.properties
      kubectl apply -f yaml/dpm_ConfigMap.yaml

      # reset DPM specs with dummy SCH URL and labels
      sed -i -e 's/'"$SCH_HOST"'/sch-url/' yaml/dpm_ConfigMap.yaml
      sed -i -e 's/'"$SDC_LABEL"'/auth-sdc/' yaml/dpm_ConfigMap.yaml

      # update SDC specs with $SDC_VERSION and to have SDC hostanme start with the username
      sed -i -e 's/latest/'"$SDC_VERSION"'/' yaml/sdcDeployment.yaml
      sed -i -e 's/sanju-auth-sdc/'"$SDC_HOSTNAME"'/' yaml/sdcDeployment.yaml

      # Deploy the SDC pod
      kubectl apply -f yaml/sdcDeployment.yaml &
      cmdWait

      # Reset the specs with the defaults for next run
      sed -i -e 's/'"$SDC_VERSION"'/latest/' yaml/sdcDeployment.yaml
      sed -i -e 's/'"$USER"'-sdc-latest/sanju-auth-sdc/' yaml/sdcDeployment.yaml
fi

printf "Please wait for the pods to come online................"
while [[ $(kubectl get deployments.apps auth-sdc | awk 'FNR == 2 {print $2}') == "0/1" ]]; do
  spinner
done

# Wait for the LoadBalancer IP to be available
while [[ $(kubectl describe ingress sdc | awk 'FNR == 3 {print $2}' | awk '{print length}') -le 10 ]]; do
  spinner
done

SDC_IP=$(kubectl get ingress sdc | awk 'FNR == 2 {print $4}')
printf "\n"

log 'You will be prompted to enter