#!/usr/bin/env bash

echo -e "Quickwit (type $suffix):"
if find ./quickwit/data/indexes/${test}/*.split >/dev/null 2>&1; then
  echo -e "${COLORED}\tNo need to rebuild${NC}"
  exit 10
fi

docker-compose -f ../../docker-compose.yml --env-file ../../.env stop quickwit
docker-compose -f ../../docker-compose.yml --env-file ../../.env rm -f quickwit

suffix=$suffix docker-compose -f ../../docker-compose.yml --env-file ../../.env up -d quickwit

if timeout 10 docker logs -f quickwit_engine | grep -qm1 'REST server is ready'; then
  echo -e "\tQuickwit ready to accept connections"
else
  echo -e "\tQuickwit timeout failed"
  exit 1
fi

export HOST='http://localhost:7280'

echo -e "\tStart building quickwit_engine"

curl -s -XPOST "${HOST}/api/v1/indexes" \
  --header "content-type: application/yaml" \
  --data-binary @./quickwit/index-config.yaml

if [ ! -f data/data.jsonl ]; then
    echo -e "\tConvert CSV to JSONL. The current operation might take time. You can check progress by 'cat ./data/data.jsonl | wc -l'"
    php ./quickwit/csv_jsonl.php ./data/data.csv ./data/data.jsonl
    echo -e "\tEnd converting"
fi

split -l 10000 -a 5 ./data/data.jsonl ./data/data_splitted.
