#!/bin/bash


https_config(){
   if [ "${SSL}" = "true" ];then
       ssh root@${IP_LIST[0]} "sed -i /nginx/,/proxy/s/^#//g ${DOCKER_COMPOSE_DIR}/${DOCKER_COMPOSE_FILE}"
       ssh root@${IP_LIST[0]} "sed -i /HTTP_PROTOCOL_TYPE/http/https/g ${DOCKER_COMPOSE_DIR}/${DOCKER_COMPOSE_FILE}"
       if [ -z "${DOMAIN_NAME}" ];then
            echo "ERROR: please configure DOMAIN_NAME"
            exit 1
       else
           ssh root@${IP_LIST[0]} "sed -i s/ab.edoc2.com/${DOMAIN_NAME}/g ${DOCKER_COMPOSE_DIR}/${DOCKER_COMPOSE_FILE}"
       fi

   fi
}

node_port_check(){
    local NODE_PORT_STATUS=0
    for ip in ${IP_LIST[@]}
    do
        if [ -n "${NODE_PORT}" ];then
            ssh root@${ip} ss -ntl |grep ":${NODE_PORT}[[:space:]]" &> /dev/null && NODE_PORT_STATUS=1
        else
            NODE_PORT=80
            ssh root@${ip} ss -ntl |grep ":${NODE_PORT}[[:space:]]" &> /dev/null && NODE_PORT_STATUS=1
        fi
        if [ "${NODE_PORT_STATUS}" == 1 ];then
            echo "${NODE_PORT} port has been use on ${ip}"
            exit 1
        fi
    done
}

node_port_config(){
     if [ ! -z "${NODE_PORT}" ];then
       ssh root@${IP_LIST[0]} "sed -i /published/s/80/${NODE_PORT}/g ${DOCKER_COMPOSE_DIR}/${DOCKER_COMPOSE_FILE}"
   fi

}

production_config(){
    case ${PRODUCTION} in
        indrive)
            echo "deploy InDrive"
            ssh root@${IP_LIST[0]} "sed -i s/PRODUCTION=edoc2/PRODUCTION=${PRODUCTION}/g ${DOCKER_COMPOSE_DIR}/${DOCKER_COMPOSE_FILE}"
            ssh root@${IP_LIST[0]} "sed -i s/WF=true/WF=false/g ${DOCKER_COMPOSE_DIR}/${DOCKER_COMPOSE_FILE}"
            ssh root@${IP_LIST[0]} "sed -i /wf:/,/max-file:/s/^/#/g ${DOCKER_COMPOSE_DIR}/${DOCKER_COMPOSE_FILE}"
            ;;
        edoc2)
            echo "deploy ecm !!!"
            ;;
         *)
            echo "ERROR: please config production error !!"
            ;;
    esac

}


cluster_config(){
    ssh root@${IP_LIST[0]} "sed -i /CACHEMODULE=redis/s/^#//g ${DOCKER_COMPOSE_DIR}/${ENVFILE}"
}

middleware_config(){
     if [ "${MIDDLEWARE_MODE}" = "standalone" ] || [ "${MIDDLEWARE_MODE}" = "cluster" ];then
         scp Template/middleware-master-${MIDDLEWARE_MODE}.yml root@${IP_LIST[0]}:${DOCKER_COMPOSE_DIR}/
         if [ "${MIDDLEWARE_DATA_PATH}" != '/home/edoc2/macrowing/edoc2v5/data' ];then
             ssh root@${IP_LIST[0]} "sed -i s,/home/edoc2/macrowing/edoc2v5/data,${MIDDLEWARE_DATA_PATH},g ${DOCKER_COMPOSE_DIR}/middleware-master-${MIDDLEWARE_MODE}.yml"
         fi
     else
         echo "middleware mode configured not supported !!"
         exit 2
     fi
}

docker_compose_config(){
    scp Template/docker-compose*.yml root@${IP_LIST[0]}:${DOCKER_COMPOSE_DIR}/${DOCKER_COMPOSE_FILE}
    scp Template/envfile.env root@${IP_LIST[0]}:${DOCKER_COMPOSE_DIR}/${ENVFILE}
    if [ ${#IP_LIST[@]} -gt 1 ];then
        cluster_config
    fi
    data_path_create
    data_path_config
    middleware_data_path_create
    middleware_config
    storage_config
    production_config
    node_port_config
    https_config
}
