#!/bin/bash
set -euo pipefail

#
# This script ensures that non-testing code never uses io.ReadAll and io.Copy and
# prefers their netxlite counterparts who also take a context argument.
#
# See https://github.com/ooni/probe/issues/1609.
#

exitcode=0
for file in $(find . -type f -name \*.go); do
	if [ "$file" = "./internal/netemx/ooapi_test.go" ]; then
		# We're allowed to use ReadAll and Copy in this file because
		# it's code that we only use for testing purposes.
		continue
	fi

	if [ "$file" = "./internal/netxlite/iox.go" ]; then
		# We're allowed to use ReadAll and Copy in this file to
		# implement safer wrappers for these functions.
		continue
	fi

	if [ "$file" = "./internal/must/must_test.go" ]; then
		# We're allowed to use ReadAll and Copy in this file to
		# avoid depending on netxlite, given that netxlite's test
		# suite already depends on must.
		continue
	fi

	if [ "$file" = "./internal/shellx/shellx.go" ]; then
		# We're allowed to use ReadAll and Copy in this file to
		# avoid depending on netxlite, given that netxlite's does
		# not compile with go1.18 due to the quic dependency.
		continue
	fi

	if [ "$file" = "./internal/shellx/shellx_test.go" ]; then
		# We're allowed to use ReadAll and Copy in this file to
		# avoid depending on netxlite, given that netxlite's does
		# not compile with go1.18 due to the quic dependency.
		continue
	fi

	if [ "$file" = "./internal/testingsocks5/request.go" ]; then
		# We're allowed to use ReadAll and Copy in this file because
		# it's code that we only use for testing purposes.
		continue
	fi

	if [ "$file" = "./internal/testingx/dnsoverhttps.go" ]; then
		# We're allowed to use ReadAll and Copy in this file because
		# it's code that we only use for testing purposes.
		continue
	fi

	if [ "$file" = "./internal/testingx/dnsoverhttps_test.go" ]; then
		# We're allowed to use ReadAll and Copy in this file because
		# it's code that we only use for testing purposes.
		continue
	fi

	if [ "$file" = "./internal/testingx/httpproxy.go" ]; then
		# We're allowed to use ReadAll and Copy in this file because
		# it's code that we only use for testing purposes.
		continue
	fi

	if [ "$file" = "./internal/testingx/httptestx.go" ]; then
		# We're allowed to use ReadAll and Copy in this file because
		# it's code that we only use for testing purposes.
		continue
	fi

	if [ "$file" = "./internal/testingx/httptestx_test.go" ]; then
		# We're allowed to use ReadAll and Copy in this file because
		# it's code that we only use for testing purposes.
		continue
	fi

	if [ "$file" = "./internal/testingx/oonibackendwithlogin.go" ]; then
		# We're allowed to use ReadAll and Copy in this file because
		# it's code that we only use for testing purposes.
		continue
	fi

	if [ "$file" = "./internal/testingx/oonibackendwithlogin_test.go" ]; then
		# We're allowed to use ReadAll and Copy in this file because
		# it's code that we only use for testing purposes.
		continue
	fi

	if [ "$file" = "./internal/testingx/oonicollector.go" ]; then
		# We're allowed to use ReadAll and Copy in this file because
		# it's code that we only use for testing purposes.
		continue
	fi

	if [ "$file" = "./internal/testingx/oonicollector_test.go" ]; then
		# We're allowed to use ReadAll and Copy in this file because
		# it's code that we only use for testing purposes.
		continue
	fi

	if [ "$file" = "./internal/testingx/tlssniproxy.go" ]; then
		# We're allowed to use ReadAll and Copy in this file because
		# it's code that we only use for testing purposes.
		continue
	fi

	if [ "$file" = "./internal/testingx/tlsx_test.go" ]; then
		# We're allowed to use ReadAll and Copy in this file because
		# it's code that we only use for testing purposes.
		continue
	fi

	if [ "$file" = "./internal/testingx/geoip_test.go" ]; then
		# We're allowed to use ReadAll and Copy in this file because
		# it's code that we only use for testing purposes.
		continue
	fi

	if [ "$file" = "./pkg/gobash/version.go" ]; then
		# We're allowed to use ReadAll and Copy in this file because
		# this code is only used by the build system
		continue
	fi

	if grep -q 'io\.ReadAll' $file; then
		echo "in $file: do not use io.ReadAll, use netxlite.ReadAllContext" 1>&2
		exitcode=1
	fi
	if grep -q 'ioutil\.ReadAll' $file; then
		echo "in $file: do not use ioutil.ReadAll, use netxlite.ReadAllContext" 1>&2
		exitcode=1
	fi

	if grep -q 'io\.Copy' $file; then
		echo "in $file: do not use io.Copy, use netxlite.CopyContext" 1>&2
		exitcode=1
	fi
	if grep -q 'ioutil\.Copy' $file; then
		echo "in $file: do not use ioutil.Copy, use netxlite.CopyContext" 1>&2
		exitcode=1
	fi
done
exit $exitcode
