#!/bin/sh

INSTALL_PKGS="go-toolset-1.13.*"
OPERATOR_PATH=/usr/local/bin/kie-cloud-operator
UI_PATH=/usr/local/bin/console-cr-form

yum -y install ${INSTALL_PKGS}

REPO=kie-cloud-operator
GHORG=github.com/kiegroup
GHFULL=${GHORG}/${REPO}
GOPATH=$(go env GOPATH)
GOCACHE=$(go env GOCACHE)
go version

mkdir -p ${GOPATH}/src/${GHFULL}
tar xfz /tmp/artifacts/kie-cloud-operator.tar.gz --strip-components=1 -C ${GOPATH}/src/${GHFULL}
cd ${GOPATH}/src/${GHFULL} && pwd

export GOFLAGS=-mod=vendor
export GO111MODULE=on
go generate ./...
go vet ./...
go test ./...
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -mod=vendor -a -o ${OPERATOR_PATH} ./cmd/manager
if [[ ! -f ${OPERATOR_PATH} ]] ; then
    echo "${OPERATOR_PATH} does not exist, aborting."
    exit 1
fi
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -mod=vendor -a -o ${UI_PATH} ./cmd/ui
if [[ ! -f ${UI_PATH} ]] ; then
    echo "${UI_PATH} does not exist, aborting."
    exit 1
fi

mv build/bin/* /usr/local/bin/
/usr/local/bin/user_setup

yum -y autoremove ${INSTALL_PKGS}
yum -y update-minimal --setopt=tsflags=nodocs --security --sec-severity=Important --sec-severity=Critical
yum clean all
rm -rf /var/cache/yum ${GOPATH} ${GOCACHE}
