##!/bin/bash

# @file:    set_process.sh
# @brief:   系统应用添加进程脚本
# @version: 1.0
# @date:    2024/12/31 10:22:43
# @author:  lunankun

#前置all的重置或者新增都是由稳态的第一个进程来处理，所有进程收到这条消息后先判断自己的进程号是否是1，而且是稳态，否则不处理，所有操作均由这个进程完成，

# 设置日志文件路径
LOGFILE="$FEP_ENV/dat/log/start_fe.log"

# 输出当前时间并打印进程停止信息
echo "" ; echo "" 
echo "****** `date "+%F %R:%S"` start setting Processes after 3 sec ******"
echo "" >>"$LOGFILE"
echo "" >>"$LOGFILE"
echo "****** `date "+%F %R:%S"` start setting Processes after 3 sec ******" >>"$LOGFILE"

# 函数检查并处理日志文件大小
check_log_file() {
    if [ -n "$1" ]; then
        FILE_SIZE=0
        FILE_SIZE=$(du "$1" | awk '{print $1}')
        
        if [ $FILE_SIZE -ge 5120 ]; then
            if [ -f "$1".3 ]; then
                rm -f "$1".3
            fi
            if [ -f "$1".2 ]; then
                mv "$1".2 "$1".3
            fi
            if [ -f "$1".1 ]; then
                mv "$1".1 "$1".2
            fi
            mv "$1" "$1".1
        fi
    fi
}


# 调用检查日志文件大小的函数
check_log_file $LOGFILE

# 定义查找并杀死进程的函数
kill_process_by_name() {
    PROCESS_NAME=$1
    PID=$(ps -ef | grep "$PROCESS_NAME" | grep -v "grep" | awk '{print $2}')

    if [ -n "$PID" ]; then
        echo "Found process '$PROCESS_NAME' with PID: $PID"
        echo "Killing process..."
        kill -9 $PID
        if [ $? -eq 0 ]; then
            echo "Process '$PROCESS_NAME' killed successfully."
        else
            echo "Failed to kill the process '$PROCESS_NAME'."
        fi
    else
        echo "Process '$PROCESS_NAME' not found."
    fi
}

# 功能块开始
handle_reset() {
    # 功能：reset
    # 进程号：$1
    # 进程类型：$2
	if [ "$1" -ge 1 ] && [ "$1" -lt 10 ]; then
		if [ "$2" == "all" ]; then
		
			# 关闭旧的看门狗进程
			kill_process_by_name "/FeProject/bin/fe_watchdog"
			
			# 关闭旧的 stat 进程
			kill_process_by_name "/FeProject/bin/pt61850netd_pqfe -d cfg_stat_data"
			
			# 关闭旧的 recall 进程
			kill_process_by_name "/FeProject/bin/pt61850netd_pqfe -d cfg_recallhis_data"
			
			# 关闭旧的 3s 进程
			kill_process_by_name "/FeProject/bin/pt61850netd_pqfe -d cfg_3s_data"
			
			# 关闭旧的 comtrade 进程
			kill_process_by_name "/FeProject/bin/pt61850netd_pqfe -d cfg_soe_comtrade"
			
			#关闭进程后等待一段时间，防止端口占用
			#sleep 5
		
			# 清空 runtime.cf 中的所有进程配置
			sed -i '/cfg_stat_data/d' /home/pq/FeProject/etc/runtime.cf
			sed -i '/cfg_recallhis_data/d' /home/pq/FeProject/etc/runtime.cf
			sed -i '/cfg_3s_data/d' /home/pq/FeProject/etc/runtime.cf
			sed -i '/cfg_soe_comtrade/d' /home/pq/FeProject/etc/runtime.cf
			sed -i '/fe_watchdog/d' /home/pq/FeProject/etc/runtime.cf
			
			# 根据进程号添加对应进程配置
			if [ "$1" -eq 1 ]; then
				#看门狗固定放在第一个，防止stop时会把要杀死的进程重启
				sed -i "2a\\$(printf '/FeProject/bin/ ^ fe_watchdog -m 8192 ^ ^ ^ 1 ^ IGNORE_RESTART ^\n')" /home/pq/FeProject/etc/runtime.cf
				# 进程号为 1，按固定格式添加
				sed -i "2a\\$(printf '/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_stat_data -s 1_1^ ^ ^ 1 ^                ^\n')" /home/pq/FeProject/etc/runtime.cf
				sed -i "2a\\$(printf '/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_recallhis_data -s 1_1^ ^ ^ 1 ^                ^\n')" /home/pq/FeProject/etc/runtime.cf
				sed -i "2a\\$(printf '/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_3s_data^ ^ ^ 1 ^                ^\n')" /home/pq/FeProject/etc/runtime.cf
				sed -i "2a\\$(printf '/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_soe_comtrade^ ^ ^ 1 ^                ^\n')" /home/pq/FeProject/etc/runtime.cf
				
			else
				#看门狗固定放在第一个，防止stop时会把要杀死的进程重启
				sed -i "2a\\$(printf '/FeProject/bin/ ^ fe_watchdog -m 8192 ^ ^ ^ 1 ^ IGNORE_RESTART ^\n')" /home/pq/FeProject/etc/runtime.cf
				# 进程号大于 1，按 -s ${i}_ 格式添加
				for i in $(seq 1 $1); do
					# 在 runtime.cf 中插入对应的配置行，直接插入变量替换结果
					sed -i "2a/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_stat_data -s ${i}_${1}^ ^ ^ 1 ^                ^" /home/pq/FeProject/etc/runtime.cf
					sed -i "2a/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_recallhis_data -s ${i}_${1}^ ^ ^ 1 ^                ^" /home/pq/FeProject/etc/runtime.cf
				done
				#以下部分没有多进程
				sed -i "2a\\$(printf '/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_3s_data^ ^ ^ 1 ^                ^\n')" /home/pq/FeProject/etc/runtime.cf
				sed -i "2a\\$(printf '/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_soe_comtrade^ ^ ^ 1 ^                ^\n')" /home/pq/FeProject/etc/runtime.cf
				
			fi
			
			# 确保文件已被写入并刷新
			sync
	
			# 重新启动服务
			
			/home/pq/FeProject/boot/start_fe.sh
			
			echo "****** reset all in $1******" >>"$LOGFILE"	
		elif [ "$2" == "stat" ]; then
			# 清空 runtime.cf 中包含 cfg_stat_data 的行
			sed -i '/cfg_stat_data/d' /home/pq/FeProject/etc/runtime.cf
			
			# 根据进程号来添加新的进程配置
			for i in $(seq 1 $1); do
				sed -i "2a/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_stat_data -s ${i}_${1}^ ^ ^ 1 ^                ^" /home/pq/FeProject/etc/runtime.cf
			done
			
			# 关闭旧的看门狗进程
			kill_process_by_name "/FeProject/bin/fe_watchdog"
			
			# 关闭旧的 stat 进程
			kill_process_by_name "/FeProject/bin/pt61850netd_pqfe -d cfg_stat_data"
			
			#sleep 5
			
			# 启动服务不影响其他功能的进程
			/home/pq/FeProject/boot/start_fe.sh
			
			echo "****** reset stat in $1******" >>"$LOGFILE"
		elif [ "$2" == "recall" ]; then
			# 清空 runtime.cf 中包含 cfg_recallhis_data 的行
			sed -i '/cfg_recallhis_data/d' /home/pq/FeProject/etc/runtime.cf
			
			# 根据进程号来添加新的进程配置
			for i in $(seq 1 $1); do
				sed -i "2a/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_recallhis_data -s ${i}_${1}^ ^ ^ 1 ^                ^" /home/pq/FeProject/etc/runtime.cf
			done
			
			# 关闭旧的看门狗进程
			kill_process_by_name "/FeProject/bin/fe_watchdog"
			# 关闭旧的 recall 进程
			kill_process_by_name "/FeProject/bin/pt61850netd_pqfe -d cfg_recallhis_data"
			
			#sleep 5
			
			# 启动服务不影响其他功能的进程
			/home/pq/FeProject/boot/start_fe.sh
			
			echo "****** reset recall in $1******" >>"$LOGFILE"
		else
			echo "******  process reset type null  ******" 
			echo "******  process reset type null  ******" >>"$LOGFILE"
		fi
	else
		# 如果 $1 不在 1 到 10 之间，记录错误日志
		echo "****** Error: Invalid process number '$1'. It must be between 1 and 10. ******" 
		echo "****** Error: Invalid process number '$1'. It must be between 1 and 10. ******" >>"$LOGFILE"
	fi
}

handle_add() {
    # 功能：add
    # 进程号：$1
    # 进程类型：$2


	if [ "$1" -gt 1 ] && [ "$1" -lt 10 ]; then
		# 如果 $1 在 1 和 10 之间，进入处理逻辑
		if [ "$2" == "all" ]; then
			# 检查是否已存在该条目，避免重复添加
			if ! grep -q "/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_stat_data -s ${1}_${1}^" /home/pq/FeProject/etc/runtime.cf; then
				sed -i "2a/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_stat_data -s ${1}_${1}^ ^ ^ 1 ^                ^" /home/pq/FeProject/etc/runtime.cf
			fi
			
			if ! grep -q "/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_recallhis_data -s ${1}_${1}^" /home/pq/FeProject/etc/runtime.cf; then
				sed -i "2a/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_recallhis_data -s ${1}_${1}^ ^ ^ 1 ^                ^" /home/pq/FeProject/etc/runtime.cf
			fi
		elif [ "$2" == "stat" ]; then
			# 检查是否已存在该条目，避免重复添加
			if ! grep -q "/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_stat_data -s ${1}_${1}^" /home/pq/FeProject/etc/runtime.cf; then
				sed -i "2a/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_stat_data -s ${1}_${1}^ ^ ^ 1 ^                ^" /home/pq/FeProject/etc/runtime.cf
			fi
		elif [ "$2" == "recall" ]; then
			# 检查是否已存在该条目，避免重复添加
			if ! grep -q "/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_recallhis_data -s ${1}_${1}^" /home/pq/FeProject/etc/runtime.cf; then
				sed -i "2a/FeProject/bin/ ^ pt61850netd_pqfe -d cfg_recallhis_data -s ${1}_${1}^ ^ ^ 1 ^                ^" /home/pq/FeProject/etc/runtime.cf
			fi
		else
			echo "****** process add type null ******" 
			echo "****** process add type null ******" >> "$LOGFILE"
		fi
		
		# 关闭旧的看门狗进程
		kill_process_by_name "/FeProject/bin/fe_watchdog"
		
		#sleep 5
        
		# 启动服务，不影响正在运行的进程
		/home/pq/FeProject/boot/start_fe.sh
		
	else
		# 如果 $1 不在 1 到 10 之间，记录错误日志
		echo "****** Error: Invalid process number '$1'. It must be between 1 and 10. ******"
		echo "****** Error: Invalid process number '$1'. It must be between 1 and 10. ******" >> "$LOGFILE"
	fi

	echo "****** add $2 -- $1******" >>"$LOGFILE"
}

# 获取当前脚本的进程ID
CURRENT_PID=$$

# 检查是否有其他的set_process.sh脚本正在运行，排除当前脚本
if pgrep -f "set_process.sh" | grep -v "^$CURRENT_PID$" > /dev/null; then
    echo "set_process.sh is already running. Exiting..."
    echo "set_process.sh is already running. Exiting..." >>"$LOGFILE"
    exit 1
fi

#脚本应该等待3秒钟
sleep 3

# 根据入参判断是 reset 还是 add
if [ "$1" == "reset" ]; then
    handle_reset $2 $3
elif [ "$1" == "add" ]; then
    handle_add $2 $3
else
    echo "Invalid option. Usage: $0 {reset|add} {process_number} {process_type}"
    exit 1
fi

# 获取当前时间并记录进程添加成功的日志
DT=$(date "+%F %R:%S.%N")
echo "****** ${DT:0:23} set Processes Successfully ******" 
echo "" >>"$LOGFILE"
echo "****** ${DT:0:23} set Processes Successfully ******" >>"$LOGFILE"
