#!/bin/bash

# 
# 
# 

AGS_USER=argus
INSTALL_DIR=/opt
AGS_HOME=${INSTALL_DIR}/argus
AGS_PACK=argus-3.1.tar.gz

csudo=""
if command -v sudo > /dev/null; then
    csudo="sudo"
fi


function InitSystem() {
    ## 修改主机名
#    if ! ${csudo} hostnamectl set-hostname argus-server; then
#        echo "主机名修改失败"
#        exit
#    fi

    ## 修改时区
    if ! ${csudo} ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime; then
        echo "时区修改失败"
        exit 0
    fi

    ## 内核优化
    if ${csudo} sh -c 'echo "net.ipv4.tcp_syncookies = 1" >> /etc/sysctl.conf';then
    	${csudo} sysctl -p &> /dev/null
    fi 

    ## 修改最大文件数
    ${csudo} sh -c 'echo -e "* soft nofile 65535\n* hard nofile 65535\n" >> /etc/security/limits.conf'
}

function prepare_instarll() {
	## 创建运行用户
	if ! ${csudo} id argus &> /dev/null;then
		${csudo} useradd argus
	fi
	## 部署sudo权限
	${csudo} sh -c 'echo "argus   ALL=(ALL)       NOPASSWD: ALL" >> /etc/sudoers'
	## 部署JDK
	if [ -e ./corretto-8.tar.gz ];then
		${csudo} tar -xf corretto-8.tar.gz -C /usr/local
		${csudo} tee /etc/profile.d/jdk.sh <<EOL 1>/dev/null
export JAVA_HOME=/usr/local/corretto-8
export PATH=\$PATH:\$JAVA_HOME/bin
EOL
	else
		echo "没有找到corretto-8.tar.gz"
		exit
	fi
	## 安装字体
	## 安装tftp
}

function install_argus() {
	[ ! -e ${AGS_PACK} ] && echo "没有找到argus-3.1.tar.gz" && exit 1

	${csudo} tar -xf ${AGS_PACK} -C ${INSTALL_DIR} &> /dev/null

	${csudo} chown ${AGS_USER}:${AGS_USER} $AGS_HOME -R

	${csudo} sed -i "s#/opt/argus#$INSTALL_DIR/argus#g" ${AGS_HOME}/support-files/*

	${csudo} cp -ar ${AGS_HOME}/support-files/*.service /lib/systemd/system/

	${csudo} cp -ar ${AGS_HOME}/support-files/pgsql /etc/init.d/pgsql

	${csudo} systemctl daemon-reload
	
	${csudo} cp -ar ./argus /usr/bin/argus

	${csudo} sed -i '/exit/i\/usr/bin/argus start all' /etc/rc.local

}


function kill9() {
    status=`ps -ef | grep $1 | grep -v grep | awk '{print $2}' | wc -l`

    if [ $status -ne 0 ]
        then
            for i in `ps -ef | grep $1 | grep -v grep | awk '{print $2}'`
            do
                     ${csudo} kill -9 $i
            done
    fi                                
}

function checkprocess() {
	flag=0
	status=`ps -ef | grep $1 | grep -v grep | awk '{print $2}' | wc -l`
	if [ $status -ne 0 ]; then
		flag=$status
	fi
}

function stop_argus_app() {
	${csudo} systemctl stop argus-app
	sleep 1
	kill9 argus-admin-app
}

function start_argus_app() {
	checkprocess argus-admin-app
	if [ $flag -eq 0 ]; then
		${csudo} systemctl start argus-app
	else
		stop_argus_app
		${csudo} systemctl start argus-app
	fi
}

function stop_argus_core() {
	${csudo} systemctl stop argus-core
	sleep 1
	kill9 argus_server
}

function start_argus_core() {
	checkprocess argus_server
	if [ $flag -eq 0 ]; then
		${csudo} systemctl start argus-core
	else
		stop_argus_core
		${csudo} systemctl start argus-core
	fi
}

function stop_argus_agent() {
	${csudo} systemctl stop argus-agent
	sleep 1
	kill9 argus_agent
}

function start_argus_agent() {
	checkprocess argus_agent
	if [ $flag -eq 0 ]; then
		${csudo} systemctl start argus-agent
	else
		start_argus_agent
		${csudo} systemctl start argus-agent
	fi
}


function stop_docker() {
	${csudo} systemctl stop docker
	sleep 1
	kill9 docker
}

function start_docker() {
	checkprocess docker
	if [ $flag -eq 0 ]; then
		${csudo} systemctl start docker
	else
		stop_docker
		${csudo} systemctl start docker
	fi
}

function stop_argus_java() {
	${csudo} systemctl stop argus-java
	sleep 1
	kill9 java-agent
}

function start_argus_java() {
	checkprocess java-agent
	if [ $flag -eq 0 ]; then
		${csudo} systemctl start argus-java
	else
		stop_argus_java
		${csudo} systemctl start argus-java
	fi
}

function stop_nginx() {
	${csudo} systemctl stop nginx
	sleep 1
	kill9 nginx
}

function start_nginx() {
	checkprocess nginx
	if [ $flag -eq 0 ]; then
		${csudo} systemctl start nginx
	else
		stop_nginx
		${csudo} systemctl start nginx
	fi
}

function stop_pgsql() {
	${csudo} service pgsql stop
	sleep 1
	kill9 postmaster
}

function start_pgsql() {
	checkprocess postmaster
	if [ $flag -eq 0 ]; then
		${csudo} service pgsql start
	else
		stop_pgsql
		${csudo} service pgsql start
	fi
}

function clear_argus() {

	argus stop all

	[ -d ${AGS_HOME} ] && ${csudo} rm -rf ${AGS_HOME}

	${csudo} rm -rf /lib/systemd/system/argus-*.service
	${csudo} rm -rf /lib/systemd/system/nginx.service
	${csudo} rm -rf /lib/systemd/system/docker.service
	${csudo} rm -rf /etc/init.d/pgsql
}



case $1 in
	"start")
		case $2 in
			"all")
				start_pgsql
				sleep 2
				start_argus_core
				start_argus_agent
				start_argus_java
				start_docker
				start_argus_app
				start_nginx
			;;
			"*")
				echo "Not found service...."
			;;
		esac
	;;

	"stop")
		case $2 in 
			"all")
				stop_argus_app
				stop_docker
				stop_argus_core
				stop_argus_agent
				stop_argus_java
				stop_nginx
				stop_pgsql
			;;
			"*")
				echo "Not found service...."
			;;
		esac	
	;;

	"install")
		InitSystem
		prepare_instarll
		install_argus
		argus start all
		echo "Argus Install Success"
	;;

	"clear")

		clear_argus
	;;

	"*")
		echo "Use start or stop service"
	;;
esac
