#!/bin/bash

LOG_PROM_FILE="/alertmanager/alert.log"
RUN_ALERTMANAGER="/bin/alertmanager --config.file=/alertmanager/alertmanager.yml $@" 

init(){
	#运行环境
	runmode=$RUNMODE

	if [ "$runmode"x = ""x ]; then
	    runmode="prod"
	fi
	echo "runmode="$runmode

  if [ ! -f "/alertmanager/alertmanager.yml" ];then
      cp /etc/alertmanager/alertmanager.yml  /alertmanager
 	fi
}

#==============修改运行环境 start===================
start(){
  echo $RUN_ALERTMANAGER
	eval $RUN_ALERTMANAGER
    if [ $? -ne 0 ] ;then              
        return 1
    fi
}

check_private(){
    if [ "$runmode"x = "private"x ] || [ "$runmode"x = "paas"x ]; then
        sed "s,http://cloud.kingdee.com/kws/openapi/alert/notice,http://kws-app:9645/openapi/alert/notice,g" -i /alertmanager/alertmanager.yml
    fi

    if [ "$runmode"x = "test"x ] ; then
        sed "s,http://cloud.kingdee.com/kws/openapi/alert/notice,http://kws-test-app-9645-300037.kcssz.cloud.kingdee.com/openapi/alert/notice,g" -i /alertmanager/alertmanager.yml
    fi
}

#==========================================
#执行命令
#==========================================
init
check_private

#keep running
echo "DEBUG="$DEBUG
if [ "$DEBUG"x = "1"x ]; then
    exec "/bin/bash"
else
	start
fi
