#!/bin/bash -ex

echo "render_templates START..."

rm -rf noobaa-operator

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/noobaa/noobaa-operator.git" noobaa-operator

pushd noobaa-operator

git reset --hard "b946eff4c093225f490c8570794c862bb1802773"
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 build/_output/manifests

# Set the noobaa images
export NOOBAA_IMAGE=$(tag_to_sha ${NOOBAA_OPERATOR_IMAGE_PULL_URL})
export NOOBAA_CORE_IMAGE=$(tag_to_sha ${NOOBAA_CORE_IMAGE_PULL_URL})
export NOOBAA_DB_IMAGE=$(tag_to_sha "${SIDECAR_MAP[noobaa-db]}")

# 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
export REPLACES=""
if test "$CSV_Z_VERSION" -gt 0; then
        export REPLACES="mcg-operator.v$OCS_VERSION.$(($CSV_Z_VERSION - 1))"
fi

make gen-odf-package "csv-name=mcg-operator.clusterserviceversion.yaml" "operator-image=${NOOBAA_IMAGE}" "core-image=${NOOBAA_CORE_IMAGE}" "db-image=${NOOBAA_DB_IMAGE}" "skip-range=${SKIP_RANGE}" "replaces=${REPLACES}"

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

popd

### Modify manifests ###
# Remove old manifests dir
rm -rf bundle/manifests
mkdir bundle/manifests

# 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|')

# Copy the new manifests to dist-git
# Add OCS operator to the related images field
cp -a noobaa-operator/build/_output/manifests/ ./bundle

## We need to modify the noobaa-operator version
# Install proper yq yaml parser, first
echo "Downloading yq..."
curl -Ls https://github.com/mikefarah/yq/releases/download/2.4.0/yq_linux_amd64 -o yq
export YQ_TOOL=$(readlink -f ./yq)
chmod +x "$YQ_TOOL"

### Fix CSV version

PKG_FILE="bundle/manifests/mcg-operator.clusterserviceversion.yaml"

"$YQ_TOOL" w -i "$PKG_FILE" 'metadata.name' "mcg-operator.v${CSV_VERSION}"
"$YQ_TOOL" w -i "$PKG_FILE" 'spec.version' "${CSV_VERSION}"

# Add the full_version label for QE to be able to identify the build
sed -i "/^  labels:\$/a\    full_version: $CPAAS_PRODUCT_VERSION-$RELEASE" "$PKG_FILE"

# 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: mcg-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 noobaa-operator "$YQ_TOOL"
