#!/usr/bin/env bash

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

SCRIPT_ROOT="$(dirname "${BASH_SOURCE[0]}")/.."

bash "${SCRIPT_ROOT}/vendor/k8s.io/code-generator/kube_codegen.sh" deepcopy \
  github.com/openshift/openshift-network-operator/pkg/generated github.com/openshift/cluster-network-operator/pkg/apis \
  "network:v1" \
  --go-header-file "${SCRIPT_ROOT}/hack/custom-boilerplate.go.txt"


echo "Generating CRDs"
mkdir -p _output/crds
GO111MODULE=on GOFLAGS=-mod=vendor go build -tags=tools -o _output/bin/controller-gen sigs.k8s.io/controller-tools/cmd/controller-gen
_output/bin/controller-gen crd paths=./pkg/apis/... output:crd:dir=_output/crds

HEADER="# This file is automatically generated. DO NOT EDIT"

## Add a new CRD? Duplicate these lines
echo "${HEADER}" > manifests/0000_70_cluster-network-operator_01_pki_crd.yaml
cat _output/crds/network.operator.openshift.io_operatorpkis.yaml >> manifests/0000_70_cluster-network-operator_01_pki_crd.yaml

# and also the CRD from library-go
rm -f "manifests/0000_70_network_01_networks"*.yaml
for file in "vendor/github.com/openshift/api/operator/v1/zz_generated.crd-manifests/0000_70_network_01_networks"*.yaml; do
  # Extract the filename from the path
  filename=$(basename "${file}")

  echo "${HEADER}" > "manifests/${filename}"
  cat "${file}" >>  "manifests/${filename}"
done

echo "${HEADER}" > manifests/0000_70_cluster-network-operator_01_egr_crd.yaml
cat vendor/github.com/openshift/api/networkoperator/v1/zz_generated.crd-manifests/001_egressrouters.crd.yaml >> manifests/0000_70_cluster-network-operator_01_egr_crd.yaml

echo "${HEADER}" > bindata/cloud-network-config-controller/common/001-crd.yaml
cat vendor/github.com/openshift/api/cloudnetwork/v1/zz_generated.crd-manifests/001_cloudprivateipconfigs.crd.yaml >> bindata/cloud-network-config-controller/common/001-crd.yaml
