help(){
  echo "MISSING ENVIRONMENT VARIABLES"
  echo "export API_URL=https://dev1.edge-preprod.dev/api/v2"
  echo "export API_BANNER=mailmen"
  echo "export API_ORGANIZATION=edge-dev1-retail-b526aa"
  echo "export API_USERNAME=<ncr id>"
  echo "export API_PASSWORD=<edge password>"
  exit
}


if [ -z "${API_URL}" ]; then
  API_URL=https://dev1.edge-preprod.dev/api/v2
fi

if [ -z "${API_BANNER}" ]; then
  API_BANNER=mailmen
fi

if [ -z "${API_ORGANIZATION}" ]; then
  API_ORGANIZATION=edge-dev1-retail-b526aa
fi

if [ -z "${API_USERNAME}" ]; then
  echo "API_USERNAME ENV NOT PROVIDED"
  help
fi

if [ -z "${API_PASSWORD}" ]; then
  echo "API_PASSWORD ENV NOT PROVIDED"
  help
fi


STORE="$API_USERNAME-$(date +"%Y-%m-%d-%H-%M-%S")"

echo "API_URL: $API_URL"
echo "API_BANNER: $API_BANNER"
echo "API_ORGANIZATION: $API_ORGANIZATION"
echo "API_USERNAME: $API_USERNAME"
echo "STORE_NAME: $STORE"

k3d="$1"
kubectl="$2"
tag="$3"

edge connect \
 --banner="$API_BANNER" \
 --organization="$API_ORGANIZATION" \
 --store="$STORE" \
 --create-site \
 --kubeconfig="$HOME/.kube/config" \
 --kubeconfig-context=k3d-ien-0 \
 --cluster-type=dsds \
 --api-endpoint="$API_URL" \
 --username="$API_USERNAME" \
 --password="$API_PASSWORD"

 if [ $? -ne 0 ]; then
   echo "could not connect cluster: $CLUSTER_ID"
   exit
 fi

# register network services
 edge networkservice register \
     --username "$API_USERNAME" \
     --password "$API_PASSWORD" \
     --organization "$API_ORGANIZATION" \
     --store "$STORE" \
     --banner "$API_BANNER" \
     --api-endpoint "$API_URL" \
     --service-type kube-vip \
     --ipv4 "10.10.11.2"
 edge networkservice register \
     --username "$API_USERNAME" \
     --password "$API_PASSWORD" \
     --organization "$API_ORGANIZATION" \
     --store "$STORE" \
     --banner "$API_BANNER" \
     --api-endpoint "$API_URL" \
     --service-type dns \
     --ipv4 "8.8.8.8"
 edge networkservice register \
     --username "$API_USERNAME" \
     --password "$API_PASSWORD" \
     --organization "$API_ORGANIZATION" \
     --store "$STORE" \
     --banner "$API_BANNER" \
     --api-endpoint "$API_URL" \
     --service-type ntp \
     --ipv6 "0.ubuntu.pool.ntp.org"

# node registration
CP_MAC_ADDRESS=$(docker network inspect k3d-ien-0 | jq '.[] | .Containers | to_entries[] | select(.value.Name == "k3d-ien-0-server-0")' | jq -r '.value.MacAddress')
TP1_MAC_ADDRESS=$(docker network inspect k3d-ien-0 | jq '.[] | .Containers | to_entries[] | select(.value.Name == "k3d-ien-1-0")' | jq -r '.value.MacAddress')
TP2_MAC_ADDRESS=$(docker network inspect k3d-ien-0 | jq '.[] | .Containers | to_entries[] | select(.value.Name == "k3d-ien-2-0")' | jq -r '.value.MacAddress')

edge terminal register \
    --username "$API_USERNAME" \
    --password "$API_PASSWORD" \
    --organization "$API_ORGANIZATION" \
    --store "$STORE" \
    --banner "$API_BANNER" \
    --api-endpoint "$API_URL" \
    --hostname "k3d-ien-0-server-0" \
    --role "controlplane" \
    --class "server" \
    --lane "0" \
    --mac "$CP_MAC_ADDRESS" \
    --discover-disks "all" \
    --prefix4 "1" \
    --prefix6 "1" \
    --dhcp4 "true"
edge terminal register \
    --username "$API_USERNAME" \
    --password "$API_PASSWORD" \
    --organization "$API_ORGANIZATION" \
    --store "$STORE" \
    --banner "$API_BANNER" \
    --api-endpoint "$API_URL" \
    --hostname "k3d-ien-1-0" \
    --role "worker" \
    --class "touchpoint" \
    --lane "1" \
    --mac "$TP1_MAC_ADDRESS" \
    --discover-disks "all" \
    --prefix4 "1" \
    --prefix6 "1" \
    --dhcp4 "true"
edge terminal register \
    --username "$API_USERNAME" \
    --password "$API_PASSWORD" \
    --organization "$API_ORGANIZATION" \
    --store "$STORE" \
    --banner "$API_BANNER" \
    --api-endpoint "$API_URL" \
    --hostname "k3d-ien-2-0" \
    --role "worker" \
    --class "touchpoint" \
    --lane "2" \
    --mac "$TP2_MAC_ADDRESS" \
    --discover-disks "all" \
    --prefix4 "1" \
    --prefix6 "1" \
    --dhcp4 "true"

$kubectl -n warehouse-system scale --replicas=0 deployment/lumperctl

if [ $? -ne 0 ]; then
  echo "LUMPER NOT FOUND"
  exit
fi

$kubectl create ns fluent-operator
$kubectl create ns pxe
$kubectl create ns vpn
$kubectl create ns sds
$kubectl create ns spegel
$kubectl create ns edge-iam
$kubectl create ns k8s-gateway
$kubectl apply -f ../../config/pallets/sds/nodeagent/base/crds
$kubectl apply -k ../../config/components/edge-iam-v2/crds
$kubectl apply -f ../../config/components/edge-iam-v2/targets/env/kind/crds # Prometheus
$kubectl apply -f ../../config/pallets/linkerdctl/linkerd-crds/linkerd-crds.yaml # LinkerD
$kubectl apply -f ../../third_party/k8s/emissary/crds/emissary-crds.yaml # Emissary

echo "Checking for shipments-sync kustomization"
while ! $kubectl wait --for=jsonpath='{.status.conditions[0].reason}'=ReconciliationSucceeded Kustomization shipments-sync -n edge-flux &> /dev/null
do
  echo "waiting for Kustomization shipments-sync"
  sleep 2
done

echo "Checking for chariot-sync kustomization"
while ! $kubectl wait --for=jsonpath='{.status.conditions[0].reason}'=ReconciliationSucceeded Kustomization chariot-sync -n edge-flux &> /dev/null
do
  echo "waiting for Kustomization chariot-sync"
  sleep 2
done

echo "Done with kustomizations"

CLUSTER_ID=$($kubectl get Shipment --no-headers -o custom-columns=":metadata.name")
if [ -z "${CLUSTER_ID}" ]; then
  echo "SHIPMENT NOT FOUND"
  exit
fi

echo "SHIPMENT: $CLUSTER_ID"

kubectl patch Shipment "$CLUSTER_ID" --type=merge -p '{"metadata":{"annotations":{"kustomize.toolkit.fluxcd.io/reconcile":"disabled"}}}'

$kubectl patch Shipment --type=merge -p '{"spec": {"pallets": [{"name":"datasync", "tag": '"\"$tag\""'}]}}' "$CLUSTER_ID"
$kubectl patch Shipment --type=merge -p '{"spec": {"unpack": {"capabilities": [] }}}' "$CLUSTER_ID"

$kubectl create ns data-sync-couchdb

# prevent invalid characters in password for couchdb
CLEAN_PASSWORD=$(echo "$API_PASSWORD" | tr -dc '[:alnum:]')
$kubectl create secret generic couchdb-local-creds -n data-sync-couchdb --from-literal=username="$API_USERNAME" --from-literal=password="$CLEAN_PASSWORD" --from-literal=cookieAuthSecret=cookiemonster

$kubectl -n warehouse-system scale --replicas=1 deployment/lumperctl