#!/bin/bash

if [ -f /etc/UVP-release  ] && [ `cat /etc/UVP-release | grep -c "UVP"` -ne 0 ]
then
    g10s_logFile="/var/log/UltraPath/ultrapathTool.log"
elif [ -f /etc/uvp_version  ] && [ `cat /etc/uvp_version | grep -c "UVP"` -ne 0 ]
then
    g10s_logFile="/var/log/UltraPath/ultrapathTool.log"
elif [ -f /etc/EulerLinux.conf  ]
then
    g10s_logFile="/var/log/UltraPath/ultrapathTool.log"
else
    g10s_logFile="/opt/UltraPath/log/ultrapathTool.log"
fi

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_logFile}" ] && [ -w "${g10s_logFile}" ] && echo "[ERROR:2][g10s][`date +%Y-%m-%d_%H:%M:%S`][$0,${FUNCNAME[1]},${BASH_LINENO[0]}] param error[$1,$2]" >> "${g10s_logFile}"
        t100s_exit 1
    fi
    if [ "$3" != "" ]
    then
        fileFlag=$3
    else
        fileFlag=${0##*/}
        fileFlag=${fileFlag%%_*}
    fi
    [ -f "${g10s_logFile}" ] && [ -w "${g10s_logFile}" ] && echo "[EVENT:$1][${fileFlag}][`date +%Y-%m-%d_%H:%M:%S`][`id -u`][$0,${FUNCNAME[1]},${BASH_LINENO[0]}] $2">>"${g10s_logFile}"
    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_logFile}" ] && [ -w "${g10s_logFile}" ] && echo "[ERROR:2][g10s][`date +%Y-%m-%d_%H:%M:%S`][$0,${FUNCNAME[1]},${BASH_LINENO[0]}] param error[$1,$2]" >> "${g10s_logFile}"
        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_logFile}" ] && [ -w "${g10s_logFile}" ] && echo  "[ERROR:$1][${fileFlag}][`date +%Y-%m-%d_%H:%M:%S`][`id -u`][$0,${FUNCNAME[1]},${BASH_LINENO[0]}] $2">>"${g10s_logFile}"
    return 0
}

msg_file="/opt/UltraPath/wall_msg"
cfg_File="/etc/UltraPath.packages.info"


function getFillMsg()
{
    if [ `cat ${cfg_File} | grep -c "^[[:blank:]]*${1}"` -eq 0 ]
    then
        cfg_File="null"
        echo "null"
        return 0
    fi
  
    up_CfgValue=`cat ${cfg_File} |grep "solutionType" |grep "^[[:blank:]]*${1}" | awk -F "=" '{print $2}'`
    service_Type=$(echo ${up_CfgValue})
    if [ $service_Type == "service" ]
    then 
        echo "this is a message send by UltraPath, for avoiding duplicated disks caused data corruption, it is strongly recommended to restart your system or active UltraPath manually." > $msg_file
        echo "To active UltraPath, first run \"service nxup stop\" or \"/etc/init.d/nxup stop\" to stop service, then run \"service nxup start\" or \"/etc/init.d/nxup start\" to start service." >> $msg_file  
    else
        echo "this is a message send by UltraPath, for avoiding duplicated disks caused data corruption, it is strongly recommended to restart your system." > $msg_file
    fi
 
    echo "To disable this warning message, please run \"stop_up_restart_warn\" by a root user." >> $msg_file
    chmod 600 $msg_file
    print_event "1006" "The up_restart_warn are executed successfully."
}
getFillMsg
sleep 900
while [ "" == "" ]
do
	wall < $msg_file        
	sleep 900       
done

rm -rf $msg_file