#!/bin/bash
	set -e
while true; do
  if psql -p ${r7_db_port} -U ${POSTGRES_USER} -d ${POSTGRES_DB} -c '\q' 2>/dev/null; then
    echo "База данных доступна!"
	rm -f /import_is_done
	for SQLFILE in /docker-entrypoint-initdb.d/db/cddisk/Alter/*/*.sql /docker-entrypoint-initdb.d/db/Updates/*/*.sql
	do
	    echo ""
	    echo "RUN $SQLFILE"
	    psql -U ${POSTGRES_USER} -q -f "$SQLFILE" \
            -v api_url="'$r7_API_URL'" \
            -v payments_url="'$r7_PAYMENTS_URL'" \
            -v draw_url="'$r7_DRAW_URL'" \
            -v site_url="'$r7_SITE_URL'" \
            -v document_url="'$r7_DS_FRONTEND_URL'" \
            -v domain="'$r7_SITE_DOMAIN'" \
            -v admin_url="'$r7_ADMIN_URL'" \
            -v mail_url="'$r7_CDMAIL_URL'" \
            -v contact_url="'$r7_CONTACT_URL'" \
            -v calendar_url="'$r7_CALENDAR_URL'" \
            -v projects_url="'$r7_PROJECTS_URL'" \
            -v pages_url="'$r7_PAGES_URL'" \
            -v forms_url="'$r7_FORMS_URL'" \
            -v core_machine_key="'$r7_CORE_MACHINE_KEY'" \
            -v api_url_internal="'$r7_API_INTERNAL_URL'" \
            -v jwt_key="'$r7_JWT_KEY'" \
            -v enable_projects="'$ENABLE_PROJECTS'" \
            -v team_rabbit_pass="'$r7_team_rabbit_pass'" \
            -v pages_client_id="'$r7_PAGES_CLIENT_ID'" \
            -v pages_secret="'$r7_PAGES_CLIENT_SECRET'" \
            -v forms_client_id="'$r7_FORMS_CLIENT_ID'" \
            -v forms_secret="'$r7_FORMS_CLIENT_SECRET'" \
            -v team_client_id="'$r7_TEAM_CLIENT_ID'" \
            -v team_secret="'$r7_TEAM_CLIENT_SECRET'" \

		echo ""
	done
	echo ""
	echo ""
	echo "#################################"
	echo "############## END ##############"
	echo "## Версия БД после обновления: ##"
	echo "#################################"
	echo ""
	echo ""

	psql -U ${POSTGRES_USER} -c 'SELECT * FROM public."Versions";'


	echo ""
	echo ""
	echo "################################"
	echo "### Обновление БД завершено ####"
	echo "################################"
	echo ""
	echo ""

	break  # Прерываем цикл, если доступна
  else
    echo "База данных недоступна. Проверяем снова через 5 секунд..."
    sleep 5  # Ждем 5 секунд перед повторной проверкой
  fi
done

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -c "CREATE DATABASE pagesdb;"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -c "GRANT ALL PRIVILEGES ON DATABASE pagesdb TO "$POSTGRES_USER";"

cd /docker-entrypoint-initdb.d/db/pages/Pages
for file in Tables/*.sql Init/*.sql
do
  psql -U ${POSTGRES_USER} -d pagesdb -f "${file}" \
            -v api_url="'$r7_API_URL'" \
            -v payments_url="'$r7_PAYMENTS_URL'" \
            -v draw_url="'$r7_DRAW_URL'" \
            -v site_url="'$r7_SITE_URL'" \
            -v document_url="'$r7_DS_FRONTEND_URL'" \
            -v domain="'$r7_SITE_DOMAIN'" \
            -v admin_url="'$r7_ADMIN_URL'" \
            -v mail_url="'$r7_CDMAIL_URL'" \
            -v contact_url="'$r7_CONTACT_URL'" \
            -v calendar_url="'$r7_CALENDAR_URL'" \
            -v projects_url="'$r7_PROJECTS_URL'" \
            -v pages_url="'$r7_PAGES_URL'" \
            -v forms_url="'$r7_FORMS_URL'" \
            -v core_machine_key="'$r7_CORE_MACHINE_KEY'" \
            -v api_url_internal="'$r7_API_INTERNAL_URL'" \
            -v jwt_key="'$r7_JWT_KEY'" \
            -v enable_projects="'$ENABLE_PROJECTS'" \
            -v team_rabbit_pass="'$r7_team_rabbit_pass'" \
            -v pages_client_id="'$r7_PAGES_CLIENT_ID'" \
            -v pages_secret="'$r7_PAGES_CLIENT_SECRET'" \
            -v forms_client_id="'$r7_FORMS_CLIENT_ID'" \
            -v forms_secret="'$r7_FORMS_CLIENT_SECRET'" \
            -v team_client_id="'$r7_TEAM_CLIENT_ID'" \
            -v team_secret="'$r7_TEAM_CLIENT_SECRET'" \

done

cd ${cwd}

touch ${PGDATA}/import_is_done

