#!/bin/sh /etc/rc.common

START=99

set_switch_on="uci set http_apk_proxy.settings.enabled=1"
set_switch_off="uci set http_apk_proxy.settings.enabled=0"
set_switch_commit="uci commit http_apk_proxy"
HTTP_APK_PROXY_SH="/usr/sbin/http_apk_proxy.sh"

export EXTRA_COMMANDS=" on off log_stat reload_iptable_rule"
export EXTRA_HELP="	on	Switch to the start state and start
	off	Switch to the stop state and stop
	log_stat	log statistics
	reload_iptable_rule	reload iptable rule"

log_stat() {
    apk_stat_file="/proc/http_apk/apk_stat"
    if [ -f $apk_stat_file ]; then
        line=`cat $apk_stat_file`
        v1=`echo $line | awk -F "," '{print $1}' | awk -F ":" '{print $2}'`
        v2=`echo $line | awk -F "," '{print $2}' | awk -F ":" '{print $2}'`
        v3=`echo $line | awk -F "," '{print $3}' | awk -F ":" '{print $2}'`
        v4=`echo $line | awk -F "," '{print $4}' | awk -F ":" '{print $2}'`
        v5=`echo $line | awk -F "," '{print $5}' | awk -F ":" '{print $2}'`
        v6=`echo $line | awk -F "," '{print $6}' | awk -F ":" '{print $2}'`
        v7=`echo $line | awk -F "," '{print $7}' | awk -F ":" '{print $2}'`
        logger stat_points_privacy http_apk_stat_plus=$v1,$v2,$v3,$v4,$v5,$v6,$v7
        echo "0" > $apk_stat_file
    fi
}

reload_iptable_rule() {
    switch=`uci get http_apk_proxy.settings.enabled -q`
    if [ $switch -ne "1" ]; then
        #if not enabled, just exit
        return 0
    fi
    $HTTP_APK_PROXY_SH reload_iptable_rule
}

start() {
    #if in ft_mode, do not start!
    ft_mode=`cat /proc/xiaoqiang/ft_mode`
    if [ "$ft_mode" -ne "0" ]; then
	return 0
    fi

    switch=`uci get http_apk_proxy.settings.enabled -q`
    if [ $switch -ne "1" ]; then
        #if not enabled, just exit
        return 0
    fi

    $HTTP_APK_PROXY_SH on
    return 0
}

stop() {
    $HTTP_APK_PROXY_SH off
    return 0
}

off() {
    $set_switch_off >/dev/null 2>&1
    $set_switch_commit >/dev/null 2>&1
    stop
    return $?
}

on() {
    $set_switch_on >/dev/null 2>&1
    $set_switch_commit >/dev/null 2>&1

    start
    return $?
}

