#!/bin/bash -e

# Wait for pypi server to be ready (workers need time to load ddtrace)
max_retries=30
retry_interval=5
for i in $(seq 1 $max_retries); do
  if curl -sf -o /dev/null http://pypi/simple/; then
    echo "pypi server is ready"
    break
  fi
  if [ "$i" -eq "$max_retries" ]; then
    echo "pypi server did not become ready in time"
    exit 1
  fi
  echo "Waiting for pypi server... attempt $i/$max_retries"
  sleep $retry_interval
done

# Upload a package
curl -f -F ':action=file_upload' -F 'name=owslogger' -F 'version=1.1.1' -F 'content=@/var/app/owslogger-1.1.1.tar.gz' http://pypi/simple/

# Now install the recently uploaded package
python3 -m venv venv
./venv/bin/pip install --trusted-host pypi -i http://pypi/pypi/ owslogger
