#!/bin/bash -ex

echo "render_templates START..."

rm -rf src

source include/ocs.sh
export OCS_VERSION=$(echo $CPAAS_PRODUCT_VERSION | cut -d'.' -f -2)

# Install golang 1.16, from include/ocs.sh
get_golang_1_16

# Get the ocs-operator sources for the commit
git clone "https://github.com/red-hat-storage/odf-operator" src

pushd src

git reset --hard "568484e224464537e6ce4ce386456e8d1a7e9b52"
git clean -fxd

# Login to RH registry for getting info about sidecar images with a service account
skopeo login --authfile .skopeo-auth.json -u '6340056|ocs-cpaas' -p 'eyJhbGciOiJSUzUxMiJ9.eyJzdWIiOiJhNDAzMTM3MTdiZGE0ODYxOTVlZTI1ZTQwZDcwM2QxMyJ9.VhReKSe542C8xjlRV3fkGF0DeN6t24YBjnbmn_9u9togxhxb5MBqlF3yZgay7TVFZzFqaiXk3fHzIAv39HHIjaC-LFED7RF63mCBpF-7T8Y4Yvr7NpcEDCi_GHrUB_369tb61N85CVQPloEN8JGuQuzMGV0itfCP8MA20kRj9Us9b8C2wOLl6z6SdU0P2BBLkwkhdypmnkp--FerEgcTj2BN3kZxsR7h_v52naEv9UXGospr-QPwUBHEu4W06BtJu_WFqz8xJWmVSiSSvpLRs0_dI0XtavVys2_jzqPgS6eY7UBXlJ2CX6t8oRoOoaum-PuxeqNG-KuKn2ntLJyrKgau8gGLloToFODu021K6kJYNc706VZ7Y98lkfME3inbq8MgM0C-FYvy2tvc-ji4vtzasynsHXaZmwBrg0aJcskbG8FG5r0BLUpzAFcie69G_Ya2DsVKy72MicFjCxkuciQB0CUaJMx1H-sXD7iqVU0CCi09gGfO4DYsLOadg1JOrwwh-EvYWCdbGRh3uyPAtVP2zLMWCkdiDJQVhFlQGU9Aw9ffxx_UhNRpJBgLmb_ANJZnrvEu7etPnz0cUHs32HYT23wlR4e10hgqSsem7Xpucb_xpYGkOa8pUnVS2k45rCkhToaVWDZ10R4ASz8pwRnmbUHiyTfJ03nwWA-Ni1Y' registry.redhat.io

# Set the version to copy the ODF version
export CSV_VERSION="${CPAAS_PRODUCT_VERSION}"

rm -rf bundle/manifests

### Define the dependencies
# Versions are tied across all the bundles DS
export VERSION="$CSV_VERSION"

# Set the channel specs
export CHANNELS="stable-$OCS_VERSION"
export DEFAULT_CHANNEL="stable-$OCS_VERSION"

# Set the catalog source and the namespace
export OPERATOR_NAMESPACE="openshift-storage"
export OPERATOR_CATALOGSOURCE="redhat-operators"

# Override noobaa/mcg-operator variables
export NOOBAA_BUNDLE_NAME="mcg-operator"
export NOOBAA_BUNDLE_IMG_TAG="v$VERSION"
export NOOBAA_SUBSCRIPTION_CATALOGSOURCE_NAMESPACE="openshift-marketplace"

# Override ocs-operator variables
export OCS_BUNDLE_NAME="ocs-operator"
export OCS_BUNDLE_IMG_TAG="v$VERSION"
export OCS_SUBSCRIPTION_CATALOGSOURCE_NAMESPACE="openshift-marketplace"

# Override ibm-operator variables
export IBM_SUBSCRIPTION_CATALOGSOURCE="certified-operators"
export IBM_SUBSCRIPTION_CATALOGSOURCE_NAMESPACE="openshift-marketplace"

# Set the components image
export IMG=$(tag_to_sha ${ODF_OPERATOR_IMAGE_PULL_URL})
export ODF_CONSOLE_IMG=$(tag_to_sha ${ODF_CONSOLE_IMAGE_PULL_URL})
export RBAC_PROXY_IMG=$(tag_to_sha "${SIDECAR_MAP[rbac-proxy]}")

# Set skipRange and replaces for clean upgrade paths
export SKIP_RANGE=">=4.2.0 <${CSV_VERSION}"
CSV_Z_VERSION=$(echo "${CSV_VERSION}" | cut -d '.' -f 3)
# Add the replaces line for non-X.Y.0 builds to the CSV to keep the older bundles unmasked
if test "$CSV_Z_VERSION" -gt 0; then
        export REPLACES="odf-operator.v$OCS_VERSION.$(($CSV_Z_VERSION - 1))"
fi

make bundle

#Logout from RH registry
skopeo logout --authfile .skopeo-auth.json registry.redhat.io

popd

# Modify the dist-git
# Remove old manifests and metadata
rm -rf bundle
mkdir bundle

# Copy the new manifests to the dist-git
cp -a src/bundle/manifests/ ./bundle/
cp -a src/bundle/metadata/ ./bundle/

# TODO: Remove this hack once odf-operator repo implements the feature
cat >> "bundle/manifests/odf-operator.clusterserviceversion.yaml" << EOF
  relatedImages:
  - image: $IMG
    name: odf-operator
  - image: $ODF_CONSOLE_IMG
    name: odf-console
  - image: $RBAC_PROXY_IMG
    name: rbac-proxy
EOF

# Get release number
RELEASE=$(cat Dockerfile|egrep -e 'release="' | sed -e 's|^.*release="\(.*\)".*$|\1|')

## The release number magic
# Bump the release field if uncommented, uncomment release field without bumping it if commented out
sed -i -e "s|^LABEL release=.*\$|LABEL release=\"$((RELEASE+1))\"|" Dockerfile
sed -i -e "s|^#LABEL release=.*\$|LABEL release=\"$RELEASE\"|" Dockerfile

# Get an actual current release to update full_version properly
RELEASE=$(cat Dockerfile|egrep -e 'release="' | sed -e 's|^.*release="\(.*\)".*$|\1|')

# Add the full_version label for QE to be able to identify the build
sed -i "/^  labels:\$/a\    full_version: $CSV_VERSION-$RELEASE" bundle/manifests/odf-operator.clusterserviceversion.yaml

# Update the example odf-operator image
sed -i "s|quay.io/ocs-dev/odf-operator:.*\$|$IMG|g" bundle/manifests/odf-operator.clusterserviceversion.yaml

# Only set the default channel for the X.Y.0 builds
CSV_Z_VERSION=$(echo "${CSV_VERSION}" | cut -d '.' -f 3)
if test "$CSV_Z_VERSION" -eq 0; then
        # Update the default channel
        sed -i -e "s|^.*LABEL operators.operatorframework.io.bundle.channel.default.v1=.*$|LABEL operators.operatorframework.io.bundle.channel.default.v1=stable-${OCS_VERSION}|" Dockerfile
        sed -i -e "s|^.*operators.operatorframework.io.bundle.channel.default.v1:.*$|  operators.operatorframework.io.bundle.channel.default.v1: \"stable-${OCS_VERSION}\"|" bundle/metadata/annotations.yaml
else
        # Comment out the default channel
        sed -i -e "s|^.*LABEL operators.operatorframework.io.bundle.channel.default.v1=.*$|#LABEL operators.operatorframework.io.bundle.channel.default.v1=stable-${OCS_VERSION}|" Dockerfile
        sed -i -e "s|^.*operators.operatorframework.io.bundle.channel.default.v1:.*$|  #operators.operatorframework.io.bundle.channel.default.v1: \"stable-${OCS_VERSION}\"|" bundle/metadata/annotations.yaml
fi

sed -i -e "s|^.*operators.operatorframework.io.bundle.channels.v1:.*$|  operators.operatorframework.io.bundle.channels.v1: \"stable-${OCS_VERSION}\"|" bundle/metadata/annotations.yaml
sed -i -e "s|^.*operators.operatorframework.io.bundle.package.v1:.*$|  operators.operatorframework.io.bundle.package.v1: odf-operator|" bundle/metadata/annotations.yaml

# Update versions of dependencies if they exist
if test -e bundle/metadata/dependencies.yaml; then
        sed -i -e "s/version: .*/version: \"<$OCS_VERSION.0 || $VERSION\"/g" bundle/metadata/dependencies.yaml
fi

rm -rf src
