#!/bin/bash

DEPLOY_CONFIG_FILE=deploy-config/ecm.conf
IP=$(awk -F '=' /HOST=/'{print $2}' ${DEPLOY_CONFIG_FILE})
DATA_PATH=$(awk -F '=' '/STORAGE_PATH=/{print $2}' ${DEPLOY_CONFIG_FILE})
NODE_PORT=$(awk -F '=' '/SERVICE_PORT=/{print $2}' ${DEPLOY_CONFIG_FILE})
DEPLOY_MODE=$(awk -F '=' '/DEPLOY_MODE=/{print $2}' ${DEPLOY_CONFIG_FILE})
PASSWORD=$(awk -F '=' '/PASSWORD=/{print $2}' ${DEPLOY_CONFIG_FILE})
STORAGE_MODE=$(awk -F '=' '/STORAGE_MODE=/{print $2}' ${DEPLOY_CONFIG_FILE})
PRODUCTION=$(awk -F '=' '/PRODUCTION=/{print $2}' ${DEPLOY_CONFIG_FILE})
S3SECRET=$(awk -F '=' '/S3SECRET=/{print $2}' ${DEPLOY_CONFIG_FILE})
S3ACCESS=$(awk -F '=' '/S3ACCESS=/{print $2}' ${DEPLOY_CONFIG_FILE})
S3URL=$(awk -F '=' '/S3URL=/{print $2}' ${DEPLOY_CONFIG_FILE})
S3BUCKET=$(awk -F '=' '/S3BUCKET=/{print $2}' ${DEPLOY_CONFIG_FILE})
SSL=$(awk -F '=' '/SSL=/{print $2}' ${DEPLOY_CONFIG_FILE})
DOMAIN_NAME=$(awk -F '=' '/DOMAIN_NAME=/{print $2}' ${DEPLOY_CONFIG_FILE})
SWIFTUSERNAME=$(awk -F '=' '/SWIFTUSERNAME=/{print $2}' ${DEPLOY_CONFIG_FILE})
S3HealthUrl=$(awk -F '=' '/S3HealthUrl=/{print $2}' ${DEPLOY_CONFIG_FILE})
CONTAINERID=$(awk -F '=' '/CONTAINERID=/{print $2}' ${DEPLOY_CONFIG_FILE})
MIDDLEWARE_MODE=$(awk -F '=' '/MIDDLEWARE_MODE=/{print $2}' ${DEPLOY_CONFIG_FILE})
MIDDLEWARE_DATA_PATH=$(awk -F '=' '/MIDDLEWARE_DATA_PATH=/{print $2}' ${DEPLOY_CONFIG_FILE})
MIDDLEWARE_STANDALONE_HA=$(awk -F '=' '/MIDDLEWARE_STANDALONE_HA=/{print $2}' ${DEPLOY_CONFIG_FILE})
DOCKER0_NETWORK=$(awk -F '=' '/DOCKER0_NETWORK=/{print $2}' ${DEPLOY_CONFIG_FILE})
DOCKER_GWBRIDGE_NETWORK=$(awk -F '=' '/DOCKER_GWBRIDGE_NETWORK=/{print $2}' ${DEPLOY_CONFIG_FILE})


DOCKER_COMPOSE_DIR='/opt'
DOCKER_COMPOSE_FILE=docker-compose.yml
ENVFILE=envfile.env
SSH_OPT='-o StrictHostKeyChecking=no'
IP_LIST=($(echo ${IP}| tr ',' ' '))


source scripts/prepare.sh
source scripts/env_config.sh
source scripts/ssh_key_config.sh
source scripts/docker_install.sh
source scripts/datapath_config.sh
source scripts/storage_config.sh
source scripts/deploy_mode.sh
source scripts/compose_config.sh
source scripts/portainer_install.sh
	

main(){
    ssh_key_config
    prepare
    system_env_config
    docker_install
    docker_compose_config
    deploy_mode
    portainer_install
    echo "The docker-compose file is stored in the ${IP_LIST[0]}  ${DOCKER_COMPOSE_DIR} directory"
    echo "edoc2 deploy success"
    echo "Please access your application via http://${IP_LIST[0]}:${NODE_PORT}"
}

main
