#!/bin/bash

# The script install ocStore.
# Should be executed from the project root folder.

echo "Setup ocStore..."

set -e

docker compose down -v && docker compose up -d web mysql --remove-orphans

bash ./scripts/wait_for_containers.sh
bash ./scripts/wait_for_mysql.sh

echo "Install composer dependencies..."
docker compose exec web bash -c "cd /web; composer update"

docker compose exec web bash -c '\
	cd /web/upload/install; \
	php cli_install.php install \
		--db_hostname mysql \
		--db_username root \
		--db_password secret \
		--db_database ocstore3 \
		--db_driver mysqli \
		--db_port 3306 \
		--username admin \
		--password admin \
		--email youremail@example.com \
		--http_server https://api.tg.nikitakiselev.ru/; exit $?'

docker compose exec web bash -c "cd /web; composer update"
echo "Moving storage folder outside the system."
mv src/upload/system/storage src/

docker compose exec web bash -c "\
  sed -i \"s/DIR_SYSTEM \. 'storage\/'/'\/web\/storage\/'/g\" /web/upload/config.php
  sed -i \"s/DIR_SYSTEM \. 'storage\/'/'\/web\/storage\/'/g\" /web/upload/admin/config.php"

rm -rf src/upload/install

echo "OcStore successfully installed."
