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

START=99

. /usr/lib/dpi.sh
. /usr/lib/dpi/dpi_log_database.sh

local debug="debug=0"

start() {
    [ -f $FILD_DPI_RUNNING ] && return
    local flag=`$NETE iptables -w -t mangle -vnL FORWARD | grep DPI_RESTRICTION`
    [ -f "$flag" ] && return
    
    [ ! -f "$DPI_DATABASE_RAM" ] && {
        update_app_database
    }

    local switch=`uci get dpi.dpi.switch`
    [ "$switch" == "on" -o "$switch" == "1" ] && {
        local log_switch=`uci get dpi.dpi.log_switch`
        local nb_cpu=`uci get dpi.dpi.nb_cpu`
        local mode=`uci get dpi.dpi.mode`
        local cache=`uci get dpi.dpi.cache`

        [ "$log_switch" == "on" -o "$log_switch" == "1" ] && {
            dpi_database_loop_start 
            dpi_set_switch 11 #11 to code logic: stat switch on 
        }

        [ "$log_switch" == "off" -o "$log_switch" == "0" ] && {
            dpi_set_switch 10 #10 to code logic: stat switch off
        }

        dpi_ruleset_init
        dpi_iptables_init
        dpi_set_switch 1
        touch $FILD_DPI_RUNNING
    }

    [ "$switch" == "off" -o "$switch" == "0" ] && {
        dpi_set_switch 0
        dpi_set_switch 10 #10 to code logic: stat switch off
    }
}

stop() {
    [ -f $FILD_DPI_RUNNING ] && {
        dpi_database_loop_stop
        dpi_iptables_exit
        dpi_ruleset_exit
        sleep 1s

        rm $FILD_DPI_RUNNING
        [ -f "$FILE_PID_PATH" ] && rm $FILE_PID_PATH
    }
}

restart() {
    stop
    start
}

reload() {
    stop
    start
}