#!/bin/sh /etc/rc.common
#Copyright (C) 2008-2015
#Author: luopei
#date: 2015.5.6
#brief:create the script file for ippool

START=23

CONFIGFILE="/var/etc/ippool.conf"
BIN=ippool
RUN_D=/var/run
PID_F=$RUN_D/$BIN.pid
ITEM_F="/var/etc/config_arg"

read_config(){
	local name
	local start_ip
	local end_ip
	
	config_get name $1 name  #variable section_name  option_name
	config_get start_ip $1 start_ip
	config_get end_ip  $1 end_ip
	
	echo "${name}#${start_ip}#${end_ip}" >>$CONFIGFILE
	
}

start(){
	mkdir -p $(dirname $CONFIGFILE)
	
	[ -e $CONFIGFILE ] && rm -f $CONFIGFILE
        touch $CONFIGFILE   #avoid open error when /etc/config/ippool is empty
	local max_item=$(uci -c /etc/profile.d get profile.@ippool[0].item_max)
	local max_network=$(uci -c /etc/profile.d get profile.@dhcps[0].network_max)
	max_network=${max_network:-128}
	max_item=$(($max_item+$max_network))
	echo $max_item > $ITEM_F
	config_load ippool   #load config file
	
	config_foreach read_config ippool  #funcname section_name

	
	vnete /usr/sbin/ippoold $CONFIGFILE $PID_F &  # make it run in background
	pid=`pgrep ippoold`
	[ -n "$pid" ] && echo -1000 > /proc/${pid}/oom_score_adj
}

stop(){
	[ -e $CONFIGFILE ] && rm -f $CONFIGFILE
	[ -f $PID_F ] && kill $(cat $PID_F)
}

reload(){  # notify the client to 
   # to do something here
   config_load ippool   #load config file
   config_foreach read_config ippool  #funcname section_name
	
   return 1
}

restart(){  # 
    stop
	start
}
