#!/bin/sh  
. /etc/init.d/nas/confoper
. /etc/init.d/nas/netpolicy

noontec_echo_info()                                                                                  
{
	echo -e '\033[0;30;1m'WARN:"$1"'\033[0m'
	return 0         
}                                                                                              
                                                                                               
noontec_echo_success()                                                   
{ 
	echo -e '\033[0;32;1m'SUCC:"$1"'\033[0m'
	return 0                                   
}                                            
                                             
noontec_echo_failure()                               
{                                            
	echo -e '\033[0;31;1m'FAIL:"$1"'\033[0m'
	return 1         
}                

noontec_echo_warning()                                                                                 
{                                                                                 
	echo -e '\033[0;33;1m'WARN:"$1"'\033[0m'
	return 1         
}                                                                        
                                                                   
noontec_strstr()                                                           
{                                                                  
  [ "${1#*$2*}" = "$1" ] && return 1                               
  return 0                                                         
}                                                                  

# Output PIDs of matching processes, found using pidof                                               
noontec_pidspidof() {                                                                                     
    #pidof -c -o $$ -o $PPID -o %PPID -x "$1" || pidof -c -o $$ -o $PPID -o %PPID -x "${1##*/}"      
    pidof -o $$ -o $PPID -o %PPID "$1" || pidof -o $$ -o $PPID -o %PPID "${1##*/}"                   
}                                                                                                    
                                                                                                     
# check $pid to pids from /var/run* for {program}.  $pid should be declared                            
# Returns LSB exit code for the 'status' action.                                                     
noontec_pidsvarrun() 
{                                                                                   
    local base=${1##*/}                                                                              
    local pid_file=${2:-/var/run/$base.pid}                                                          
                                                                                                     
    pid=                                                                                             
    if [ -f "$pid_file" ] ; then                                                                     
            local line p                                                                             
        read line < "$pid_file"                                                                      
        for p in $line ; do                                                                          
            [ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid="$pid $p"                                  
        done                                                                                         
            if [ -n "$pid" ]; then                                                                   
                    return 0                                                                         
            fi                                                                                       
        return 1 # "Program is dead and /var/run pid file exists"                                    
    fi                                                                                               
    return 3 # "Program is not running"                                                              
}                                                                                                    

noontec_checkpid()                                                                                           
{                                                                                                    
    local i                                                                                          
                                                                                                     
    for i in $* ; do                                                                                 
        [ -d "/proc/$i" ] && return 0                                                                
    done                                                                                             
    return 1                                                                                         
}                                                                                                    
                                                                                                     
#find the pid of a program.                                                           
noontec_serverpidofproc()                                                                                          
{                                                                                                    
    local RC pid pid_file=                                                                           
                                                                                                     
    # Test syntax.                                                                                   
    if [ "$#" = 0 ]; then                                                                            
        echo "Usage: noontec_serverpidofproc [-p pidfile] {program}"                                               
        return 1                                                                                     
    fi                                                                                               
    if [ "$1" = "-p" ]; then                                                                         
        pid_file=$2                                                                                  
        shift 2                                                                                      
    fi                                                                                               
    fail_code=3 # "Program is not running"                                                           
                                                                                                     
    # First try "/var/run/*.pid" files                                                               
    noontec_pidsvarrun "$1" "$pid_file"                                                                  
    RC=$?                                                                                            
    if [ -n "$pid" ]; then                                                                           
        echo $pid                                                                                    
        return 0                                                                                     
    fi                                                                                               
                                                                                                     
    [ -n "$pid_file" ] && return $RC                                                                 
    noontec_pidspidof "$1" || return $RC                                                                  
}                          

#check server runing status
noontec_serverstatus() {                                                                                          
    local base pid pid_file=                                                                         
                                                                                                     
    # Test syntax.                                                                                   
    if [ "$#" = 0 ] ; then                                                                           
        echo "Usage: status [-p pidfile] {program}"                                                  
        return 1                                                                                     
    fi                                                                                               
    if [ "$1" = "-p" ]; then                                                                         
        pid_file=$2                                                                                  
        shift 2                                                                                      
    fi                                                                                               
    base=${1##*/}                                                                                    
                                                                                                     
    # First try "pidof"                                                                              
    noontec_pidsvarrun "$1" "$pid_file"                                                                  
    RC=$?                                                                                            
    if [ -z "$pid_file" -a -z "$pid" ]; then                                                         
        pid="$(noontec_pidspidof "$1")"                                                                   
    fi                                                                                               
    if [ -n "$pid" ]; then                                                                           
            echo "${base} (pid $pid) is running..."                                                  
            return 0                                                                                 
    fi                  
                                                                                                     
    case "$RC" in                                                                                    
        0)                                                                                           
            echo "${base} (pid $pid) is running..."                                                  
            return 0                                                                                 
            ;;                                                                                       
        1)                                                                                           
                    echo "${base} dead but pid file exists"                                          
                    return 1                                                                         
            ;;                                                                                       
    esac                                                                                             
    # See if /var/lock/subsys/${base} exists                                                         
    if [ -f /var/lock/subsys/${base} ]; then                                                         
        echo "${base} dead but subsys locked"                                                        
        return 2                                                                                     
    fi                                                                                               
    echo "${base} is stopped"                                                                        
    return 3                                                                                         
}                                                                                                    
 
#kill the pids of the program
noontec_killproc()                                                                                           
{                                                                                                    
    local RC killlevel= base pid pid_file= delay                                                     
                                                                                                     
    RC=0; delay=3                                                                                    
    # Test syntax.                                                                                   
    if [ "$#" -eq 0 ]; then                                                                          
        echo "Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"                          
        return 1                                                                                     
    fi                                                                                               
    if [ "$1" = "-p" ]; then                                                                         
        pid_file=$2                                                                                  
        shift 2                                                                                      
    fi                                                                                               
    if [ "$1" = "-d" ]; then                                                                         
        delay=$2                                                                                     
        shift 2                                                                                      
    fi                                                                                               
                                                                                                     
                                                                                                     
    # check for second arg to be kill level                                                          
    [ -n "${2:-}" ] && killlevel=$2                                                                  
                                                                                                     
        # Save basename.                                                                             
        base=${1##*/}                                                                                
                                                                                                     
        # Find pid.                                                                                  
    noontec_pidsvarrun "$1" "$pid_file"                                                                  
    if [ -z "$pid_file" -a -z "$pid" ]; then                                                         
        pid="$(noontec_pidspidof "$1")"                                                                   
    fi                                                                                               
                                                                                                     
        # Kill it.                                                                                   
        if [ -n "$pid" ] ; then                                                                      
                [ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base "                       
        if [ -z "$killlevel" ] ; then                                                                
               if noontec_checkpid $pid 2>&1; then                                                           
               # TERM first, then KILL if not dead                                                   
               kill -TERM $pid >/dev/null 2>&1                                                       
               usleep 100000                                                                         
               if noontec_checkpid $pid && sleep 1 &&                                                        
                  noontec_checkpid $pid && sleep $delay &&                                                   
                  noontec_checkpid $pid ; then                                                               
                                kill -KILL $pid >/dev/null 2>&1    
                usleep 100000                                                                        
               fi                                                                                    
                fi                                                                                   
            noontec_checkpid $pid  
            RC=$?                                                                                    
            [ "$RC" -eq 0 ] && noontec_echo_failure "$base shutdown" || noontec_echo_success "$base shutdown"        
            RC=$((! $RC))                                                                            
        # use specified level only                                                                   
        else                                                                                         
                if noontec_checkpid $pid; then                                                               
                        kill $killlevel $pid >/dev/null 2>&1                                         
                RC=$?                                                                                
                [ "$RC" -eq 0 ] && noontec_echo_success "$base $killlevel" || noontec_echo_failure "$base $killlevel"
            elif [ -n "${LSB:-}" ]; then                                                             
                RC=7 # Program is not running                                                        
            fi                                                                                       
        fi                                                                                           
    else                                                                                             
        if [ -n "${LSB:-}" -a -n "$killlevel" ]; then                                                
            RC=7 # Program is not running                                                            
        else                                                                                         
            noontec_echo_failure "$base shutdown"                                                            
            RC=0                                                                                     
        fi                                                                                           
    fi                                                                                               
                                                                                                     
        # Remove pid file if any.                                                                    
    if [ -z "$killlevel" ]; then                                                                     
            rm -f "${pid_file:-/var/run/$base.pid}"                                                  
    fi                                                                                               
    return $RC                                                                                       
}

noontec_execcommandstrret()
{
	command="$1"
	key="$2"
	#0/wrong,1/right
	#ethtool -s eth0 speed 100 2>&1|grep "Invalid";echo $?
	$command 2>&1|grep -q "$key"
	if [ $?	-eq 0 ]
	then
		#echo 1 //find the key
		return 1
	else
		#echo 0 //no find the key
		return 0
	fi
}

noontec_checkipvaliade()
{
	mode="$1"
	ip_mask="$2"
	if [ "$mode" -eq 1 ]
	then
		retstr="$(echo "$ip_mask"|sed '/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/p')"
	else
		retstr="$(echo "$ip_mask"|sed '/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-4])$/p')" 
	fi
	
	if [ ! -z "$retstr" ]
	then
		echo 1
		return 1
	else
		echo 0
		return 0
	fi	
}

#delete the keystring
noontec_delstringline()
{
	filename="$1"
	repace_str="$2"
	if [ -f "${filename}" ]
	then
		busybox sed -i "/${repace_str}/d" ${filename} > /dev/null 2>&1
		if [ "$?" -ne 0 ];then
			echo 1
			return 1
		fi
	fi

	echo 0
	return 0
}


#modify the keystring
noontec_replace_str()
{
	filename="$1"
	repace_str="$2"
	new_str="$3"
	repace_str="$(echo ${repace_str}|awk 'BEGIN{MYESCAP="/";NEWESCAP="\\/"}	\
	{if(match($0,MYESCAP) > 0){gsub(MYESCAP,NEWESCAP,$0)}}END{print $0}')"
	new_str="$(echo ${new_str}|awk 'BEGIN{MYESCAP="/";NEWESCAP="\\/"}	\
	{if(match($0,MYESCAP) > 0){gsub(MYESCAP,NEWESCAP,$0)}}END{print $0}')"
	#busybox sed -i 's/'${repace_str}'/'${new_str}'/g' ${filename}
	busybox sed -i "s/${repace_str}/${new_str}/g" ${filename} > /dev/null 2>&1
	if [ "$?" -eq 0 ]
	then
		echo 0
	else
		echo 1
		return 1
	fi

	return 0
}

#modify the keystring
noontec_replace_str1()
{
	filename="$1"
	repace_str="$2"
	new_str="$3"
	repace_str="$(echo ${repace_str}|awk 'BEGIN{MYESCAP="/";NEWESCAP="\\/"}	\
	{if(match($0,MYESCAP) > 0){gsub(MYESCAP,NEWESCAP,$0)}}END{print $0}')"
	new_str="$(echo ${new_str}|awk 'BEGIN{MYESCAP="/";NEWESCAP="\\/"}	\
	{if(match($0,MYESCAP) > 0){gsub(MYESCAP,NEWESCAP,$0)}}END{print $0}')"
	#echo repace_str:$repace_str
	#echo new_str:$new_str
	#busybox sed -i 's/'${repace_str}'/'${new_str}'/g' ${filename}
	busybox sed -i "s/^${repace_str}/${new_str}/g" ${filename} > /dev/null 2>&1
	if [ "$?" -eq 0 ]
	then
		echo 0
	else
		echo 1
		return 1
	fi

	return 0
}

noontec_replace_str2()
{
	filename="$1"
	repace_str="$2"
	new_str="$3"
	repace_str="$(echo ${repace_str}|awk 'BEGIN{MYESCAP="/";NEWESCAP="\\/"}	\
	{if(match($0,MYESCAP) > 0){gsub(MYESCAP,NEWESCAP,$0)}}END{print $0}')"
	new_str="$(echo ${new_str}|awk 'BEGIN{MYESCAP="/";NEWESCAP="\\/"}	\
	{if(match($0,MYESCAP) > 0){gsub(MYESCAP,NEWESCAP,$0)}}END{print $0}')"
	#echo repace_str:$repace_str
	#echo new_str:$new_str
	#busybox sed -i "s/^${repace_str}/${new_str}/g" ${filename} > /dev/null 2>&1
	#busybox sed -i "s/^[^#]*${repace_str}.*$/${new_str}/" ${filename} > /dev/null 2>&1
	busybox sed -i "s/^[^#]*${repace_str}.*/${new_str}/" ${filename} > /dev/null 2>&1
	if [ "$?" -eq 0 ]
	then
		echo 0
	else
		echo 1
		return 1
	fi

	return 0
}

noontec_replace_str3()
{
	filename="$1"
	repace_str="$2"
	new_str="$3"
	repace_str="$(echo ${repace_str}|awk 'BEGIN{MYESCAP="/";NEWESCAP="\\/"}	\
	{if(match($0,MYESCAP) > 0){gsub(MYESCAP,NEWESCAP,$0)}}END{print $0}')"
	new_str="$(echo ${new_str}|awk 'BEGIN{MYESCAP="/";NEWESCAP="\\/"}	\
	{if(match($0,MYESCAP) > 0){gsub(MYESCAP,NEWESCAP,$0)}}END{print $0}')"
	#echo repace_str:$repace_str
	#echo new_str:$new_str
	#busybox sed -i "s/^${repace_str}/${new_str}/g" ${filename} > /dev/null 2>&1
	#busybox sed -i "s/^[^#]*${repace_str}.*$/${new_str}/" ${filename} > /dev/null 2>&1
	busybox sed -i "s/\s*#.*${repace_str}.*/${new_str}/" ${filename} > /dev/null 2>&1
	if [ "$?" -eq 0 ]
	then
		echo 0
	else
		echo 1
		return 1
	fi

	return 0
}

noontec_find_str()
{
	filename="$1"
	find_str="$2"
	find_str="$(echo ${find_str}|awk 'BEGIN{MYESCAP="/";NEWESCAP="\\/"}	\
	{if(match($0,MYESCAP) > 0){gsub(MYESCAP,NEWESCAP,$0)}}END{print $0}')"
	#echo find_str:$find_str
	if [ $# -gt 2 ]
	then
		
		delimiter="$3"
#		delimiter="$(echo ${delimiter}|awk 'BEGIN{MYESCAP="\\";NEWESCAP="\\\\"}	\
#		{if(match($0,MYESCAP) > 0){gsub(MYESCAP,NEWESCAP,$0)}}END{print $0}')"
#		echo delimiter:$delimiter	
#		delimiter=${3:0:2}
#		if [ "$delimiter" == "\\" ]
#		then
#			delimiter=${3:0:2}	
#		fi
		
		filed="$4"
		#result="$(busybox sed  -n '/'${find_str}'/'p  ${filename}|awk -F"${delimiter}" '{print $'$filed'}')"
		result="$(busybox sed  -n "/${find_str}/"p  ${filename}|awk -F"${delimiter}" '{print $'$filed'}')" 
		if [ "$?" -eq 0 ]
		then
			echo "$result"
		else
			echo 1
			return 1
		fi
	else
		result="$(busybox sed -n "/${find_str}/"p  ${filename})"
		if [ "$?" -eq 0 ]
		then
			echo "$result"
		else
			echo 1
			return 1
		fi
	fi
	
	return 0
}

#find the keystring 
noontec_find_str1()
{
	filename="$1"
	find_str="$2"
	find_str="$(echo ${find_str}|awk 'BEGIN{MYESCAP="/";NEWESCAP="\\/"}	\
	{if(match($0,MYESCAP) > 0){gsub(MYESCAP,NEWESCAP,$0)}}END{print $0}')"
	#echo find_str:$find_str
	if [ $# -gt 2 ]
	then
		delimiter="$3"
#		delimiter="$(echo ${delimiter}|awk 'BEGIN{MYESCAP="\\";NEWESCAP="\\\\"}	\
#		{if(match($0,MYESCAP) > 0){gsub(MYESCAP,NEWESCAP,$0)}}END{print $0}')"
#		delimiter=${3:0:2}
#		if [ "$delimiter" == "\\" ]
#		then
#			delimiter=${3:0:2}	
#		fi
		
		filed="$4"
		#result="$(busybox sed  -n '/'${find_str}'/'p  ${filename}|awk -F"${delimiter}" '{print $'$filed'}')"
		#result="$(busybox sed  -n "/^${find_str}/"p  ${filename}|awk -F"${delimiter}" '{print $'$filed'}')"
		result="$(busybox sed  -n "/^[ \t]*${find_str}.*$/"p  ${filename}|awk -F"${delimiter}" '{print $'$filed'}')"
		if [ "$?" -eq 0 ]
		then
			echo "$result"
		else
			echo 1
			return 1
		fi
	else
		result="$(busybox sed -n "/^${find_str}/"p  ${filename})"
		if [ "$?" -eq 0 ]
		then
			echo "$result"
		else
			echo 1
			return 1

		fi
	fi
	
	return 0
}


nootec_gethttpport()
{                  
	httpconf=/etc/nginx/nginx.conf
	port=$(sed -n '/\#\s*HTTP\s*server\b/,/\#\s*HTTP\s*server\b/{/[^#][ |  |   |    |\t]*\blisten\b/p}' "$httpconf" |awk '{print $2}' |awk -F";" '{print $1}')
	if [ -z "$port" ];then
		port=80
	fi
	echo $port
	
}

nootec_gethttpservername() 
{                                                                                                                            
	httpconf=/etc/nginx/nginx.conf
	servername=$(sed -n '/\#\s*HTTP\s*server\b/,/\#\s*HTTP\s*server\b/{/[^#][ |  |   |    |\t]*\bserver_name\b/p}' "$httpconf" |awk '{print $2}' |awk -F";" '{print $1}')
	if [ -z "$servername" ];then
		servername=localhost
	fi
	echo $servername
}
nootec_sethttpport()
{                 
	httpconf=/etc/nginx/nginx.conf
	tmp=/tmp/nginx.conf
	if [ -z "$1" ];then
		echo 1
		return 1
	fi 
	port="$1"
	sed -i '/\#\s*HTTP\s*server\b/,/\#\s*HTTP\s*server\b/s/[^#][ |  |   |    |\t]*\blisten\b.*/\tlisten\t'$port';/' "$httpconf"
	if [ $? -ne 0 ];then
		echo 1
		return 1
	else
		cp $httpconf $tmp -a
		echo 0
		return 0
	fi	
}
nootec_sethttpservername() 
{                                                                                                                            
	httpconf=/etc/nginx/nginx.conf
	if [ -z "$1" ];then
		echo 1
		return 1
	fi 
	servername="$1"
	sed -i '/\#\s*HTTP\s*server\b/,/\#\s*HTTP\s*server\b/s/[^#][ |  |   |    |\t]*\bserver_name\b.*/\tserver_name\t'$servername';/' "$httpconf"
	if [ $? -ne 0 ];then
		echo 1
		return 1
	else
		echo 0
		return 0
	fi	
}
nootec_gethttpsport()
{                  
	httpconf=/etc/nginx/nginx.conf
	port=$(sed -n '/\#\s*HTTPS\s*server\b/,/\#\s*HTTPS\s*server\b/{/[^#][ |  |   |    |\t]*\blisten\b/p}' "$httpconf" |awk '{print $2}' |awk -F";" '{print $1}')
	if [ -z "$port" ];then
		port=80
	fi
	echo $port
	
}
nootec_gethttpsservername() 
{                                                                                                                            
	httpconf=/etc/nginx/nginx.conf
	servername=$(sed -n '/\#\s*HTTPS\s*server\b/,/\#\s*HTTPS\s*server\b/{/[^#][ |  |   |    |\t]*\bserver_name\b/p}' "$httpconf" |awk '{print $2}' |awk -F";" '{print $1}')
	if [ -z "$servername" ];then
		servername=localhost
	fi
	echo $servername
}

nootec_gethttpsstatus()
{                                                                                                                            
	httpconf=/etc/nginx/nginx.conf
	sslstatus=$(sed -n '/\#\s*HTTPS\s*server\b/,/\#\s*HTTPS\s*server\b/{/[^#][ |  |   |    |\t]*\bssl\b/p}' "$httpconf" |awk '{print $2}' |awk -F";" '{print $1}')
	if [ -z "$sslstatus" ];then
		sslstatus="off"
	fi
	echo $sslstatus
}

nootec_sethttpsstatus()
{                 
	httpconf=/etc/nginx/nginx.conf
	if [ -z "$1" ];then
		echo 1
		return 1
	fi 
	sslstatus="$1"
	if [ "$sslstatus" != "on" ];then
		sslstatus="off"
	fi
	sed -i '/\#\s*HTTPS\s*server\b/,/\#\s*HTTPS\s*server\b/s/[^#][ |  |   |    |\t]*\bssl\b.*/\tssl\t'$sslstatus';/' "$httpconf"
	if [ $? -ne 0 ];then
		echo 1
		return 1
	else
		echo 0
		return 0
	fi	
}

nootec_sethttpsport()
{                 
	httpconf=/etc/nginx/nginx.conf
	if [ -z "$1" ];then
		echo 1
		return 1
	fi 
	port="$1"
	sed -i '/\#\s*HTTPS\s*server\b/,/\#\s*HTTPS\s*server\b/s/[^#][ |  |   |    |\t]*\blisten\b.*/\tlisten\t'$port';/' "$httpconf"
	if [ $? -ne 0 ];then
		echo 1
		return 1
	else
		echo 0
		return 0
	fi	
}
nootec_sethttpsservername() 
{                                                                                                                            
	httpconf=/etc/nginx/nginx.conf
	if [ -z "$1" ];then
		echo 1
		return 1
	fi 
	servername="$1"
	sed -i '/\#\s*HTTPS\s*server\b/,/\#\s*HTTPS\s*server\b/s/[^#][ |  |   |    |\t]*\bserver_name\b.*/\tserver_name\t'$servername';/' "$httpconf"
	if [ $? -ne 0 ];then
		echo 1
		return 1
	else
		echo 0
		return 0
	fi	
}

noontec_getsystemunicodelist()
{
	echo CP936 CP950 CP866 CP932 CP949 ASCII ISO8859-7 ISO8859-1 ISO8859-2 UTF-8
}

noontec_getcurrentsystemunicode()
{
	langencodingfile="/mnt/base/etc/.ftpset.conf"

	langencoding=$(awk '/^UseEncoding/{print $3}' "$langencodingfile")
	if [ -z "$langencoding" ];then
		echo UTF-8
	else
		echo ${langencoding}
	fi	
}

noontec_setsystemunicode()
{
	langencodingfile="/mnt/base/etc/.ftpset.conf"

	if [ ! -z "$1" ];then
		langencoding="$1"
	else
		langencoding="UTF-8"
	fi
	sed -i '/\bUseEncoding\b.*/d' "$langencodingfile"
	if [ $? -eq 0 ];then
		echo "UseEncoding UTF-8 ${langencoding}" >> "$langencodingfile"
		if [ $? -eq 0 ];then
			echo 0
			return 0
		fi
	fi
	echo 1
	return 1
}

noontec_systemuptime()
{
	uptimeinfo=$(uptime |awk -F"," '{print $2 $3}')
	echo "$uptimeinfo"
}
#hh:mm[:ss]
#[YYYY.]MM.DD-hh:mm[:ss]
#YYYY-MM-DD hh:mm[:ss]
#[[[[[YY]YY]MM]DD]hh]mm[.ss]

noontec_getsystime()        
{                             
	mydate=$(date +"$1")  
	if [ "$?" -eq 0 ]     
	then                  
		echo $mydate  
	else                  
		echo 1       
		return 1                                                                                            
	fi                    
	return 0                
}                        
noontec_setsystime()   
{                                 
	mydate=$(date -s "$1")             
	if [ "$?" -eq 0 ]     
	then                              
		echo 0
		return 0                                                                                            
	else          
		echo 1   
		return 1                                                                                            
	fi                                                                                                   
}

noontec_autointernetnetsystime()   
{                    
	if [ -z "$1" ];then
		echo 1
		return 1
	fi             
	#ntpdate -t 1 time.windows.com > /dev/null 2>&1
	ntpdate -t 1 "$1" > /dev/null 2>&1
	if [ "$?" -ne 0 ];then    
		echo 1
		return 1
	else
		echo 0 
		return 0                                                                                            
	fi                  
}

noontec_settimezone()
{
	if [ -r "/usr/share/zoneinfo/$1" ]
	then
		cp -rf /usr/share/zoneinfo/$1  /etc/localtime > /dev/null 2>&1
		if [ "$?" -ne 0 ]     
		then
			echo 1   
			return 1
		fi
	fi
	
	echo 0
	return 0

}

#tar -cjvf base.tar.bz2 /mnt/base/*
#tar cjvf /mnt/work/tmp/pt.tar.bz2 PT_CONFIG/*
#date +"%Y%m%d%H%M%S"
noontec_bakupfile()
{
	if [ -z "$1" ] || [ -z "$2" ];then
		echo 1
		return 1
	fi

	srcfilenamepath="$1"
	bakupedfilenamepath="$2"
	tar_type="$3"

	#srcfilename="$(basename $srcfilenamepath)"
	#srcfiledir="$(dirname $srcfilenamepath)"
	srcfiledir="$srcfilenamepath"

	#filetype=$(expr substr "$(ls -ld "$bakupedfilenamepath" > /dev/null 2>&1)" 1 1)
	#echo filetype:$filetype
	#if [ $? -ne 0 ];then
	#	echo 1
	#	return 1
	#fi
	#if [ "$filetype" = "d" ];then
	#	bakupedfilename="$srcfilename"
	#	bakupeddir="$bakupedfilenamepath"
	#else
	#	bakupedfilename="$(basename $bakupedfilenamepath)"
	#	bakupeddir="$(dirname $bakupedfilenamepath)"
	#fi
	bakupedfilename="$(basename $bakupedfilenamepath)"
	bakupeddir="$(dirname $bakupedfilenamepath)"
	if [ ! -d  "${bakupeddir}" ];then
		mkdir -p "${bakupeddir}"
		if [ $? -ne 0 ]
		then
			echo 1
			return 1
		fi
	fi
	ls -d "$srcfilenamepath" > /dev/null 2>&1
	if [ $? -ne 0 ]
	then
		echo 1
		return 1
	fi

	tarparms=""
	suffix=""
	case $tar_type
	in
		1)
			tarparms="-czvf"	
			suffix="tar.gz"
			;;
		*)
			tarparms="-cjvf"	
			suffix="tar.bz2"
			;;
	esac
##############
	#echo tarparms:$tarparms,bakupeddir:$bakupeddir,bakupedfilename:$bakupedfilename,srcfilename:$srcfilename
	cd "${srcfiledir}" > /dev/null 2>&1
	if [ $? -ne 0 ];then
		if [ -e "$srcfilenamepath" ];then
			srcfilename="$(basename $srcfilenamepath)"
			srcfiledir="$(dirname $srcfilenamepath)"
			cd "${srcfiledir}" > /dev/null 2>&1
			if [ $? -ne 0 ];then
				echo 1
				return 1
			else
				tar "${tarparms}" "${bakupeddir}/${bakupedfilename}.${suffix}" "$srcfilename"  > /dev/null 2>&1
			fi
		else
			echo 1
			return 1
		fi
	else
		#tar "${tarparms}" "${bakupeddir}/${bakupedfilename}.$(date +"%Y%m%d%H%M%S").${suffix}" "." # > /dev/null 2>&1
		tar "${tarparms}" "${bakupeddir}/${bakupedfilename}.${suffix}" "."  > /dev/null 2>&1
	fi
#############	
	if [ $? -ne 0 ];then
		cd - > /dev/null 2>&1
		echo 1
		return 1
	else	
		cd - > /dev/null 2>&1  
		echo 0
		return 0
	fi
	
}
																												
noontec_restorefile()
{
	if [ -z "$1" ] || [ -z "$2" ];then
		echo 1
		return 1
	fi

	srcfilenamepath="$1"
	bakupedfilenamepath="$2"
	tar_type="$3"

	#srcfilename="$(basename $srcfilenamepath)"
	#srcfiledir="$(dirname $srcfilenamepath)"
	#bakupedfilename="$(basename $bakupedfilenamepath)"
	#bakupeddir="$(dirname $bakupedfilenamepath)"
	
	if [ ! -f "$srcfilenamepath" ];then
		echo 1
		return 1
	fi

	#filetype=$(expr substr "$(ls -ld "$srcfilenamepath" > /dev/null 2>&1)" 1 1)
	#if [ ! "${filetype} ] || [ $? -ne 0 ] 
	#then
	#	echo 1
	#	return 1
	#fi	
	
	if [ ! -d  "${bakupedfilenamepath}" ];then
		mkdir -p "${bakupedfilenamepath}"
		if [ $? -ne 0 ]
		then
			echo 1
			return 1
		fi
	fi
	cd "${bakupedfilenamepath}" > /dev/null 2>&1
	if [ $? -ne 0 ];then
		echo 1
		return 1
	fi
	
	tarparms=""
	suffix=""
	case $tar_type
	in
		1)
			tarparms="-xvf"	
			tar "${tarparms}" "${srcfilenamepath}" -C .  > /dev/null 2>&1
			;;
		2)
			tarparms="-y x"	
			unrar $tarparms "${srcfilenamepath}" .  > /dev/null 2>&1
			;;
		3)
			tarparms="-o -x"	
			unzip $tarparms "${srcfilenamepath}" -d .  > /dev/null 2>&1
			;;
		
		*)
			tarparms="-xvf"	
			tar "${tarparms}" "${srcfilenamepath}" -C .  > /dev/null 2>&1
			;;
	esac
	#echo tarparms:$tarparms,srcfilenamepath:$srcfilenamepath,bakupedfilenamepath:$bakupedfilenamepath
	if [ $? -ne 0 ];then
		cd -	> /dev/null 2>&1
		echo 1
		return 1
	else	
		cd -	> /dev/null 2>&1
		echo 0
		return 0
	fi
	
}
noontec_usbdiskbak()
{
	srcusbdisk="$1"
	destusbdisk="$2"
	if [ -z "$srcusbdisk" ] || [ -z "$destusbdisk" ];then
		echo 1
		return 1
	fi
	
	ls -d "$srcusbdisk" > /dev/null 2>&1
	if [ $? -ne 0 ];then
		echo 1
		return 1
	fi
	bakupedfilename="$(basename $destusbdisk)"
	bakupeddir="$(dirname $destusbdisk)"
	if [ ! -d $bakupeddir ];then
		mkdir -p $bakupeddir
		if [ $? -ne 0 ];then
			echo 1
			return 1
		fi
	fi
	dd if=$srcusbdisk of=$destusbdisk bs=1M
	if [ $? -ne 0 ];then
		echo 1
		return 1
	fi
	return 0
}																												
noontec_usbdiskrestore()
{
	srcusbdisk="$1"
	destusbdisk="$2"
	if [ -z "$srcusbdisk" ] || [ -z "$destusbdisk" ];then
		echo 1
		return 1
	fi
	
	ls -d "$srcusbdisk" > /dev/null 2>&1
	if [ $? -ne 0 ];then
		echo 1
		return 1
	fi
	bakupedfilename="$(basename $destusbdisk)"
	bakupeddir="$(dirname $destusbdisk)"
	ls -d "$bakupeddir" > /dev/null 2>&1
	if [ $? -ne 0 ];then
		echo 1
		return 1
	fi
	dd if=$srcusbdisk of=$destusbdisk bs=1M
	if [ $? -ne 0 ];then
		echo 1
		return 1
	fi

	echo 0
	return 0
}																												
noontec_updatediskstandbyconfigfile()
{
	filename=$1
	if [ ! -f "$filename" ]
	then
		echo 1
		return 1
		
	fi
	cat "${filename}" |grep '^[^#.*]'|grep -v '^\s*#.*'|grep "General\|cycle-time\|idle-time\|syslog\|command" > "/tmp/.spindown" &&	smartctl --scan |awk 'function check_hdd_standby(devicename) { printf"\n[Disk %d]\nname = %s\nspindown = 1\n",myindex,devicename} BEGIN{FS="[\t ]";myindex=0} {check_hdd_standby(substr($1,6,4));myindex+=1}END{printf "\n# Because of a bug in iniParser you should leave an empty line at the bottom of this file.\n\n"}' >> "/tmp/.spindown" && mv "/tmp/.spindown"  "${filename}"
	if [ $? -ne 0 ]
	then
		echo 1
		return 1
	else	
		echo 0
	fi
	return 0
	 
}

noontec_getnetdevlist()
{
	devfilepath="/proc/net/dev"
	devnmlst="$(awk -F":" '$0 ~/:/ && $0 !~/lo/ && $0 !~/sit0/ && $0 !~ /ipddp/ && $0 !~ /\w*\d*\.\d*/ {gsub(/:/,"",$1);print $1}' "$devfilepath" 2> /dev/null)"
	if [ $? -eq 0 ]
	then
		echo $devnmlst
		return 0
	else
		echo 1
		return 1
	fi
	
}

noontec_getnetinfo1()
{
	#ifconfig |awk '/Link encap/{print $1}'|awk '$0 !~/lo/' |while read devnm
	devfilepath="/proc/net/dev"
	awk -F":" '$0 ~/:/ && $0 !~/lo/ && $0 !~/sit0/ && $0 !~/ipddp.*/ && $0 !~ /\w*\d*\.\d*/ {gsub(/:/,"",$1);print $1}' "$devfilepath" |while read devnm
	do
		ifconfig "$devnm" up > /dev/null 2>&1
		ifconfig $devnm |grep -q "UP BROADCAST.*RUNNING" > /dev/null 2>&1
		if [ $? -eq 0 ]
                then
			status="up"
                else
			status="down"
		fi
		ip="$(ifconfig $devnm|awk '/inet addr/ {print $2}'|awk -F: '{print $2}')"
		if [ -z $ip ];then ip="-";fi
		mac="$(ifconfig $devnm|awk '/HWaddr/{print $5}')"
		if [ -z $mac ];then mac="-";fi
		RX="$(ifconfig $devnm|awk  '/RX packets:/ {print $2}'|awk -F: '{print $2}')"
		if [ -z $RX ];then RX="-";fi
		TX="$(ifconfig $devnm|awk  '/TX packets:/ {print $2}'|awk -F: '{print $2}')"
		if [ -z $TX ];then TX="-";fi
		error="$(ifconfig $devnm|awk  '/errors:/ {print $3}'|awk -F: 'BEGIN{Total=0}{Total+=$2}END{print Total}')"
		if [ -z $error ];then error="-";fi
		echo -e "$devnm $status $ip $mac $RX $TX $error|"
	done      
}

noontec_getnetinfo2()
{
	networkconf="/etc/network"
	devfilepath="/proc/net/dev"
	awk -F":" '$0 ~/:/ && $0 !~/lo/ && $0 !~/sit0/ && $0 !~/ipddp.*/ && $0 !~ /\w*\d*\.\d*/ {gsub(/:/,"",$1);print $1}' "$devfilepath" |while read devnm
	do
		ifconfig "$devnm" up > /dev/null 2>&1
		speed="$(ethtool $devnm|awk -F":" '/Speed:/{print $2}')"
		if [ -z $speed ];then speed="-";fi
		dhcp="$(confoper "${networkconf}" $devnm DHCP)"
		if [ -z $dhcp ];then dhcp="-";fi
		ifconfig $devnm |grep -q "UP BROADCAST.*RUNNING" > /dev/null 2>&1
		if [ $? -eq 0 ]
                then
			status="up"
                else
			status="down"
		fi
		ip="$(ifconfig $devnm|awk '/inet addr/ {print $2}'|awk -F: '{print $2}')"
		if [ -z $ip ];then ip="-";fi
		netmask="$(ifconfig $devnm|awk '/Mask/ {print $4}'|awk -F: '{print $2}')"
		if [ -z $netmask ];then netmask="-";fi
		gateway="$(route -n|grep "0.0.0.0.*UG.*$devnm"|awk '{print $2}')"
		if [ -z $gateway ];then gateway="-";fi
		mac="$(ifconfig $devnm|awk '/HWaddr/{print $5}')"
		if [ -z $mac ];then mac="-";fi
                RX="$(ifconfig $devnm|awk  '/RX packets:/ {print $2}'|awk -F: '{print $2}')"
                if [ -z $RX ];then RX="-";fi
                TX="$(ifconfig $devnm|awk  '/TX packets:/ {print $2}'|awk -F: '{print $2}')"
                if [ -z $TX ];then TX="-";fi
		mtu="$(ifconfig $devnm|awk -F":" '/MTU:/{print $2}'|awk '{print $1}')"
		if [ -z $mtu ];then ntu="-";fi
		echo -e "$devnm,$dhcp,$ip,$netmask,$gateway,$mac,$speed,$mtu,$RX,$TX,$status|"
	done      
}

noontec_getdefaultgateway()
{
	local retnum=""
	local retdev=""
	local outfile="/tmp/.defaultnetdev"
	route -n | grep UG > ${outfile}
	while read line
	do
		metric=`echo ${line} |awk '{print $5}'`
		devname=`echo ${line} |awk '{print $8}'`
		#echo "======Metric:${metric}======Devname:${devname}======="
		if [ "XXX${retnum}" == "XXX" ] || [ ${metric} -le ${retnum} ]; then
			retnum=${metric}
			retdev=${devname}
		fi
	done < ${outfile}
	rm -fr ${outfile}
	echo "$retdev"
	return 0
}

noontec_setdefaultgateway()
{
        netdev=$1                   
        /usr/init.d/setgw ${netdev}
	return 	0
}

noontec_getnetrate() 
{
	devnm="$1"
	if [ -z "$devnm" ];then echo 1;return 1;fi

	speed="$(ethtool $devnm 2>/dev/null |awk -F":" '/Speed:/{print $2}' 2>/dev/null)"
	duplex="$(ethtool $devnm 2>/dev/null |awk -F":" '/Duplex:/{print $2}' 2>/dev/null)"
	if [ -z "$speed" ];then speed="-";fi
	if [ -z "$duplex" ];then duplex="-";fi
	echo $speed $duplex 
	
	return 0
}
#mode----0/auto;1/100mps/full;2/1000mps/full;3/unknown
noontec_getnetspeed() 
{
	devnm="$1"
	if [ -z "$devnm" ];then echo 1;return 1;fi

	negotiation="$(ethtool eth0 2>/dev/null |awk -F":" '/Auto-negotiation:/{print $2}' 2>/dev/null)"
	speed="$(ethtool $devnm 2>/dev/null |awk -F":" '/Speed:/{print $2}' 2>/dev/null)"
	duplex="$(ethtool $devnm 2>/dev/null|awk -F":" '/Duplex:/{print $2}' 2>/dev/null)"

	#if [ -z $speed ];then speed="-";fi
	#if [ -z $duplex ];then duplex="-";fi
	#if [ "$speed" = "-" -o "$duplex" = "-" ];then echo 0;return 0;fi
	
	if [ "$speed" = " 100Mb/s" -a "$duplex" = " Full" ] || [ "$speed" = " 100Mb/s" -a "$duplex" = " FULL" ] || [ "$speed" = " 100Mb/s" -a "$duplex" = " full" ]
	then 
		echo 1
		return 1
	fi
	if [ "$speed" = " 1000Mb/s" -a "$duplex" = " Full" ] || [ "$speed" = " 1000Mb/s" -a "$duplex" = " FULL" ] || [ "$speed" = " 1000Mb/s" -a "$duplex" = " full" ]
	then 
		echo 2
		return 0
	fi
	#if [ "$negotiation" = "on" -o "$negotiation" = "ON" -o "$negotiation" = "On" ];then echo 0;return 0;fi
	#unkown	
	
	echo 0
	return 0
}

noontec_setnetspeed() 
{
	devnm="$1"
	mode="$2"
	
	if [ -z "$devnm" ];then echo 1;return 1;fi
	if [ "$mode" -eq 1 ]
	then
		noontec_execcommandstrret "ethtool -s $devnm autoneg off"  "Invalid"
		if [ "$?" -ne 0 ];then echo 1;ethtool -s $devnm autoneg on 2>/dev/null;return 1;fi
		noontec_execcommandstrret "ethtool -s $devnm speed 100" "Invalid"
		if [ "$?" -ne 0 ];then echo 1;ethtool -s $devnm autoneg on 2>/dev/null;return 1;fi
		noontec_execcommandstrret "ethtool -s $devnm duplex full" "Invalid" 
		if [ "$?" -ne 0 ];then echo 1;ethtool -s $devnm autoneg on 2>/dev/null;return 1;fi
	elif [ "$mode" -eq 2 ]
	then
		noontec_execcommandstrret "ethtool -s $devnm autoneg off" "Invalid"
		if [ "$?" -ne 0 ];then echo 1;ethtool -s $devnm autoneg on 2>/dev/null;return 1;fi
		noontec_execcommandstrret "ethtool -s $devnm speed 1000" "Invalid"
		if [ "$?" -ne 0 ];then echo 1;ethtool -s $devnm autoneg on 2>/dev/null;return 1;fi
		noontec_execcommandstrret "ethtool -s $devnm duplex full" "Invalid"
		if [ "$?" -ne 0 ];then echo 1;ethtool -s $devnm autoneg on 2>/dev/null;return 1;fi
	else
		noontec_execcommandstrret "ethtool -s $devnm autoneg on" "Invalid"
		if [ "$?" -ne 0 ];then echo 1;ethtool -s $devnm autoneg on 2>/dev/null;return 1;fi
	fi
	
	echo 0	
	return 0
}

noontec_setbonding()
{
	networkconf="/etc/network"
	devfilepath="/proc/net/dev"
	
	bondidx="$1"
	bondmode="$2"
	
	#echo bondidx:$bondidx;bondmode:$bondmode
	mode="$(confoper "${networkconf}" "$bondidx" "BONDMODE" "$bondmode")"
	if [ $? -ne 0 ]
	then
		echo 1
		return 1
	else
		echo 0
		return 0
	fi
}

noontec_setbondswitch()
{
	networkconf="/etc/network"
	devfilepath="/proc/net/dev"
	bondidx="$1"
	bondswitch=$2
	
	retstr="$(confoper "${networkconf}" "$bondidx")"
	echo retstr:$retstr,bondidx:$bondidx,bondswitch:$bondswitch
	if [ -z "$retstr" -a "$bondswitch" = "yes" ] || [ -z "$retstr" -a "$bondswitch" = "YES" ] || [ -z "$retstr" -a "$bondswitch" = "Yes" ] 
	then
		confaddsection "$networkconf" "$bondidx" > /dev/null 2>1&
		if [ $? -ne 0 ]
		then
			echo 1
			return 1
		fi	
		confadditem "$networkconf" "$bondidx" "BONDMODE" "0" > /dev/null 2>1&  
		if [ $? -ne 0 ]
		then
			echo 1
			return 1
		fi	
		confoper "$networkconf" "$bondidx" "DHCP" "NO" > /dev/null 2>1&  
		if [ $? -ne 0 ]
		then
			echo 1
			return 1
		fi	
	else
		#confdelsection "$networkconf" "$bondidx"	
		confoper "$networkconf" "$bondidx" "BONDSW" "$bondswitch"
	fi	
	
	#confoper "${networkconf}" "$bondidx" "BONDSW" "$bondswitch"
	echo 0
	return 0
}

noontec_getbondinglst()
{
	mode0="load balancing (round-robin)"
	mode1="fault-tolerance (active-backup)"
	mode2="load balancing (xor)"	
	mode3="fault-tolerance (broadcast)"
	mode4="IEEE 802.3ad Dynamic link aggregation"
	mode5="transmit load balancing"
	mode6="adaptive load balancing"	
	echo "$mode0:$mode1:$mode2:$mode3:$mode4:$mode5:$mode6"
}
noontec_getbondingstatus()
{
	devfilepath="/proc/net/dev"
	devnmlst="$(awk '$0 ~/:/ && $0 ~/bond/{gsub(/:/,"",$1);print $1}' "$devfilepath" 2> /dev/null)"
	if [ ! -z "$devnmlst" ] && [ $? -eq 0 ]
	then
		echo on
		return 0
	elif [ -z "$devnmlst" ] && [ $? -eq 0 ]
	then
		echo off
		return 0
	else
		echo 1
		return 1
	fi
	return 0
}

noontec_getmemintraffic()
{
	sync && echo 1 > /proc/sys/vm/drop_caches
	tmpfile="/tmp/.tmp.mem"
	while [ 1 ]
	do
	{
		free |awk '/Mem/'|while read line
		do
			echo $line | awk '{if($2 > 0){printf "%.2f%%\n",(($3*100)/$2)}}' >> "$tmpfile" 
		done
		usleep 1000000
		busybox sed -i '1,$d' "$tmpfile"
	}
	done
	
}

noontec_getmeminfobypersent()
{
	sync && echo 1 > /proc/sys/vm/drop_caches
	tmpfile="/tmp/.tmp.mem.info"
	memfile="/tmp/.tmp.mem"
	
	if [ ! -f "$tmpfile" ];then
		touch $tmpfile
	fi	

	if [ ! -f "$memfile" ];then
		touch $memfile
	fi	
	
	cnt="$(free |awk '/Mem/'|awk '{print FNR}')"
	
	second="$1"
	
	if [ -z "$second" ]
	then
		second=0
	fi

	if [ "$second" -le 1 ]
	then
		busybox sed -i '1,$d' "$tmpfile"

	elif [ "$(($(awk 'END{print FNR}' "$tmpfile") / ${cnt}))" -ge $second ]
	then	
		while [ $cnt -gt 0 ]
		do
			busybox sed -i '1d' "$tmpfile"
	                cnt=`expr $cnt - 1`
	        done
	fi

	while read line
	do
		echo $line | awk -F":" '{print $1,$2,$3}' >> "$tmpfile"
	done < "${memfile}"
	cat "$tmpfile"
}
	
#noontec_getmeminfobypersent()
#{
#	tmpfile="/usr/bin/tmp.mem.info"
#	second="$1"
#	#cnt="$(($(awk 'END{print FNR}' $tmpfile) / 2))"
#	
#	if [ -z "$second" ]
#	then
#		second=0
#	fi
#
#	if [ "$second" -le 1 ]
#	then
#		busybox sed -i '1,$d' "$tmpfile"
#	#elif [ "$(($(awk 'END{print FNR}' $tmpfile) / 2))" -ge $second ]
#	elif [ "$(awk 'END{print FNR}' $tmpfile)" -ge $second ]
#	then	
#		busybox sed -i '1d' "$tmpfile"
#	fi
#	
#	free |awk '/Mem/'|while read line
#	do
#		#echo $line | awk '{print $2,$3,$4}' >> "$tmpfile"
#		#echo $line | awk '{print $2,$3,$4}'
#		echo $line | awk '{if($2 > 0){printf "%.2f\n",(($3*100)/$2)}}' >> "$tmpfile" 
#	done
#	cat "$tmpfile"
#} 

noontec_getmeminfobysize()
{
	sync && echo 1 > /proc/sys/vm/drop_caches
	free -m|awk '/Mem/||/Swap/'|while read line
	do
		echo $line | awk '{print $2,$3,$4}'
	done
}
 
noontec_networktraffic()
{
	export trafficinfofile="/tmp/.tmp.traffic"
	awk -vtrafficinfofile=$trafficinfofile 'BEGIN{
	devfile="/proc/net/dev";
	while( getline < devfile)
	{
		if($0 ~ /:/ && $0 !~/lo/ && ($10+0) > 0)
		{
			#print "data0:" $0
			split($1,tarrout,":");
			netout[tarrout[1]]=$10+tarrout[2];
			#print tarrout[1] " out:" netout[tarrout[1]];
			split($1,tarrin,":");
			netin[tarrin[1]]=$2+tarrin[2];
			#print tarrin[1] " in:" netin[tarrin[1]];
		}
	}
	close(devfile);
	#for(i=1;i<20;i++) 
	for(;1;) 
	{
		#print "i:" i
		system("usleep 1000000")
		while( getline < devfile)
		{
			if($0 ~ /:/ && $0 !~/lo/ && ($10+0) > 0)
			{
				#print "data1:" $0
				split($1,tarrout,":");
				if(tarrout[1] in netout)
				{
					#print tarrout[1] " out:" $10+trarrout[2] "," netout[tarrout[1]] 
					#print tarrout[1]," out:",($10+tarrout[2]-netout[tarrout[1]])*8/1024,"kb/s";
					outrate=($10+tarrout[2]-netout[tarrout[1]])*8/1024
					netout[tarrout[1]]=$10+tarrout[2];
				}      
				split($1,tarrin,":");
				if(tarrin[1] in netin)
				{
					#print tarrin[1] " in:" $2+tarrin[2] "," netin[tarrin[1]] 
					#print tarrin[1]," in:",($2+tarrin[2]-netin[tarrin[1]])*8/1024,"kb/s";
					inrate=($2+tarrin[2]-netin[tarrin[1]])*8/1024
					netin[tarrin[1]]=$2+tarrin[2];
				}
				#print tarrout[1] ":" outrate "kb/s"  ":" inrate "kb/s"
				#system("/usr/bin/test.sh" $tarrout[1] $outrate $inrate);
				print tarrout[1] ":" outrate "kb/s"  ":" inrate "kb/s" > trafficinfofile;
			} 
		}
		close(devfile);
		close(trafficinfofile);
	}
	#system("echo  > /tmp/.tmp.traffic");
	}' #> /tmp/.tmp.traffic	
	echo "" > "$trafficinfofile"
}

noontec_getnetworkinfo()
{
	tmpfile="/tmp/.tmp.traffic.info"
	trafficinfofile="/tmp/.tmp.traffic"
	devfile="/proc/net/dev";
	
	if [ ! -f "$tmpfile" ];then
		touch "$tmpfile"
	fi
	if [ ! -f "$trafficinfofile" ];then
		touch "$trafficinfofile"
	fi
	cnt="$(awk '$0 ~/:/ && $0 !~/lo/ && ($10+0) > 0 {print $1}' "$devfile" |awk 'END{print FNR}')"
	
	second="$1"

	if [ -z "$second" ]
	then
		second=0
	fi

	if [ "$second" -le 1 ]
	then
		busybox sed -i '1,$d' "$tmpfile"
	elif [ "$(($(awk 'END{print FNR}' "$tmpfile") / ${cnt}))" -ge $second ]
	then	
		while [ $cnt -gt 0 ]
		do
			busybox sed -i '1d' "$tmpfile"
	                cnt=`expr $cnt - 1`
	        done
	fi

	while read line
	do
		echo $line | awk -F":" '{print $1,$2,$3}' >> "$tmpfile"
	done < "${trafficinfofile}"
	cat "$tmpfile"
} 

noontec_getcputemp()
{
	if [ -f /sys/class/hwmon/hwmon0/device/temp_input ];then
		cat /sys/class/hwmon/hwmon0/device/temp_input
	fi
}

noontec_getdisktemp()
{
	for diskinfopath_ii in /sys/block/sd*
	do 
		#echo $diskinfopath_ii
		diskindex=$(readlink "$diskinfopath_ii" 2>/dev/null|awk -F"/" '/target1:/{print $5}'|awk -F":" '{print $3}')
		if [ $? -eq 0 ];then
			if [ ! -z "$diskindex" ];then
				if [ $? -eq 0 ];then
					devpath=$(basename "${diskinfopath_ii}")
					#echo devpath:$devpath,lininfo:$lininfo	
					#smartctl -A -d marvell /dev/${devpath} |awk '/Temperature_Celsius/{print $4}'
					smartctl -A -d marvell /dev/${devpath} |awk 'BEGIN{temp=0;}/Temperature_Celsius/{if("0"==substr($10,1,1)){temp=substr($10,2,2);}else{temp=$10;};print '"$diskindex"' ":" temp}'
				fi
			fi
		fi	
	done
}
noontec_getpidinfo()
{
	#PID  PPID USER %MEM %CPU COMMAND
	pid_file="/tmp/.tmp.pid.info"
	/bin/top - n1 c b s o COMMAND | grep -v top | grep -v sleep > $pid_file
}

noontec_getcpucorecnt()
{
	statfile="/proc/stat"
	cnt=$(awk '$1~/cpu[0-9]+/' "$statfile"|awk 'END{print NR}')
	if [ -z "$cnt" ];then
		echo 0
	else
		echo $cnt
	fi
}
noontec_getcoretraffic()
{
	core_file="/tmp/.tmp.core"
	statfile="/proc/stat"
	sleepsec="$1"
	##echo user nice system idle iowait irq softirq
	rm -rf $core_file
	cnt=$(awk '$1~/cpu[0-9]+/' "$statfile"|awk 'END{print NR}')
	#cnt=4;while [ $cnt lt 0 ];do echo cnt:$cnt;cnt=$(($cnt - 1));done 
	shell_i=0
	while [ $shell_i -lt $cnt ]
	do
		printf "cpu%d:%.2f%%\n" $shell_i 0 >> $core_file
		shell_i=$(($shell_i + 1))
	done	
	while [ 1 ]
	do
	{
		cnt=0
		awk '$1~/cpu[0-9]+/' "$statfile" | while read line
		do
			#cnt=$(awk '$1~/cpu[0-9]+/' /usr/bin/stat|awk 'END{print NR}')
			#CPULOG_1=$(echo $line | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
			CPULOG_1=$(awk '$1~/cpu'$cnt'/' "$statfile" | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
			sys_idle_1=$(echo $CPULOG_1 | awk '{print $4}')
			total_1=$(echo $CPULOG_1 | awk '{print $1+$2+$3+$4+$5+$6+$7}')

			echo "$sleepsec" |egrep -q "^[0-9]+$"                                                    
			if [ $? -eq 0 ]                                                                          
			then
				sleep $sleepsec
			else                                                                                     
				sleep 3                                                                          
			fi  

			#CPULOG_2=$(echo $line | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
			CPULOG_2=$(awk '$1~/cpu'$cnt'/' "$statfile" | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
			sys_idle_2=$(echo $CPULOG_2 | awk '{print $4}')
			total_2=$(echo $CPULOG_2 | awk '{print $1+$2+$3+$4+$5+$6+$7}')

			total=$(( $total_2 - $total_1  ))
			totalidle=$(( $sys_idle_2 - $sys_idle_1 ))
			#echo sys_idle_1:$sys_idle_1,total_1:$total_1
			#echo sys_idle_2:$sys_idle_2,total_2:$total_2
			#echo totalidle:$totalidle
			#echo total:$total
			#echo cnt:$cnt
			if [ $total -eq 0 ]
			then
				info=$(printf "cpu%d:%.2f%%\n" $cnt 0) 
			else
				info=$(printf "cpu%d:%.2f%%\n" $cnt $(( 100 * ( $total - $totalidle ) / $total ))) 
			fi
			busybox sed -i 's/cpu'$cnt':.*/'$info'/' "$core_file"
			
			cnt=$(( $cnt + 1))
		done
		#cat "$core_file"
		#usleep 1000000
		#busybox sed -i '1,$d' "$core_file"
	}
	done
}

noontec_getcoreinfo()
{
	tmpfile="/tmp/.tmp.core.info"
	core_file="/tmp/.tmp.core"
	cnt="$(awk '$1~/cpu[0-9]+/{print $1}' /proc/stat |awk 'END{print FNR}')"
	second="$1"
	
	if [ -z "$second" ]
	then
		second=0
	fi

	if [ "$second" -le 1 ]
	then
		busybox sed -i '1,$d' "$tmpfile"
	elif [ "$(($(awk 'END{print FNR}' "$tmpfile") / ${cnt}))" -ge $second ]
	then	
		while [ $cnt -gt 0 ]
		do
			busybox sed -i '1d' "$tmpfile"
	                cnt=`expr $cnt - 1`
	        done
	fi
	
	while read line
	do
		echo $line | awk -F":" '{print $1,$2,$3}' >> "$tmpfile"
	done < "${core_file}"
	cat "$tmpfile"
}


noontec_getcpuinfo()
{
	sleepsec=$1
	cputype=$2
	if [ -z "$sleepsec" ];then
		echo 1
		return 1
	fi
	if [ -z "$cputype" ];then
		cputype="cpu"
	fi
	
	##echo user nice system idle iowait irq softirq
	cpu_file="/tmp/.tmp.cpu.info"
	#CPULOG_1=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
	CPULOG_1=$(cat /proc/stat | grep "$cputype " | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
	sys_idle_1=$(echo $CPULOG_1 | awk '{print $4}')
	total_1=$(echo $CPULOG_1 | awk '{print $1+$2+$3+$4+$5+$6+$7}')

        echo "$sleepsec" |egrep -q "^[0-9]+$"                                                    
        if [ $? -eq 0 ]                                                                          
        then                                                                                     
                sleep $sleepsec                                                                  
        else                                                                                     
                sleep 3                                                                          
        fi  

	#CPULOG_2=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
	CPULOG_2=$(cat /proc/stat | grep "$cputype "| awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
	sys_idle_2=$(echo $CPULOG_2 | awk '{print $4}')
	total_2=$(echo $CPULOG_2 | awk '{print $1+$2+$3+$4+$5+$6+$7}')

	#echo Total_1:$Total_1 
	#echo Total_2:$Total_2 
	#echo SYS_IDLE_1:$SYS_IDLE_1
	#echo SYS_IDLE_2:$SYS_IDLE_2
	total=$(( $total_2 - $total_1  ))
	totalidle=$(( $sys_idle_2 - $sys_idle_1 ))
	#echo total:$total
	#echo totalidle:$totalidle
	#usage=$(( 100 * ( $total - $totalidle ) / $total ))
	#echo usage:$usage
	if [ $total -eq 0 ]
	then
		printf "%s:%.2f%%\n" "$cputype" 0
	else
		printf "%s:%.2f%%\n" "$cputype" $(( 100 * ( $total - $totalidle ) / $total ));
	fi
	
	#while [ 1 ]
	#do
	#{
	#	awk '$1~/cpu$/' /proc/stat | while read line
	#	do
	#		#echo "$line" |awk '{print $2,$3,$4,$5,$6,$7,$8}'
	#		#echo "$line" |awk '{total=$2+$3+$4+$5+$6+$7+$8;print total}'
	#		echo "$line" |awk '{total=$2+$3+$4+$5+$6+$7+$8;free=$5;	\
	#		printf "%.2f%% %.2f%%\n",(100*free)/total,(total-free)*100/total}'  > "$cpu_file"
	#		#printf "Free %.2f%%\nused %.2f%%\n",(100*free)/total,(total-free)*100/total}'  >> "$cpu_file"
	#		
	#	done
	#	cat "$cpu_file"
	#	#usleep 1000000
	#	#busybox sed -i '1,$d' "$cpu_file"
	#}
	#done
}

#noontec_serverstatus()
#{                    
#	export servername="$1"          
#        cnt="$(ps -aef |grep -v "grep"|grep -v "func_common"|grep  "$servername" |awk 'END{print FNR}')"
#        if [ $cnt -gt 0 ]
#        then
#	        echo "$servername running" 
#        else              
#	        echo "$servername stopped"
#	fi 
#	return 0
#} 

noontec_devvlanlist()
{
	vlaninfo="/proc/net/vlan/config"
	devnm=$1
	awk '$0 ~/:/ && $0 ~ /\w*\d*\.\d*/ {gsub(/:/,"",$1);print $1}' "$devfilepath" 2>/dev/null|grep "$devnm" |while read vlaninfo
	do
		echo $vlaninfo
	done	
	
	return 0
}                    


noontec_vlanserverstatus()
{
	vlaninfo="/proc/net/vlan/config"
	cnt="$(lsmod |grep -v "grep"|grep -v "func_common"|grep  "${modulevlan%%.ko}" |awk 'END{print FNR}')"
	if [ $cnt -gt 0 ]      
	then 
		echo on
	else
		echo off       
	fi       

	return 0         
}                    

noontec_vlanserverstart()
{
	vlaninfo="/proc/net/vlan/config"
	modulevlan="8021q.ko"
	cnt="$(lsmod |grep -v "grep"|grep -v "func_common"|grep  "${modulevlan%%.ko}" |awk 'END{print FNR}')"
	if [ $cnt -eq 0 ]      
	then 
		loadvlanmodule
		if [ $? -ne 0 ]
		then
			echo 1
			return 1
		fi
	fi       

	echo 0
	return 0         
}                    

noontec_vlanserverstop()
{
	vlaninfo="/proc/net/vlan/config"
	modulevlan="8021q.ko"
	cnt="$(lsmod |grep -v "grep"|grep -v "func_common"|grep  "${modulevlan%%.ko}" |awk 'END{print FNR}')"
	if [ $cnt -gt 0 ]      
	then 
		unloadvlanmodule
		if [ $? -ne 0 ]
		then
			echo 1
			return 1
		fi
	fi       

	echo 0
	return 0         
}                    

noontec_specificvlanadd()
{
	networkconf="/etc/network"
	devfilepath="/proc/net/dev"
	modulevlan="8021q.ko"
	devnm=$1
	vlanid=$2
	cnt="$(lsmod |grep -v "grep"|grep -v "func_common"|grep  "${modulevlan%%.ko}" |awk 'END{print FNR}')"
        if [ $cnt -eq 0 ]
	then
		echo 1
		return 1
	fi
	if [ -z "$devnm" ] || [ -z "$vlanid" ]
	then
		echo 1
		return 1
	fi
	

	retvlan="$(confoper "${networkconf}" "$devnm" "VLANID")"
	if [ ! -z "$ret" ];then	
		vconfig rem "$devnm"."$retvlan"
		if [ $? -ne 0 ]
		then
			echo 1
			return 1
		fi

		confoper "${networkconf}" "$devnm" "VLANID" "$vlanid"
		if [ $? -ne 0 ]
		then
			echo 1
			return 1
		fi
	fi
	echo retvlan:$retvlan
	
	vconfig add "$devnm" "$vlanid" 
	if [ $? -ne 0 ]
	then
		echo 1
		return 1
	fi

	vconfig set_flag "$devnm"."$vlanid" 1 1
	if [ $? -ne 0 ]
	then
		echo 1
		return 1
	fi
	
	if [ -z "$retvlan" ];then
		confadditem "${networkconf}" "$devnm" "VLANID" "$vlanid"
		if [ $? -ne 0 ]
		then
			vconfig rem "$devnm"."$vlanid"
			echo 1
			return 1
		fi
	fi
	
	ifconfig "$devnm"."$vlanid" up		
	
	echo 0
	return 0
			
}

noontec_specificvlanadd1()
{
	networkconf="/etc/network"
	devfilepath="/proc/net/dev"
	modulevlan="8021q.ko"
	devnm=$1
	vlanid=$2
	cnt="$(lsmod |grep -v "grep"|grep -v "func_common"|grep  "${modulevlan%%.ko}" |awk 'END{print FNR}')"
        if [ $cnt -eq 0 ]
	then
		echo 1
		return 1
	fi
	if [ -z "$devnm" ] || [ -z "$vlanid" ]
	then
		echo 1
		return 1
	fi

	grep -q -w  "$devnm.$vlanid" "$devfilepath"
	if [ $? -eq 0 ]
	then
		vconfig rem "$devnm"."$vlanid"
		if [ $? -ne 0 ]
		then
			echo 1
			return 1
		fi
	fi

	vconfig add "$devnm" "$vlanid" 
	if [ $? -ne 0 ]
	then
		echo 1
		return 1
	fi
	
	vconfig set_flag "$devnm"."$vlanid" 1 1
	if [ $? -ne 0 ]
	then
		echo 1
		return 1
	fi

	ifconfig "$devnm"."$vlanid" up		
	
	echo 0
	return 0
			
}

noontec_specificvlandel()
{
	networkconf="/etc/network"
	devnm=$1
	vlanid=$2
	
	vconfig rem "$devnm"."$vlanid"
	if [ $? -ne 0 ]
	then
		echo 1
		return 1
	fi

	confdelitem "${networkconf}" "$devnm" "VLANID"
	if [ $? -ne 0 ]
	then
		echo 1
		return 1
	fi

	echo 0
	return 0
			
}

noontec_devvlandel()
{
	devfilepath="/proc/net/dev"
	devnm=$1
	awk '$0 ~/:/ && $0 ~ /\w*\d*\.\d*/ {gsub(/:/,"",$1);print $1}' "$devfilepath" 2>/dev/null|grep "$devnm" |while read vlaninfo
	do
		vconfig rem "$vlaninfo"
		if [ $? -ne 0 ]
		then
			echo 1
			return 1
		fi
		
		confdelitem "${networkconf}" "$devnm" "VLANID"
		if [ $? -ne 0 ]
		then
			echo 1
			return 1
		fi

	done	
	
	echo 0
	return 0
}

noontec_devvlandel1()
{
	devfilepath="/proc/net/dev"
	devnm=$1
	awk '$0 ~/:/ && $0 ~ /\w*\d*\.\d*/ {gsub(/:/,"",$1);print $1}' "$devfilepath" 2>/dev/null|grep "$devnm" |while read vlaninfo
	do
		vconfig rem "$vlaninfo"
		if [ $? -ne 0 ]
		then
			echo 1
			return 1
		fi
	done	
	
	echo 0
	return 0
}

noontec_allvlandel()
{
	devfilepath="/proc/net/dev"
	devnm=$1
	vlanid=$2
	awk '$0 ~/:/ && $0 ~ /\w*\d*\.\d*/ {gsub(/:/,"",$1);print $1}' "$devfilepath" 2>/dev/null|while read vlaninfo
	do
		#noontec_specificvlandel $vlaninfo
		vconfig rem "$vlaninfo"
		if [ $? -ne 0 ]
		then
			echo 1
			return 1
		fi
		
		confdelitem "${networkconf}" "$devnm" "VLANID"
		if [ $? -ne 0 ]
		then
			echo 1
			return 1
		fi

	done	
	
	echo 0
	return 0
}

noontec_getdyndnslastupdatetime()
{	
	tmpfile="/tmp/inadyn_time.cache"
	if [ -f "$tmpfile" ]
	then
		cat "$tmpfile" | awk '{print strftime("%Y-%m-%d %H:%M:%S",$1)}'
	fi
}

noontec_getdyndnsexternip()
{
	tmpfile="/tmp/inadyn_ip.cache"
	if [ -f "$tmpfile" ]
	then
		cat "$tmpfile" 
	fi
}


noontec_checkwifivalidation()
{
	wifidevif="wlan0"
	if [ ! -d /sys/class/net/"$wifidevif" ];then
		echo 1
		return 1
	fi

	if [ ! -d /sys/class/net/"$wifidevif" ];then
		echo 1
		return 1
	else
		echo 0
		return 0	
	fi
}

#0 = infrastructure (Managed) mode, i.e., associate with an AP (default)
#1 = IBSS (ad-hoc, peer-to-peer)
#2 = AP (access point)
noontec_getwirelssapmodelist()
{
	echo "infrastructure(Managed):IBSS(ad-hoc/peer-to-peer)"
}

noontec_setwirelssapmode()
{
	WPA_SUPPLICANT_CONF="/etc/wpa_supplicant/wpa_supplicant.conf"
	WPA_SUPPLICANT_INFO="/etc/wpa_supplicant/run/wpa_supplicant"
	wifidevif="wlan0"
	if [ ! -d /sys/class/net/"$wifidevif" ];then
		echo 1
		return 1
	fi

	wirelssapmode="$1"
	if [ -z "$wirelssapmode" ];then
		echo 1
		return 1
	fi
	wirelessnetworkid=$(wpa_cli -i "$wifidevif" -p "$WPA_SUPPLICANT" add_network)
	echo "$wirelessnetworkid"||grep -qE '^[0-9]+$'
	if [ $? -eq 0 ];then
		if [ "$wirelssapmode" -eq 0  -o "$wirelssapmode" -eq 1 ];then
			wpa_cli -i "$wifidevif" -p "$WPA_SUPPLICANT"  set_network "$wirelessnetworkid" mode "$wirelssapmode" > /dev/null 2>&1
			if [ $? -eq 0 ];then
				echo 0
				return 0
			fi
		fi
	fi
	
	echo 1
	return 1
}

#0 = WEP
#1 = WPA-PSK/WPA2-PSK
#2 None
noontec_getwirelssapencyptionlist()
{
	echo WEP:WPA-PSK/WPA2-PSK:OPEN
}

noontec_getwirelssapinfo()
{
	WPA_SUPPLICANT_CONF="/etc/wpa_supplicant/wpa_supplicant.conf"
	WPA_SUPPLICANT_INFO="/etc/wpa_supplicant/run/wpa_supplicant"
	WPA_SCAN_INFO="/tmp/wpascaninfo"
	wifidevif="wlan0"


	pids=$(pidof wpa_supplicant)
	if test "XXXXX$pids" == "XXXXX"
	then
		ifconfig $wifidevif  up
		if [ ! -f "$WPA_SUPPLICANT_CONF" ];then
			echo -e "ctrl_interface=$WPA_SUPPLICANT_INFO\nap_scan=1\nupdate_config=1\n" > "$WPA_SUPPLICANT_CONF"
			
		fi
		wpa_supplicant -Dwext -c  "$WPA_SUPPLICANT_CONF" -i $wifidevif -B > /dev/null 2>&1
		if [ $? -ne 0 ];then
			#echo wpa_supplicant start failed 
			echo "1" > "$WPA_SCAN_INFO"
			echo 1
			return 1
		fi
		sleep 1
	fi
	
	if [ ! -d /sys/class/net/"$wifidevif" ];then
		echo "1" > "$WPA_SCAN_INFO"
		echo 1
		return 1
	fi

	wpa_cli -i "$wifidevif" -p "$WPA_SUPPLICANT_INFO" scan  > /dev/null 2>&1
	if [ $? -ne 0 ];then
		echo "1" > "$WPA_SCAN_INFO"
		echo 1
		return 1
	fi	
	

	sleep 5
	wpa_cli -i "$wifidevif" -p "$WPA_SUPPLICANT_INFO" scan_results |grep -v "^bssid"  > "$WPA_SCAN_INFO" 2> /dev/null
	if [ $? -ne 0 ];then
		echo "1" > "$WPA_SCAN_INFO"
		echo 1
		return 1
	fi	

	echo 0	
	return 0
}

noontec_getwirelssaplinkedinfo()
{
	WPA_SUPPLICANT_CONF="/etc/wpa_supplicant/wpa_supplicant.conf"
	WPA_SUPPLICANT_INFO="/etc/wpa_supplicant/run/wpa_supplicant"
	WPA_SCAN_INFO="/tmp/wpascaninfo"
	wifidevif="wlan0"
	linkedssid="nolinked"
	
	wlanstatus=$(cat /sys/class/net/$wifidevif/carrier 2> /dev/null)
	
	if [ ! -z  "$wlanstatus" ];then
		if [ "$wlanstatus" -eq 1 ];then
			linkedssid=$(wpa_cli -i "$wifidevif" -p "$WPA_SUPPLICANT_INFO" 2>/dev/null list_networks|awk  'BEGIN{FS="\t"}/^0/{print $2}')
		fi
	fi
	
	echo $linkedssid
	
}

noontec_wirelssapdisconnect()
{
	cdev=$1
	/etc/udev/scripts/iw stop ${cdev}
	echo 0
	return 0
}

noontec_wirelssapconnect()
{
	cdev=$1
	/etc/udev/scripts/iw stop ${cdev}
	sleep 3
	/etc/udev/scripts/iw start ${cdev}
	echo 0
	return 0
}
