#!/usr/bin/env bash

set -ev

export FLASK_APP=delphi_api.run:flask_app

ENVIRONMENT=${ENVIRONMENT:-test}

RUN_DATA_SEEDER=${RUN_DATA_SEEDER:-false}

if [ ! ${RUN_DATA_SEEDER} = 'true' ]; then
    echo -e "RUN_DATA_SEEDER != true. \nSkipping data seeding."
    exit 0
fi

# wait for postgres server ready to accept connections - timeout is 10 seconds
echo 'QUIT' | nc -w 10 ${POSTGRES_ADDRESS:-delphi-api-db} ${POSTGRES_PORT:-5432}; echo $?

SEEDER_DATASET=$([ ${ENVIRONMENT} = "test" ] && echo "test" || echo "beta")

# Run database seeder
python -m delphi_api.seeds.data_seeder run-seeder ${SEEDER_DATASET} --batch-size=50000 --truncate
