#!/bin/sh

. /usr/share/libubox/jshn.sh

BOARDDATA="/tmp/cache/systeminfo/boarddata.info"
NETWORK_CONF="/tmp/cache/network/network.conf"
NETWORK_STATUS="/tmp/cache/network/network.status"

json_load "`cat $BOARDDATA`"
json_get_var board_region board_region
json_load "`cat $NETWORK_CONF`"
json_get_var wan_protocol "wan_protocol"
json_get_var wan_interface "wan_interface"

# Non GR, disable this feature.
[ "$board_region" != "GR" ] && exit

# WAN connection type is not PPP mode
[ "$wan_protocol" != "pppoe" -a "$wan_protocol" != "pptp" -a "$wan_protocol" != "l2tp" ] && exit

# Internet connection auto reset is not enabled
[ -z "$(uci get network.$wan_interface.disconn_time)" ] && exit

# If the Connection Mode set to Always On, after the disconnection, the router will
# connect to Internet automatically again. If the Connection Mode set to Manually Connect
# and the Internet connection is ON at that time, after the disconnection, the router will
# connect to Internet automatically again. If the Connection Mode set to Manually Connect
# and the Internet connection is OFF at that time, we don't need to do anything for this
# case. If the Connection Mode set to Dial on Demand, this feature "Enable automatc
# Internet connection reset at" is disabled and gray out.

ppp_connmode=`uci get network.$wan_interface.connmode`
json_load "`cat $NETWORK_STATUS`"
json_get_var ppp_status "wan_updown"

[ "$ppp_connmode" == "1" ] && exit
[ "$ppp_connmode" == "2" -a "$ppp_status" != "up" ] && exit

logger "[Internet disconnected] 24hr automatic disconnection. "

ifdown $wan_interface

[ "$ppp_connmode" = "0" -o "$ppp_connmode" = "2" ] && ifup $wan_interface
