#!/bin/bash

#==============全局变量定义 start=======================
#运行模块名
MODULE_NAME="kws-agent"
#定义环境变量
export GOPATH=/opt/gowww/kws-agent

MAIN_FILE="$GOPATH/src/agent/main.go"
RUNTIME="$GOPATH/runtime"
BUILT_FILE="$GOPATH/src/agent/${MODULE_NAME}"
LOG_FILE="$RUNTIME/run.log"
LOG_PROM_FILE="/prometheus/prom.log"
#保存pid的临时文件,可按情况进行修改，比如放到/run/passport.pid
PID_FILE="$RUNTIME/${MODULE_NAME}.pid"
RUN_COMMAMD=${BUILT_FILE}

retention=$RETENTION
if [ "$retention"x = ""x ]; then 
	retention="31d"
fi

retentSize=$RETENTSIZE

if [ "$retentSize"x = ""x ]; then 
	retentSize="5GB"
fi

RUN_PROMETHEUS="nohup /bin/prometheus --config.file=/prometheus/prometheus.yml \
--storage.tsdb.retention.time=${retention} \
--storage.tsdb.path=/prometheus \
--web.console.libraries=/etc/prometheus/console_libraries \
--web.console.templates=/etc/prometheus/consoles \
--query.max-samples=5000000 \
--query.max-concurrency=20 \
--query.timeout=2m \
--storage.tsdb.retention.size=${retentSize} \
--storage.tsdb.min-block-duration=1h \
--web.enable-lifecycle > "${LOG_PROM_FILE}" 2>&1 &" 


#==============全局变量定义 end=========================

#==============修改运行环境 start===================

init(){
	#运行环境
	runmode=$RUNMODE
	#http端口
	httpport=$HTTPPORT

	promid=$PROMID
	# prom节点编号
    promsn=$PROMSN
	
	if [ "$runmode"x = ""x ]; then 
	    runmode="prod"
	fi
	echo "runmode="$runmode
	sed -i "s/^runmode.*/runmode=$runmode/g"  $GOPATH/src/agent/conf/app.conf
	
	if [ "$httpport"x != ""x ]; then
		echo "httpport="$httpport
	    sed -i "s/^httpport.*/httpport=$httpport/g"  $GOPATH/src/agent/conf/app.conf
	fi
	
#	if [ "$promid"x != ""x ]; then
#		echo "prome_id="$promid
#	    sed -i "s/^prome_id.*/prome_id=$promid/g"  $GOPATH/src/agent/conf/app.conf
#	fi

	if [ "$promsn"x != ""x ]; then
    	echo "promsn="$promsn
    	sed -i "2iprome_sn=$promsn"  $GOPATH/src/agent/conf/app.conf
    fi
}

#==============修改运行环境 end======================
start(){
	echo "start $MODULE_NAME"

	if [ ! -e $BUILT_FILE ]; then
		echo "no built file: ${BUILT_FILE}. start fail"
		return 1
	fi

	if [ ! -e $LOG_FILE ]; then
		touch $LOG_FILE
	fi
	
	#检查PID文件是否存在
	if [ -e $PID_FILE ]
	then
		#检查PID是否正在运行
		pid=`cat $PID_FILE`
		res=`ps aux|awk -v pid=$pid '{if($2 == pid) {print $2}}'`
		if [ "$pid" = "$res" ] ;then
			echo "has pid file: $PID_FILE,pid=$pid, $MODULE_NAME already run."
			return 0
	    fi
	fi
	#echo $RUN_COMMAMD
	if [ ! -f "/prometheus/prometheus.yml" ];then
		 mv /etc/prometheus/prometheus.yml  /prometheus
 	fi

	if [ -f "/prometheus/lock" ];then
		 rm /prometheus/lock
 	fi
	mv /etc/prometheus/ca_file  /prometheus
	echo $RUN_PROMETHEUS
	eval $RUN_PROMETHEUS
    eval $RUN_COMMAMD
    if [ $? -ne 0 ] ;then            
        return 1
    fi
}

#创建运行目录 
create_runtime(){
	if [ ! -e $RUNTIME ]; then
		mkdir -p $RUNTIME
	fi
}


#==========================================
#执行命令
#==========================================
create_runtime
init

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