#!/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-multicluster-orchestrator" src

pushd src

git reset --hard "007b3d80ba92453cd5d5bb47bd6c967773dfb551"
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

export CHANNELS="stable-$OCS_VERSION"
export DEFAULT_CHANNEL="stable-$OCS_VERSION"

export VERSION="$CSV_VERSION"

export IMAGE_REGISTRY="quay.io"
export REGISTRY_NAMESPACE="rhceph-dev"
export IMAGE_TAG="$ODF_MULTICLUSTER_OPERATOR_TAG"
export IMAGE_NAME="odf-multicluster-operator"

# Set the odf-multicluster-operator image
export IMG=$(tag_to_sha ${ODF_MULTICLUSTER_OPERATOR_IMAGE_PULL_URL})

# 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-multicluster-orchestrator.v$OCS_VERSION.$(($CSV_Z_VERSION - 1))"
fi

# envsubst is not available, use tee symlink instead, fix the IMG later
ln -s $(which tee) "$GOPATH/bin/envsubst"

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: Fix the IMG, envsubst is missing so we have to do it manually for now
sed -i -e "s|\${IMG}|$IMG|g" bundle/manifests/odf-multicluster-orchestrator.clusterserviceversion.yaml

# TODO: Remove this hack once odf-multicluster-operator repo implements the feature
cat >> "bundle/manifests/odf-multicluster-orchestrator.clusterserviceversion.yaml" << EOF
  relatedImages:
  - image: $IMG
    name: odf-multicluster-operator
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-multicluster-orchestrator.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-multicluster-orchestrator|" 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
