#!/bin/bash
set -e

echo Running: flake8 python linting

cd $(dirname $0)/../tests/integration
tox -e flake8

cd ../..

if ! command -v golangci-lint; then
  echo "missing golangci-lint"
  exit
fi

echo Running: golangci-lint
golangci-lint run

echo Tidying up modules
cd pkg/apis/
go mod tidy
cd ../..
go mod tidy

echo Verifying modules
go mod verify

dirty_files="$(git status --porcelain --untracked-files=no)"
if [ -n "$dirty_files" ]; then
  echo "Encountered dirty repo! Aborting."
  echo "If you're seeing this, it means there are uncommitted changes in the repo."
  echo "If you're seeing this in CI, it probably means that your Go modules aren't tidy, or more generally that running"
  echo "validation would result in changes to the repo. Make sure you're up to date with the upstream branch and run"
  echo "'go mod tidy' and commit the changes, if any. The offending changed files are as follows:"
  echo "$dirty_files"
  exit 1
fi

# Check diff between ./go.mod and ./pkg/apis/go.mod
badmodule="false"
while read -r module tag; do
  # Get tag from module in ./go.mod
  roottag=$(cat go.mod | sed '1,/^require/d' | grep "${module} " | awk '{ print $2 }')
  echo "${module}:"
  echo "${tag} (./pkg/apis/go.mod)"
  echo "${roottag} (./go.mod)"
  # Compare with tag from module in ./pkg/apis/go.mod
  if [[ "${tag}" != "${roottag}" ]]; then
    echo "${module} is different ("${tag}" vs "${roottag}")"
    badmodule="true"
  fi
done <<<$(cat pkg/apis/go.mod | sed '1,/require/d' | head -n -1 | grep -v indirect | grep rancher | awk '{ print $1,$2 }')

if [[ "${badmodule}" == "true" ]]; then
  echo "Diff found between ./go.mod and ./pkg/apis/go.mod"
  exit 1
fi
