#!/bin/sh /etc/rc.common
# Copyright 2020 -2021 sirpdboy (herboy2008@gmail.com)

START=95
STOP=10

. /lib/functions.sh
EXTRA_COMMANDS="nstest"
EXTRA_HELP="	netspeedtest"
NS_FILE=/usr/share/netspeedtest/netspeedtest.txt
TMP_T=/tmp/netspeedtest
LOG="/var/log/netspeedtest.log"

limit_log() {
	local logf=$1
	[ ! -f "$logf" ] && return
	local sc=100
	[ -n "$2" ] && sc=$2
	local count=$(grep -c "" $logf)
	if [ $count -gt $sc ];then
		let count=count-$sc
		sed -i "1,$count d" $logf
	fi
}

init_env() {
	[ ! -f "$LOG" ] && echo "netspeedtest by sirpdboy" > $LOG
	echo 1 > $NS_FILE
   
}

nstest() {
    init_env
	[ -f "$TMP_T" ] && return
    echo 1 > $TMP_T
    echo -ne "\n  ————————————————————————————" | tee -a $LOG
    /etc/init.d/speedtest  | tee -a $NS_FILE
	ns_server=`cat  $NS_FILE | grep 'Hosted by' | cut -c10-`
	ns_down=`cat  $NS_FILE | grep 'Download:' |awk -F: '{printf $2}' `
	ns_up=`cat  $NS_FILE | grep 'Upload:' |awk -F: '{printf $2}' `
	echo -ne "\n  测服信息:${ns_server} " | tee -a $LOG
    echo -ne "\n  下行速率:${ns_down} ---- 上行速率:${ns_up} " | tee -a $LOG
	echo -ne "\n  测试时间: " | tee -a $LOG
	echo $(date +%Y-%m-%d" "%H:%M:%S) | tee -a $LOG
	rm -rf $TMP_T
}

reload(){
 	start
}

start() {
    limit_log $LOG 200
	nstest
}
