#!/bin/bash

g10s_operFile="/opt/UltraPath/log/oper_log"

function print_event()
{
    local fileFlag=""

    if [ `echo "$1"|grep -E -c "[0-9]+"` -eq 0 ] || [ "$2" = "" ]
    then
        echo -e -n "\033[31m[ERROR:2][g10s][`date +%Y-%m-%d_%H:%M:%S`][$0,${FUNCNAME[1]},${BASH_LINENO[0]}]\033[0m ";echo "param error[$1,$2]"
        [ -f "${g10s_operFile}" ] && [ -w "${g10s_operFile}" ] && echo "[`date +%Y-%m-%d_%H:%M:%S`][ERROR:2][g10s][$0,${FUNCNAME[1]},${BASH_LINENO[0]}] param error[$1,$2]" >> "${g10s_operFile}"
        t100s_exit 1
    fi
    if [ "$3" != "" ]
    then
        fileFlag=$3
    else
        fileFlag=${0##*/}
        fileFlag=${fileFlag%%_*}
    fi
    [ -f "${g10s_operFile}" ] && [ -w "${g10s_operFile}" ] && echo "[`date +%Y-%m-%d_%H:%M:%S`][`id -u`][EVENT:$1][${fileFlag}][$0,${FUNCNAME[1]},${BASH_LINENO[0]}] $2">>"${g10s_operFile}"
    return 0
}

function print_error()
{
    local fileFlag=""
    local tmpDebugon=""

    if [ "$2" = "" ] || [ `echo "$1"|grep -E -c "[0-9]+"` -eq 0 ]
    then
        echo -e -n "\033[31m[ERROR:2][g10s][`date +%Y-%m-%d_%H:%M:%S`][$0,${FUNCNAME[1]},${BASH_LINENO[0]}]\033[0m ";echo "param error[$1,$2]"
        [ -f "${g10s_operFile}" ] && [ -w "${g10s_operFile}" ] && echo "[`date +%Y-%m-%d_%H:%M:%S`][ERROR:2][g10s][$0,${FUNCNAME[1]},${BASH_LINENO[0]}] param error[$1,$2]" >> "${g10s_operFile}"
        return 1
    fi
    if [ "$3" != "" ]
    then
        fileFlag=$3
    else
        fileFlag=${0##*/}
        fileFlag=${fileFlag%%_*}
    fi
   
    echo -e -n "\033[31m[ERROR:$1][${fileFlag}][`date +%Y-%m-%d_%H:%M:%S`][$0,${FUNCNAME[1]},${BASH_LINENO[0]}]\033[0m ";echo "$2"
   
    [ -f "${g10s_operFile}" ] && [ -w "${g10s_operFile}" ] && echo  "[`date +%Y-%m-%d_%H:%M:%S`][`id -u`][ERROR:$1][${fileFlag}][$0,${FUNCNAME[1]},${BASH_LINENO[0]}] $2">>"${g10s_operFile}"
    return 0
}

#The 'killall' command may be not installed in some operating systems by default. This function is used to replace the 'killall' command.
#return 0 is succeed to stop the service, return 1 is fail to stop the service(May be param invalid. May be multiple matches and can not kill them).
function kill_service_by_name()
{
    local number
    local time
    local service_pid
        
    if [ -z "${1}" ]; then
        basic_trace_msg "ERROR" "param invalid in function kill_service_by_name." "verbose"
        return 1
    fi
        
    number=$(ps x | grep -v "grep" | grep -cw "${1}")
    if [ ${number} -lt 1 ]; then
        return 0
    elif [ ${number} -gt 1 ]; then
        return 1
    fi
        
    number=$(which killall 2>/dev/null | grep -c "/killall")
    if [ ${number} -eq 1 ]; then
        killall "${1}" 2>/dev/null
        number=$(ps x | grep -v "grep" | grep -cw "${1}")
        if [ ${number} -lt 1 ]; then
           return 0
        fi
    fi
        
    service_pid=$(ps x | grep -v "grep" | grep -w "${1}" | awk '{print $1}')
    kill -9 ${service_pid} 2>/dev/null
    for (( time=0; time<11; time=time+1))
    do
        number=$(ps x | grep -v "grep" | grep -cw "${1}")
        if [ ${number} -eq 0 ]; then
            return 0
        else
            sleep 0.5
        fi
    done
        
    return 1
}

pgrep up_restart_warn 2>/dev/null
if [ $? -eq 0 ]
then
    kill_service_by_name up_restart_warn
    return_val=$?
    if [ "${return_val}" = "0" ]; then
        echo "succeed to stop up_restart_warn"
        print_event "1020" "The up_restart_warn are stopped successfully."
        exit
    fi
    
    print_error "1021" "fail to stop up_restart_warn."
    exit
fi
echo "up_restart_warn is not exist"
print_event "1022"  "up_restart_warn is not exist."
