#!/bin/bash

edoc2_dir_create(){
    ssh root@${IP_LIST[0]} "mkdir -p ${DATA_PATH}/{edoc2Docs,edrmscore}"
}

every_edoc2_dir_create(){
    for ip in ${IP_LIST[@]}
    do
        ssh root@${ip} "mkdir -p ${DATA_PATH}/edoc2Docs"
    done
}

middleware_dir_create(){
    ssh root@${IP_LIST[0]} "mkdir -p ${MIDDLEWARE_DATA_PATH}/{mysql,redis,es,rabbitmq}"
    ssh root@${IP_LIST[0]} "mkdir -p ${DBBACKUP_PATH}/dbbackup"
}

middleware_cluster_dir_create(){
   for ((i=0;i<=2;i++)) 
   do
        ssh root@${IP_LIST[i]} "mkdir -p ${MIDDLEWARE_DATA_PATH}/{mysql,redis,es,rabbitmq}"
        ssh root@${IP_LIST[i]} "mkdir -p ${DBBACKUP_PATH}/dbbackup"
   done

}

data_path_create(){
    case ${STORAGE_MODE} in
         local)
             if [ ${#IP_LIST[@]} -ne 1 ];then
                  echo "STORAGE_MODE configure error !!"
                  exit 3
             fi    
             edoc2_dir_create
             ;;
         nas)
             if [ ${#IP_LIST[@]} -eq 1 ];then
                  edoc2_dir_create
             else            
                  check_share_storage ${DATA_PATH}
                  edoc2_dir_create
             fi
             ;;     
          s3|swift|oss|ceph)
             every_edoc2_dir_create
           ;;
          *)
              echo "storage mode configure not support!!"
              exit 7
             ;;
    esac
    
}

check_share_storage(){
    if [ -z "${1}" ];then
        echo "ERROR: please configuration "
        exit 1
    fi
    for ip in ${IP_LIST[@]}
    do
        ssh root@${ip} mount |grep ${1} &> /dev/null || SHARE_STORAGE_STATUS=1
        if [ "${SHARE_STORAGE_STATUS}" = "1" ];then   
		    echo "ERROR: ${ip} please mount share storage ${1}!"
            exit 1
        fi
    done
}

data_path_config(){
   if [ -n "${DATA_PATH}" ] && [ "${DATA_PATH}" != '/home/edoc2/macrowing/edoc2v5/data' ];then
       ssh root@${IP_LIST[0]} "sed -i s,/home/edoc2/macrowing/edoc2v5/data,${DATA_PATH},g ${DOCKER_COMPOSE_DIR}/${DOCKER_COMPOSE_FILE}" 
   fi

}

middleware_data_path_create(){
    case ${MIDDLEWARE_MODE} in
        standalone)
           middleware_dir_create
           ;;
        cluster)
           if [ "${STORAGE_MODE}" = "nas" -a "${DATA_PATH}" = "${MIDDLEWARE_DATA_PATH}" ];then
               echo "MIDDLEWARE_DATA_PATH configure error"
               exit 3
           fi
           middleware_cluster_dir_create
           ;;
    esac 
}
