#!/usr/bin/env bash

# Your Sourcegraph server URL
SOURCEGRAPH_URL="https://sourcegraph.nonprod.dwpcloud.uk"

# Your user token
USER_TOKEN="$SOURCEGRAPH_TOKEN"

# API endpoint to get users
API_ENDPOINT="/.api/graphql"

# Max User Count
MAX_USER_COUNT=200

# GraphQL query to get the number of users
GRAPHQL_QUERY='{"query":"{ users { totalCount } }"}'

# Function to get the user count
get_user_count() {
  curl -s -H "Authorization: token $USER_TOKEN" \
    -H "Content-Type: application/json" \
    -d "$GRAPHQL_QUERY" \
    "$SOURCEGRAPH_URL$API_ENDPOINT" |
    jq '.data.users.totalCount'
}

# Check the user count and raise an alert if it reaches 200
user_count=$(get_user_count)
if [ "$user_count" -ge "$MAX_USER_COUNT" ]; then
  echo "Alert: User count has reached $user_count"
else
  echo "Current user count is $user_count"
fi
