#!/bin/bash
set -e

if [ -n "$SKIP_VALIDATE" ]; then
    echo Skipping validation
    exit
fi

cd $(dirname $0)/..

echo Running: install script signature checks
sha256sum -c install.sh.sha256sum

echo Running: go mod tidy
go mod tidy

echo Running: go generate
GOOS=linux CC=gcc CXX=g++ go generate

echo Running validation

. ./scripts/version.sh

if [ -n "$DIRTY" ]; then
    echo Source dir is dirty
    git status --porcelain --untracked-files=no
    git diff
    exit 1
fi

echo Running: go version
DEPENDENCIES_URL="https://raw.githubusercontent.com/kubernetes/kubernetes/${VERSION_K8S}/build/dependencies.yaml"
GOLANG_VERSION=$(curl -sL "${DEPENDENCIES_URL}" | yq e '.dependencies[] | select(.name == "golang: upstream version").version' -)
if ! go version | grep -s "go version go${GOLANG_VERSION} "; then
  echo "Unexpected $(go version) - Kubernetes ${VERSION_K8S} should be built with go version go${GOLANG_VERSION}"
  exit 1
fi

echo Running: go mod verify
go mod verify

if [ ! -e build/data ];then
    mkdir -p build/data
fi

if ! command -v golangci-lint; then
    echo Skipping validation: no golangci-lint available
    exit
fi

echo Running: golangci-lint
# https://github.com/golangci/golangci-lint/issues/2788
CGO_ENABLED=0 golangci-lint run -v
