#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

SCRIPTS_DIR="$(dirname "$0")"
CREATED_AT="$(date -u +'%Y-%m-%d %H:%M:%S')"
# Provides $OS,$ARCH,$PLAFORM,$ROOT variables
source "$SCRIPTS_DIR/os-env.sh"

YQ="$ROOT/bin/$PLATFORM/yq"

# Apply patch-bundle-csv-scc.yaml on CSV file
# (cannot be done using Kustomize as the final CSV is generated by operator-sdk binary)
$YQ -i ".spec.install.spec.clusterPermissions += load(\"$ROOT/hack/patch-bundle-csv-scc.yaml\")" $ROOT/bundle/manifests/datadog-operator.clusterserviceversion.yaml

# Add annotations required for upstream publication
IMAGE=$($YQ '.spec.install.spec.deployments[0].spec.template.spec.containers[0].image' bundle/manifests/datadog-operator.clusterserviceversion.yaml)
$YQ -i ".metadata.annotations.containerImage = \"$IMAGE\"" bundle/manifests/datadog-operator.clusterserviceversion.yaml
$YQ -i ".metadata.annotations.createdAt = \"$CREATED_AT\"" bundle/manifests/datadog-operator.clusterserviceversion.yaml
$YQ -i ".metadata.annotations.support = \"Datadog, Inc.\"" bundle/manifests/datadog-operator.clusterserviceversion.yaml

# Add feature annotations (required)
$YQ -i '.metadata.annotations."features.operators.openshift.io/disconnected" = "true"' bundle/manifests/datadog-operator.clusterserviceversion.yaml
$YQ -i '.metadata.annotations."features.operators.openshift.io/fips-compliant" = "false"' bundle/manifests/datadog-operator.clusterserviceversion.yaml
$YQ -i '.metadata.annotations."features.operators.openshift.io/proxy-aware" = "false"' bundle/manifests/datadog-operator.clusterserviceversion.yaml
$YQ -i '.metadata.annotations."features.operators.openshift.io/tls-profiles" = "false"' bundle/manifests/datadog-operator.clusterserviceversion.yaml
$YQ -i '.metadata.annotations."features.operators.openshift.io/token-auth-aws" = "false"' bundle/manifests/datadog-operator.clusterserviceversion.yaml
$YQ -i '.metadata.annotations."features.operators.openshift.io/token-auth-azure" = "false"' bundle/manifests/datadog-operator.clusterserviceversion.yaml
$YQ -i '.metadata.annotations."features.operators.openshift.io/token-auth-gcp" = "false"' bundle/manifests/datadog-operator.clusterserviceversion.yaml

# Add skipRange annotation to allow direct upgrades. '>=1.7.0' is hard-coded because this is the version where the webhook conversion was removed from the bundle
VERSION=$($YQ '.spec.version' bundle/manifests/datadog-operator.clusterserviceversion.yaml )
$YQ -i ".metadata.annotations.\"olm.skipRange\" = \">=1.7.0 <$VERSION\"" bundle/manifests/datadog-operator.clusterserviceversion.yaml

# Set spec.replaces to latest released version
$YQ -i ".spec.\"replaces\" = \"datadog-operator.v$LATEST_VERSION\"" bundle/manifests/datadog-operator.clusterserviceversion.yaml

# Add OpenShift version annotation (adding in main bundle as it's used for OpenShift Community)
$YQ -i ".annotations.\"com.redhat.openshift.versions\" = \"v4.6\"" bundle/metadata/annotations.yaml

# set `DD_TOOL_VERSION` to `redhat`
$YQ -i "(.spec.install.spec.deployments[] | select(.name == \"datadog-operator-manager\") | .spec.template.spec.containers[] | select(.name == \"manager\") | .env[] | select(.name == \"DD_TOOL_VERSION\") | .value) = \"redhat\"" bundle/manifests/datadog-operator.clusterserviceversion.yaml

# Patch deploymentName, as `operator-sdk generate bundle` sets it to something that doesn't exist
eval $SED 's/datadog-operator-webhook/datadog-operator-manager/g' bundle/manifests/datadog-operator.clusterserviceversion.yaml

# The Operators in k8s-operatorhub/community-operators have slightly different requirements
COMM_OPS_PATH="bundle-community-operators/"
rm -rf "$COMM_OPS_PATH"
cp -R "bundle/" "$COMM_OPS_PATH"

# Delete spec.replaces
$YQ -i "del(.spec.\"replaces\")" "$COMM_OPS_PATH/manifests/datadog-operator.clusterserviceversion.yaml"

# set `DD_TOOL_VERSION` to `operatorhub`
$YQ -i "(.spec.install.spec.deployments[] | select(.name == \"datadog-operator-manager\") | .spec.template.spec.containers[] | select(.name == \"manager\") | .env[] | select(.name == \"DD_TOOL_VERSION\") | .value) = \"operatorhub\"" $COMM_OPS_PATH/manifests/datadog-operator.clusterserviceversion.yaml
