#!/bin/sh
# ãADSL·Զذκ
# ܣֵ֧ӵĶʱذμƻ
#		֧ذ־֧2048¼¼ڴֵʱԶµ1536¼

RAMDISK="/usr/ramdisk"
if [ -f ${RAMDISK}/PG.conf ]; then
        . ${RAMDISK}/PG.conf
        PGPATH="${RAMDISK}"
else
        . /etc/PG.conf
fi

LOG_RECORD_MAX=2048
LOG_REMOVE_MAX=1024

PGBIN=${PGPATH}/bin
FLOWEYE=${PGBIN}/floweye
EVENTFILE=${PGETC}/log/pending_events

plan_dir="${PGETC}/plan"
log_dir="${RAMDISK}/tmp/redial"
pppoeplan_conf="${plan_dir}/pppoeplan.conf"
pppoeplan_list="${plan_dir}/pppoeplan_list.conf"
pppoeplan_log="${log_dir}/redial.log"
pppoeplan_tmp_log="${log_dir}/redial_tmp.log"

mkdir -p ${plan_dir}
mkdir -p ${log_dir}
[ ! -f ${pppoeplan_conf} ] && touch ${pppoeplan_conf}
[ ! -f ${pppoeplan_list} ] && touch ${pppoeplan_list}
[ ! -f ${pppoeplan_log} ] && touch ${pppoeplan_log}

server()
{
        while true
        do
                if [ -f ${pppoeplan_conf} ]; then
			. ${pppoeplan_conf}

                	if [ "${enabled}" != "1" ]; then
                        	sleep 1
                        	continue
                	fi
		fi

		ntm=`date +%s`
		y="`date -r ${ntm} +%Y`"
		m="`date -r ${ntm} +%m`"
		d="`date -r ${ntm} +%d`"
		w="`date -d ${ntm} | cut -d' ' -f1`"
		h="`date -r ${ntm} +%H`"
		ms="`date -r ${ntm} +%M`"
				
		tm="${y}-${m}-${d} ${w} ${h}:${ms}"
                while read proxy_name disbled createtm chgtm year month day week hour minute remark othe
                do
                        [ "${disbled}" != "0" ] && continue
						#echo ${proxy_name} ${tm}
						
			[ "${year}" != "0000" -a "${year}" != "${y}" ] && continue
			[ "${month}" != "00" -a "${month}" != "${m}" ] && continue
			[ "${day}" != "00" -a "${day}" != "${d}" ] && continue
			[ "${week}" != "null" -a "${week}" != "${w}" ] && continue
			[ "${hour}" != "24" -a "${hour}" != "${h}" ] && continue
			[ "${minute}" != "60" -a "${minute}" != "${ms}" ] && continue

			log=`grep "^${proxy_name} ${tm} OK" ${pppoeplan_log}`
			[ "${log}" != "" ] && continue

			errmsg=`${FLOWEYE} nat setpppoe name=${proxy_name} redial=1`
			if [ "$?" != "0" ]; then
				log=`grep "^${proxy_name} ${tm} FAIL" ${pppoeplan_log}`
				[ "${log}" == "" ] && echo "${proxy_name} ${tm} FAIL" >> ${pppoeplan_log}
			else
				echo "${proxy_name} ${tm} OK" >> ${pppoeplan_log}
			fi

                done << EOF
`cfg_list`
EOF

		count=`cat ${pppoeplan_log} | wc -l`
		if [ ${count} -gt ${LOG_RECORD_MAX} ]; then
			tail -n ${LOG_REMOVE_MAX} ${pppoeplan_log} > ${pppoeplan_tmp_log}
			mv ${pppoeplan_tmp_log} ${pppoeplan_log}
		fi

                sleep 2
        done
}

cfg_list()
{
        grep -E "^.+ +[0-9] +[0-9]+ +[0-9]+ +[0-9]{4} +[0-9]{2} +[0-9]{2} +[a-zA-Z]{3,4} +[0-9]{2} +[0-9]{2}" ${pppoeplan_list}
}

cfg_add()
{
        local proxy_name="${1}"
        local disbled=${2}
        local year="${3}"
        local month="${4}"
        local day="${5}"
		local week="${6}"
        local hour="${7}"
        local minute="${8}"
        local remark="${9}"
		local createtm=`date +%s`
        local chgtm=${createtm}

		errarg=`echo ${*} | grep -E "^.+ +[0-9] +[0-9]{4} +[0-9]{2} +[0-9]{2} +[a-zA-Z]{3,4} +[0-9]{2} +[0-9]{2}"`
        if [ "${errarg}" = "" ]; then
                echo "arg err."
                exit 1
        fi
		
        local tmp="`cfg_list | grep "^${proxy_name} "`"

        if [ "${tmp}" != "" ]; then
                echo "exist."
                exit 1
        fi

        echo "${proxy_name} ${disbled} ${createtm} ${chgtm} ${year} ${month} ${day} ${week} ${hour} ${minute} ${remark}" >> ${pppoeplan_list}
}

cfg_del()
{
        local proxy_name=${1}

        if [ "${proxy_name}" = "" ]; then
                echo "arg err."
                exit 1
        fi

        men=`grep -v "^${proxy_name} " ${pppoeplan_list}`
        echo "${men}" > ${pppoeplan_list}
}

cfg_chang()
{
        local proxy_name="$1"
        local disbled=""
        local year=""
        local month=""
        local day=""
		local week=""
        local hour=""
        local minute=""
        local remark=""
        local chgtm=""

        if [ "${proxy_name}" = "" ]; then
                echo "arg err."
                exit 1
        fi

        local tmp="`cfg_list | grep "^${proxy_name} "`"
        
        if [ "${tmp}" = "" ]; then
                echo "not exist."
                exit 1
        fi

        eval "`echo "${tmp}" | sed "s/\([();']\)/\\\\\\\1/g" | \
			  awk '{print "proxy_name="$1" disbled="$2" createtm="$3" chgtm="$4" year="$5" month="$6" day="$7" week="$8" hour="$9" minute="$10" remark="$11}'`"

        shift
        eval "`echo "${*}" | sed "s/\([();']\)/\\\\\\\1/g"`"

        if [ "${disbled}" = "" -a "${year}" = "" -a "${month}" = "" \
		  -a "${day}" = "" -a "${hour}" = "" -a "${minute}" = "" ]; then
                echo "arg can not be empty."
                exit 1
        fi

		chgtm="`date +%s`"
        men=`grep -v "^${proxy_name} " ${pppoeplan_list}`
        echo "${men}" > ${pppoeplan_list}
        echo "${proxy_name} ${disbled} ${createtm} ${chgtm} ${year} ${month} ${day} ${week} ${hour} ${minute} ${remark}" >> ${pppoeplan_list}
}

cfg_log_show()
{
		grep -E "$*" ${pppoeplan_log}
}

cmd_killme()
{
		local me=${0##*/}

		ps axw | grep "${me} -s" | grep -v grep | \
		while read pid othe
		do
				kill ${pid}
		done
}

count_server()
{
		local me=${0##*/}
		
		pcount=`ps axw | grep "${me} -s" | grep -v grep | wc -l`
		echo $((${pcount}))
}

enabled_server()
{
		local status=${1}
	
		if [ "${status}" = "" ]; then
				[ -f ${pppoeplan_conf} ] && . ${pppoeplan_conf}
				[ "${enabled}" = "" ] && enabled=0
				echo ${enabled}
				return 
		fi

		[ "${status}" != "1" ] && status=0

        men=`grep -v "^enabled=" ${pppoeplan_conf}`
        echo "${men}" > ${pppoeplan_conf}
		echo "enabled=${status}" >> ${pppoeplan_conf}
}

help()
{
        printf "usage:\t -s|--server\n"
		printf "      \t -e|--enabled-server [1|0]\n"
        printf "      \t -k|--kill_server\n"
		printf "      \t -n|--count_server\n"
        printf "      \t -l|--list\n"
        printf "      \t -a|--add proxy_name disbled year month day week hour minute [remark]\n"
        printf "      \t -d|--del proxy_name\n"
        printf "      \t -c|--chang proxy_name [disbled=[?]] [year=[?]] [month=[?]] [day=[?]] \n"
        printf "      \t                       [week=[?]] [hour=[?]] [minute=[?]] [remark=[?]]\n"
        printf "      \t -g|--log [proxy_name]\n"
		
        printf "\n"
        printf "parameter format description:\n"
        printf "      \t year   2014 or 0000\n"
        printf "      \t month  08   or 00\n"
        printf "      \t day    03   or 00\n"
        printf "      \t week   null or Mon|Tue|Wed|Thu|Fri|Sat|Sun\n"
        printf "      \t hour   14   or 24\n"
        printf "      \t minute 36   or 60\n"
}

case ${1} in
        "-s"|"--server")
                shift
                server $*
        ;;
		"-e"|"--enabled-server")
				shift
				enabled_server $*
		;;
        "-k"|"--killme")
                shift
                cmd_killme $*
        ;;
        "-n"|"--count_server")
                shift
                count_server $*
        ;;
        "-l"|"--list")
                shift
                cfg_list $*
        ;;
        "-a"|"--add")
                shift
                cfg_add $*
        ;;
        "-d"|"--del")
                shift
                cfg_del $*
        ;;
        "-c"|"--chang")
                shift
                cfg_chang $*
        ;;
		"-g"|"--log")
				shift
				cfg_log_show $*
		;;
        *)
                help
        ;;
esac


