#!/bin/bash

prepare(){
     s3_config_check
     swift_config_check
     other_config_check
     node_port_check
}

string_check(){
    if [ -z "$1" ];then 
       echo "ERROR: please config $1"
       exit 1
    fi
}

other_config_check(){
   string_check ${IP}
   string_check ${PASSWORD}
   string_check ${STORAGE_MODE}
}

s3_config_check(){
    if [ "${STORAGE_MODE}" == "s3" ];then
        string_check ${S3SECRET}
        string_check ${S3ACCESS}
        string_check ${S3URL}
        string_check ${S3BUCKET}
    fi
}

swift_config_check(){
    if [ "${STORAGE_MODE}" = "swift" ];then
        string_check ${S3URL}
        string_check ${S3SECRET}
        string_check ${SWIFTUSERNAME}
        string_check ${CONTAINERID}
    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
}

