#!/usr/bin/env bash
set -o errexit
if [ -f "../env/magento.env" ]; then
    source "../env/magento.env"
else
    echo "Warning: magento.env file not found."
fi

MEM_BYTES=$(docker info -f '{{.MemTotal}}')
MEM_MB=$(( MEM_BYTES / 1000000 ))
# When Docker Desktop is set to 6GB in the GUI, it is reported as 6227 MB
(( MEM_MB < 6227 )) && echo "There must be at least 6GB of RAM allocated to Docker in order to continue." && exit

DOMAIN=${1:-magento.test}

bin/stop

bin/start --no-dev
[ $? != 0 ] && echo "Failed to start Docker services" && exit

bin/clinotty chmod u+x bin/magento
rm -rf src && mkdir src

echo "Adding Magento modules to Composer allow-plugins directive..."
bin/clinotty composer config --no-plugins allow-plugins.magento/magento-composer-installer true
bin/clinotty composer config --no-plugins allow-plugins.magento/inventory-composer-installer true
bin/clinotty composer config --no-plugins allow-plugins.laminas/laminas-dependency-plugin true

echo "Running, Magento setup:install..."
bin/setup-install "${DOMAIN}"

echo "Copying files from container to host after install..."
bin/copyfromcontainer --all

echo "Forcing deploy of static content to speed up initial requests..."
bin/clinotty bin/magento setup:static-content:deploy -f

echo "Re-indexing with Elasticsearch..."
bin/clinotty bin/magento indexer:reindex

echo "Setting basic URL and generating SSL certificate..."
bin/setup-domain "${DOMAIN}"

echo "Fixing owner and permissions..."
bin/fixowns
bin/fixperms

echo "Clearing the cache to apply updates..."
bin/clinotty bin/magento cache:flush

echo "Installing cron, run 'bin/cron start' to enable..."
bin/clinotty bin/magento cron:install

echo "Turning on developer mode..."
bin/clinotty bin/magento deploy:mode:set developer

cp -r .vscode src/

echo "Docker development environment setup complete."
echo "You may now access your Magento instance at https://${DOMAIN}/"

echo "You may now access your Magento backend instance at https://${DOMAIN}/admin/"

echo "Use the following default credentials to log in:"
echo "Username: $MAGENTO_ADMIN_USER"
echo "Password: $MAGENTO_ADMIN_PASSWORD"