#!/bin/bash
product_list_file=$1
docker exec -i $(docker ps -qf "name=emg-pvs_registrar") python3 /var/www/pvs/dev/pvs_instance/manage.py storage create pvs_testing pvs_testing --type swift --storage-auth auth-cloud-ovh
IFS=","
# For testing purposses 'service url' and 'reporting directory' values can be fixed and not mapped from env
SERVICE_URL=emg.pass.copernicus.eu
REPORTING_DIR=/mnt/reports/
while read  product; do
echo "docker exec -i $(docker ps -qf "name=emg-pvs_registrar") python3 /core/registrar.py  --objects-prefix $product --service-url $SERVICE_URL --reporting-dir $REPORTING_DIR"
    docker exec  $(docker ps -qf "name=emg-pvs_registrar") python3 /core/registrar.py  --objects-prefix $product --service-url $SERVICE_URL --reporting-dir $REPORTING_DIR <<<$product
    
    IFS='/' read -ra ADDR <<< "$product"
    docker exec $(docker ps -qf "name=emg-pvs_registrar") bash -c 'ls /mnt/reports/. | grep $ADDR[4]' >> ${ADDR[4]}.csv


    while IFS="" read -r name
    do
    docker exec $(docker ps -qf "name=emg-pvs_registrar") cat /mnt/reports/$name > temp.xml
    tail -n +2 temp.xml > ${ADDR[4]}.xml
    done < ${ADDR[4]}.csv

    rm  ${ADDR[4]}.csv temp.xml

    
done < $product_list_file

pytest -s registrar_test.py --name $product_list_file

# Remove the created xml reports
while read product; do 
IFS='/' read -ra ADDR <<< "$product"
rm ${ADDR[4]}.xml
done < $product_list_file
