#!/bin/bash

#==============全局变量定义 start=======================
#运行模块名
MODULE_NAME="kws"
#定义环境变量
PROJECT_PATH=$GOPATH/src/smecloud/kws

MAIN_FILE="$PROJECT_PATH/app/main.go"
RUNTIME="$PROJECT_PATH/runtime"
BUILT_FILE="$PROJECT_PATH/app/${MODULE_NAME}"
LOG_FILE="$RUNTIME/run.log"
#保存pid的临时文件,可按情况进行修改，比如放到/run/passport.pid
PID_FILE="$RUNTIME/${MODULE_NAME}.pid"
BUILD_COMMAMD="go build -o ${BUILT_FILE} ${MAIN_FILE}"
RUN_COMMAMD="nohup "${BUILT_FILE}" > "${LOG_FILE}" 2>&1 &"

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

#显示使用方法
usage(){
    echo ""
	echo "usage:"
    echo ""
   	echo $0" build"
    echo "       Build project"
    echo ""
    echo $0" start"
    echo "       run go project"
    echo ""
    echo $0" stop"
    echo "       stop run go project"
    echo ""
    echo $0" restart"
    echo "       stop and start go project"
    echo ""
}

#编译程序
build(){
	echo "start build $MODULE_NAME"
	#echo $BUILD_COMMAMD

	eval $BUILD_COMMAMD

	echo "build $MODULE_NAME OK"
	return 0
}

stop(){
	echo "stop $MODULE_NAME"
	if [ ! -e $PID_FILE ]; then
		echo "no pid file: $PID_FILE, $MODULE_NAME may not run. "
		return 0 #继续向下执行
	fi

    #杀死运行的进程
	pid=`cat $PID_FILE`
	if [ "$pid" = "" ]; then
		echo "kill $MODULE_NAME fail,pid=$pid"
		return 1
	fi
	
	res=`ps aux|awk -v pid=$pid '{if($2 == pid) {print $2}}'`
	if [ "$pid" = "$res" ] ;then
		kill -9 $pid
		if [ $? -ne 0 ] ;then
			echo "kill $MODULE_NAME fail,pid=$pid"
			return 1
		fi
	else
		echo "$MODULE_NAME not run, no pid=$pid"
		return 0
	fi
	
	rm $PID_FILE
	echo "$MODULE_NAME stop success"
	return 0
}

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
    eval $RUN_COMMAMD

    if [ $? -ne 0 ] ;then            
        return 1
    fi

    pid=$!
	
	if [ "$pid" = "" ]; then
	    echo "start fail"
		return 1
	fi
	
	#休息1S再检查进程是否启动，因为如果端口冲突进程是会停止的
	sleep 1
	
	res=`ps aux|awk -v pid=$pid '{if($2 == pid) {print $2}}'`
	if [ "$pid" = "$res" ] ;then
		echo $pid > "$PID_FILE"
		echo "start $MODULE_NAME OK. PID:$pid"
		return 0
	else
		#进行运行后停止了，因为端口冲突
		echo "start fail. $MODULE_NAME had start but stop later, maybe port has been used"
		return 1
	fi
}

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


#==========================================
#执行命令
#==========================================
result=0
if [ "$1" == "build" ]; then
	create_runtime
	build
	result=$?
elif [ "$1" == "start" ]; then
	create_runtime
	start
	result=$?
elif [ "$1" == "stop" ]; then
	stop
	result=$?
elif [ "$1" == "restart" ]; then
	stop
	result=$?
	if [ $result -ne 0 ]; then
		exit 1
	fi
	start
	result=$?
else
	usage
fi

exit $result
