#/bin/bash

# Check oc version
oc version

# Setup the the file path of the registry credentials
# Previously needs to login into your private registry and in registry.redhat.io
# EG: podman login ${REGISTRY_HOST} -u ${REGISTRY_HOST_USER} -p ${REGISTRY_HOST_PASSWORD} --tls-verify=false
export REG_CREDS=${XDG_RUNTIME_DIR}/containers/auth.json

# Set the Registry host where operator image bundle will be pushed and used by catalog source
export REGISTRY_HOST=<registry_host>:<port>

# Set quay user and password to obtain quay.io token..
export QUAY_USERNAME=XXXXXXX ; export QUAY_PASSWORD=XXXXXXX
export AUTH_TOKEN=$(curl -sH "Content-Type: application/json" -XPOST https://quay.io/cnr/api/v1/users/login -d '
{
    "user": {  
        "username": "'"${QUAY_USERNAME}"'",
        "password": "'"${QUAY_PASSWORD}"'"
    }
}' | jq -r '.token')

# Build a catalog image based on the redhat-operators catalog from quay.io, tagging and pushing it to your mirror registry
# Arguments:
# - --appregistry-org: Organization (namespace) to pull from an App Registry instance
# - --from: ose-operator-registry base image using the tag that matches the target OpenShift Container Platform cluster major and minor version
# - --filter-by-os: the operating system and architecture to use for the base image, which must match the target OpenShift Container Platform cluster. Valid values are linux/amd64, linux/ppc64le, and linux/s390x
# - --to: Name your catalog image and include a tag, for example, v1
# - -a: Optional: If required, specify the location of your registry credentials file
# - --auth-token: Optional: If other application registry catalogs are used that are not public, specify a Quay authentication token
# More info: https://docs.openshift.com/container-platform/4.4/operators/olm-restricted-networks.html
oc adm catalog build \
    --appregistry-org redhat-operators \
    --from=registry.redhat.io/openshift4/ose-operator-registry:v4.4 \
    --filter-by-os="linux/amd64" \
    --to=${REGISTRY_HOST}/olm/redhat-operators:1 \
    -a ${REG_CREDS} \
    --insecure \
    --auth-token "${AUTH_TOKEN}"
# The oc adm catalog mirror command extracts the contents of your custom Operator catalog image to generate the manifests required for mirroring.
# Arguments:
# - Operator catalog image
# - insecure flag to avoid tls errros
# - operating system to mirror
# More info: https://docs.openshift.com/container-platform/4.4/operators/olm-restricted-networks.html
oc adm catalog mirror --insecure --manifests-only=true ${REGISTRY_HOST}/olm/redhat-operators:1 ${REGISTRY_HOST} --filter-by-os="linux/amd64" -a ${REG_CREDS}

# Filter CRW images to mirrror
sed -i \
-e 's/^\(.*codeready.*\)$/TOKEEP\1/' \
-e 's/^\(.*rhscl.*\)$/TOKEEP\1/' \
-e 's/^\(.*ubi8-minimal.*\)$/TOKEEP\1/' \
-e 's/^\(.*redhat-sso-7.*\)$/TOKEEP\1/' \
-e 's/^\(.*rh-sso-7.*\)$/TOKEEP\1/' \
-e 's/^\(.*jboss-eap-7.*\)$/TOKEEP\1/' \
-e 's/^\(.*postgresql.*\)$/TOKEEP\1/' \
-e '/^TOKEEP.*/!d' \
-e '/.*@sha256:.*/!d' \
-e 's/TOKEEP//g' \
redhat-operators-manifests/mapping.txt

# Manually change the image content source policy for images from the production RedHat catalog
for image in $(echo 'codeready-workspaces/theia-rhel8
codeready-workspaces/theia-endpoint-rhel8
codeready-workspaces/plugin-java8-rhel8
codeready-workspaces/plugin-kubernetes-rhel8
codeready-workspaces/plugin-openshift-rhel8
codeready-workspaces/plugin-java11-rhel8
codeready-workspaces/stacks-golang-rhel8
codeready-workspaces/stacks-cpp-rhel8
codeready-workspaces/stacks-dotnet-rhel8
codeready-workspaces/stacks-python-rhel8
codeready-workspaces/stacks-node-rhel8
codeready-workspaces/stacks-php-rhel8
codeready-workspaces/stacks-java-rhel8
codeready-workspaces/machineexec-rhel8
rhscl/mysql-57-rhel7
rhscl/mongodb-34-rhel7
rhscl/mongodb-36-rhel7
jboss-eap-7/eap73-openjdk8-openshift-rhel7')
do  
    echo "Reverting mirror to 'regisry.redhat.io' for image: $image"
    sed -i -e "s#registry\.stage\.redhat\.io/$image#registry.redhat.io/$image#" redhat-operators-manifests/imageContentSourcePolicy.yaml 
done

# Apply the catalog Source in the disconnected cluster
cat <<EOF | oc apply -f -
apiVersion: operators.coreos.com/v1alpha1
kind: CatalogSource
metadata:
  name: redhat-restricted-operators
  namespace: openshift-marketplace
spec:
  sourceType: grpc
  image: ${REGISTRY_HOST}/olm/redhat-operators:1 
  displayName: My Restricted Code Ready Workspaces Catalog
  publisher: grpc
EOF
