#!/bin/bash

# @file:    set_debug.sh
# @brief:   启动或删除进程的脚本
# @version: 1.0
# @date:    2025/2/8 10:22:43
# @author:  lunankun

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

# 输出当前时间并打印进程操作信息
echo "" ; echo "" 
echo "****** `date "+%F %R:%S"` start set_debug Processes ******"
echo "" >>"$LOGFILE"
echo "" >>"$LOGFILE"
echo "****** `date "+%F %R:%S"` start set_debug Processes ******" >>"$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_pid() {
    PID=$1
    if [ -n "$PID" ]; then
        echo "Found process with PID: $PID"
        echo "Killing process..."
        kill -9 $PID
        if [ $? -eq 0 ]; then
            echo "Process with PID: $PID killed successfully."
        else
            echo "Failed to kill the process with PID: $PID."
        fi
    else
        echo "Process not found."
    fi
}

# 启动进程的函数
start_process() {
    IP=$1
    TYPE=$2
	INDEX=$3

    # 根据类型决定启动的进程
    if [ "$TYPE" == "stat" ]; then
        PROCESS_NAME="pt61850netd_pqfe -d cfg_stat_data -a $IP -s 0_$INDEX"
    elif [ "$TYPE" == "recall" ]; then
        PROCESS_NAME="pt61850netd_pqfe -d cfg_recallhis_data -a $IP -s 0_$INDEX"
    elif [ "$TYPE" == "comtrade" ]; then
        PROCESS_NAME="pt61850netd_pqfe -d cfg_soe_comtrade -a $IP -s 0_$INDEX"
    elif [ "$TYPE" == "3s" ]; then
        PROCESS_NAME="pt61850netd_pqfe -d cfg_3s_data -a $IP -s 0_$INDEX"
    else
        echo "Invalid type: $TYPE. Supported types are stat, recall, comtrade, or 3s."
        exit 1
    fi

    # 检查是否已经有相同的进程在运行
    PID=$(ps -ef | grep "$PROCESS_NAME" | grep -v "grep" | awk '{print $2}')
    
    if [ -n "$PID" ]; then
        echo "Process '$PROCESS_NAME' already running with PID: $PID. Skipping start."
        echo "Process '$PROCESS_NAME' already running with PID: $PID. Skipping start." >>"$LOGFILE"
    else
        # 启动进程并记录PID
        echo "Starting process: $PROCESS_NAME"
        $PROCESS_NAME &
		sleep 1
        PID=$(ps -ef | grep "$PROCESS_NAME" | grep -v "grep" | awk '{print $2}')
        echo "Started process with PID: $PID"
        
        # 记录日志
        echo "****** `date "+%F %R:%S"` Started process $PROCESS_NAME with PID: $PID" >>"$LOGFILE"
    fi
}

# 删除进程的函数
delete_process() {
    IP=$1
    TYPE=$2
    INDEX=$3

    if [ "$TYPE" == "stat" ]; then
        PROCESS_NAME="pt61850netd_pqfe -d cfg_stat_data -a $IP -s 0_$INDEX"
    elif [ "$TYPE" == "recall" ]; then
        PROCESS_NAME="pt61850netd_pqfe -d cfg_recallhis_data -a $IP -s 0_$INDEX"
    elif [ "$TYPE" == "comtrade" ]; then
        PROCESS_NAME="pt61850netd_pqfe -d cfg_soe_comtrade -a $IP -s 0_$INDEX"
    elif [ "$TYPE" == "3s" ]; then
        PROCESS_NAME="pt61850netd_pqfe -d cfg_3s_data -a $IP -s 0_$INDEX"
    else
        echo "Invalid type: $TYPE. Supported types are stat, recall, comtrade, or 3s."
        exit 1
    fi

    # 查找进程并获取PID
    PID=$(ps -ef | grep "$PROCESS_NAME" | grep -v "grep" | awk '{print $2}')

    # 如果找到进程，杀死它
    if [ -n "$PID" ]; then
        kill_process_by_pid $PID
        echo "****** `date "+%F %R:%S"` Deleted process $PROCESS_NAME with PID: $PID" >>"$LOGFILE"
    else
        echo "delete Process $PROCESS_NAME not found."
		echo "delete Process $PROCESS_NAME not found." >>"$LOGFILE"
    fi
}

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

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

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

# 根据入参判断是 start 还是 delete
if [ "$1" == "start" ]; then
    if [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; then
        echo "Usage: $0 start <IP> <stat|recall|comtrade|3s>"
        exit 1
    fi
    start_process $2 $3 $4
elif [ "$1" == "delete" ]; then
    if [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; then
        echo "Usage: $0 delete <IP> <stat|recall|comtrade|3s>"
        exit 1
    fi
    delete_process $2 $3 $4
else
    echo "Invalid option. Usage: $0 {start|delete} {IP} {stat|recall|comtrade|3s}"
    exit 1
fi

# 获取当前时间并记录进程操作成功的日志
DT=$(date "+%F %R:%S.%N")
echo "****** ${DT:0:23} set_debug Process operation completed successfully ******" 
echo "" >>"$LOGFILE"
echo "****** ${DT:0:23} set_debug Process operation completed successfully ******" >>"$LOGFILE"
