#!/usr/bin/env bash

# Copyright 2020 Kohl's Department Stores, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# A simple script to update the resources generated by the operator-sdk

set -euxo pipefail

OPERATOR_SDK_VERSION="v0.17.1"

OS_TYPE="$(uname -s)" # Linux or Darwin
if [ "${OS_TYPE}" == "Darwin" ]; then
    OPERATOR_SDK_SUFFIX="x86_64-apple-darwin"
elif [ "${OS_TYPE}" == "Linux" ]; then
    OPERATOR_SDK_SUFFIX="x86_64-linux-gnu"
else
    echo "could not install operator-sdk"
    exit 1
fi

curl -L https://github.com/operator-framework/operator-sdk/releases/download/${OPERATOR_SDK_VERSION}/operator-sdk-${OPERATOR_SDK_VERSION}-${OPERATOR_SDK_SUFFIX} -o ./operator-sdk
chmod +x ./operator-sdk
go build -o ./openapi-gen k8s.io/kube-openapi/cmd/openapi-gen

./openapi-gen --logtostderr=true \
    -i ./pkg/apis/eunomia/v1alpha1 \
    -o "" \
    -O zz_generated.openapi \
    -p ./pkg/apis/eunomia/v1alpha1 \
    -h ./scripts/boilerplate_go.txt \
    -r "-" \
    -v 2
echo "generating crds..."
./operator-sdk --verbose generate crds
echo "generating k8s..."
./operator-sdk --verbose generate k8s

rm operator-sdk openapi-gen
