#!/usr/bin/env bash

set -euo pipefail

export COCKROACH_INCONSISTENT_TIME_ZONES=true

# Verify arguments.
if [[ $# -ne 3 ]]
then
  echo "usage: $0 COCKROACH-BINARY EXPECTED-VERSION EXPECTED-SHA" >&2
  exit 1
fi

readonly cockroach="$1"
readonly version="$2"
readonly sha="$3"
readonly urlfile=cockroach-url
readonly pidfile=/tmp/server_pid

# Verify that the startup time is within a reasonable upper-bound. 
# At the time of writing, the total `init` time is well under 200ms on m1pro.
max_init_time=500
if [[ $(uname -sm) == "Darwin x86_64" ]]; then
  # MacOS Intel GitHub Actions runners are a bit slow
  max_init_time=3000
fi
init_time=`GODEBUG=inittrace=1 $cockroach version 2>&1|awk '/init/ {sum += $5} END {printf("%d\n", sum)}'`
if [[ $init_time -gt $max_init_time ]]
then
  echo "'init' time is unreasonably high: $init_time"
  GODEBUG=inittrace=1 $cockroach version 2>&1 | grep init | awk '{print $2, $5}' | sort -k2,2 -rn | head
  exit 1
fi

echo ""
echo "Total 'init' time: $init_time ms"

# Verify that the total heap-allocated bytes inside 'init' functions is within a reasonable upper-bound.
# At the time of writing, the total heap-allocated bytes is ~265MB (of which ~198MB is used by UI assets).
total_bytes=`GODEBUG=inittrace=1 $cockroach version 2>&1|awk '/init/ {sum += $8} END {printf("%d\n", sum)}'`
if [[ $total_bytes -gt 536870912 ]]
then
  echo "Total 'init' heap-allocated bytes is unreasonably high: $total_bytes"
  exit 1
fi

echo "Total 'init' heap-allocated bytes: $total_bytes"
echo ""


# Display build information.
echo ""
"$cockroach" version
echo ""

# Start a CockroachDB server, wait for it to become ready, and arrange for it to
# be force-killed when the script exits.
rm -f "$urlfile"

# Generate encryption key.
echo "Generating encryption key:"
"$cockroach" gen encryption-key aes-128.key
echo ""

# Start node with encryption enabled.
"$cockroach" start-single-node --listen-addr 127.0.0.1:26257 --insecure --listening-url-file="$urlfile" --enterprise-encryption=path=cockroach-data,key=aes-128.key,old-key=plain --pid-file="$pidfile" &

trap "kill -9 $! || true" EXIT
for i in {0..8}
do
  [[ -f "$urlfile" ]] && break
  backoff=$((2 ** i))
  echo "server not yet available; sleeping for $backoff seconds"
  sleep $backoff
done

# Verify the output of a simple SQL query.
"$cockroach" sql --insecure <<EOF
  CREATE DATABASE bank;
  CREATE TABLE bank.accounts (id INT PRIMARY KEY, balance DECIMAL);
  INSERT INTO bank.accounts VALUES (1, 1000.50);
EOF
cat > expected <<EOF
id	balance
1	1000.50
EOF
"$cockroach" sql --insecure -e 'SELECT * FROM bank.accounts' > actual
diff -u expected actual

# Verify libgeos functionality on all platforms except MacOS ARM64 and Windows
if [[ $(uname -sm) == "Darwin arm64" || $(uname -s) == "MINGW64_NT"* ]]; then
  echo "Skipping libgeos tests"
else
  echo "Testing libgeos functionality"
  cat > expected <<EOF
is_valid
t
EOF
  "$cockroach" sql --insecure -e 'SELECT ST_IsValid(ST_MakePoint(1, 1)) is_valid;' > actual
  diff -u expected actual
fi

# Attempt a clean shutdown for good measure. We'll force-kill in the atexit
# handler if this fails.
kill -TERM $(cat "$pidfile") || true
trap - EXIT

# Verify reported version matches expected version.
echo "$version" > expected
"$cockroach" version | grep 'Build Tag' | cut -f2 -d: | tr -d ' ' > actual
diff -u expected actual

# Verify reported SHA matches expected SHA.
echo "$sha" > expected
"$cockroach" version | grep 'Build Commit ID' | cut -f2 -d: | tr -d ' ' > actual
diff -u expected actual
