#!/bin/bash

source xt/env;

# Wait until composer initialized
while [[ "$variable" != *"/var/www/html/var/composer_home: directory"* ]];do
    variable=$(docker exec -i -t perl6magento_web_1 /bin/bash -c "file /var/www/html/var/composer_home")
    sleep 0.1 
done 

# Wait until MySQL service is up
while [[ "$variable" != *"succeeded"* ]];do
    variable=$(docker exec -i -t perl6magento_web_1 /bin/bash -c "nc -zv db 3306")
    sleep 0.1 
done 

# Setup Magento
docker exec -it -u 0 -e SCRIPT="$(cat scripts/install-magento)" perl6magento_web_1 /bin/bash -c " 
    echo \"\$SCRIPT\" > install-magento; 
    chmod +x install-magento; 
    ./install-magento;";

# Magento sample data
docker exec -it -e SCRIPT="$(cat scripts/install-sampledata)" perl6magento_web_1 /bin/bash -c " 
    echo \"\$SCRIPT\" > install-sampledata; 
    chmod +x install-sampledata; 
    ./install-sampledata;";

# Add test user
docker exec -it -e CUSTOMERSQL="$(cat xt/sql/customer-entity.sql)" perl6magento_db_1 /bin/bash -c "
    echo \"\$CUSTOMERSQL\" > /tmp/customer.sql;
    mysql -u root -p${MYSQL_ROOT_PASSWORD} -h db magento  < /tmp/customer.sql";
