#!/bin/bash

echo "Installing curl and wget"
apt update
apt install -qqy curl wget


echo "Making work directory"
mkdir -p /srv/platform/images


echo "Downloading docker images"
cd /srv/platform/images
wget -q --show-progress -O platform-gateway.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-gateway.tar.gz
wget -q --show-progress -O platform-discovery.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-discovery.tar.gz
wget -q --show-progress -O platform-manager.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-manager.tar.gz
wget -q --show-progress -O platform-frontend.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-frontend.tar.gz
wget -q --show-progress -O platform-service-renderer.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-service-renderer.tar.gz
wget -q --show-progress -O platform-service-notifications.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-service-notifications.tar.gz
wget -q --show-progress -O platform-service-logstash.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-service-logstash.tar.gz
wget -q --show-progress -O platform-service-storage.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-service-storage.tar.gz
wget -q --show-progress -O platform-service-data-transformer.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-service-data-transformer.tar.gz
wget -q --show-progress -O platform-plugin-clickhouse.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-plugin-clickhouse.tar.gz
wget -q --show-progress -O platform-plugin-csv.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-plugin-csv.tar.gz
wget -q --show-progress -O platform-plugin-json.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-plugin-json.tar.gz
wget -q --show-progress -O platform-plugin-mssql.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-plugin-mssql.tar.gz
wget -q --show-progress -O platform-plugin-mysql.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-plugin-mysql.tar.gz
wget -q --show-progress -O platform-plugin-oracle.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-plugin-oracle.tar.gz
wget -q --show-progress -O platform-plugin-polymatica-analytics.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-plugin-polymatica-analytics.tar.gz
wget -q --show-progress -O platform-plugin-postgres.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-plugin-postgres.tar.gz
wget -q --show-progress -O platform-plugin-r7.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-plugin-r7.tar.gz
wget -q --show-progress -O platform-plugin-vertica.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-plugin-vertica.tar.gz
wget -q --show-progress -O platform-plugin-xlsx.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-plugin-xlsx.tar.gz
wget -q --show-progress -O platform-plugin-xml.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-plugin-xml.tar.gz
wget -q --show-progress -O platform-plugin-ydb.tar.gz https://repo.polymatica.ru/platform/docker/images/platform-plugin-ydb.tar.gz
wget -q --show-progress -O dtm.tar.gz https://repo.polymatica.ru/platform/docker/images/dtm.tar.gz


echo "Loading docker images"
for IMG in ./*.tar.gz; do docker load -i $IMG; done


echo "Deleting unnecessary docker images"
rm -f ./*.tar.gz || echo "Deleting unnecessary docker images not completed"


echo "Downloading configuration files"
cd /srv/platform
wget -q --show-progress -O docker-compose.yml https://repo.polymatica.ru/platform/docker/configs/docker-compose.yml
wget -q --show-progress -O traefik-docker-provider.yml https://repo.polymatica.ru/platform/docker/configs/traefik-docker-provider.yml
wget -q --show-progress -O psql_init.sh https://repo.polymatica.ru/platform/docker/configs/psql_init.sh


echo "Starting docker services"
docker compose up -d --remove-orphans || docker-compose up -d --remove-orphans
