#!/bin/bash
if [ "${WEBOFFICE}" == "true" ];then
while true
do

    WEBOFFICE_SERVER='/etc/nginx/conf.d/weboffice_server.conf'

    echo "upstream weboffice_ip {" > ${WEBOFFICE_SERVER}
 #   echo "ip_hash;" >> ${WEBOFFICE_SERVER}
    TS_IP_LIST=$(/usr/bin/dig weboffice +short +search|/usr/bin/sort)
    if [ -n "${TS_IP_LIST}" ];then
    	for IP in ${TS_IP_LIST}
    	do 
       	    echo "server ${IP};" >> ${WEBOFFICE_SERVER}
    	done
    else
        echo "server weboffice;" >> ${WEBOFFICE_SERVER} 
    fi
    echo "}" >> ${WEBOFFICE_SERVER}

    if [ ! -f '/etc/nginx/weboffice_server.md5' ];then
        /usr/bin/md5sum ${WEBOFFICE_SERVER}|cut -f1 -d ' ' > /etc/nginx/weboffice_server.md5
    else
        if [ "$(/usr/bin/md5sum ${WEBOFFICE_SERVER}|cut -f1 -d ' ')" != "$(cat /etc/nginx/weboffice_server.md5)" ];then   
             /usr/bin/md5sum ${WEBOFFICE_SERVER}|cut -f1 -d ' ' > /etc/nginx/weboffice_server.md5
            /usr/sbin/nginx -t
            if [ $? = 0 ];then
                /usr/sbin/nginx -s reload
                echo "$(date '+%Y/%m/%d %H:%M:%s') edoc2 config change,reload nginx" >> /tmp/nginx_reload.log
             else
                echo "$(date '+%Y/%m/%d %H:%M:%S') change md5 reload nginx" > /etc/nginx/weboffice_server.md5
             fi
        fi
    fi
    sleep 5
done
fi





