#!/bin/sh /etc/rc.common
# iup at boot
# Copyright (C) 2007 OpenWrt.org

START=98
STOP=15
USE_PROCD=1

. /lib/functions.sh

include /lib/network

CRONPATH="/etc/crontabs/root"
RANGE=60

management_interfaces() {
	local DHCP_IFACES=""

	is_notbridged_dhcp() {
		local config="$1"
		local proto="$(uci -q get network.$config.proto)"
		local typ="$(uci -q get network.$config.type)"
		if [ "$proto" == "dhcp" -a "$typ" != "bridge" ]; then
			DHCP_IFACES="$DHCP_IFACES $config"
		fi
	}

	config_load network
	config_foreach is_notbridged_dhcp interface
	echo $DHCP_IFACES
}

init_iup() {
	local polling_enabled
	local interval
	local starttime
	local nummber
	number=$RANDOM

	[ -f $CRONPATH ] || touch $CRONPATH

	config_load provisioning
	config_get polling_enabled polling enabled on
	config_get interval  polling  interval
	config_get starttime  polling  starttime

	### Ask for IUP related DHCP options only if IUP is enabled ###
	new_reqopts() {
		local net=$1
		local enabled
		local newreqopts=
		local baseopts=
		local reqopts="$(uci -q get network.$net.reqopts)"
		local iupopts="66 67 128 224 225 226"
		local ropt iopt
		config_get enabled iup enabled "on"
		for ropt in $reqopts; do
			case $ropt in
				66|67|128|224|225|226) ;;
				*) baseopts="$baseopts $ropt" ;;
			esac
		done
		ropt=""
		reqopts="$baseopts $iupopts"
		for ropt in $reqopts; do
			case $ropt in
				66|67|128|224|225|226) [ $enabled == "on" ] && newreqopts="$newreqopts $ropt" ;;
				*) newreqopts="$newreqopts $ropt" ;;
			esac
		done

		newreqopts="$(echo $newreqopts | tr ' ' '\n' | sort -n | tr '\n' ' ' | sed 's/^[ \t]*//;s/[ \t]*$//')"
		echo "$newreqopts"
	}
	for net in $(management_interfaces); do
		uci -q set network.$net.reqopts="$(new_reqopts $net)"
	done
	uci commit network
	ubus call network reload
	#################################################################

	if [ $polling_enabled == "off" ]; then
		sed -i "/\/sbin\/iup/d" $CRONPATH
		logger -s -t /etc/init.d/iup[$$] "Provisioning polling unscheduled" 2>/dev/console
		return
	fi

	local log_hour=$starttime
	local log_minute
	local log_interval=$interval
	if [ $interval == "weekly" ]; then
		interval="0"
		log_interval="$log_interval/sunday"
	elif [ $interval == "hourly" ]; then
		interval='*'
		starttime='*'
	else
		interval='*'
	fi
        let "number %= $RANGE"
	if ! grep -q "$starttime  \* \* \\$interval     /sbin/iup" "$CRONPATH" ; then
		if grep -q "iup" "$CRONPATH" ; then
			sed -i "/iup/d" $CRONPATH
		fi
		echo "$number $starttime * * $interval     /sbin/iup -v > /dev/null 2>&1" >> $CRONPATH
		fsync $CRONPATH
		/etc/init.d/cron restart
	fi
	log_minute=$(awk '/\/sbin\/iup/ {print $1}' /etc/crontabs/root)
	logger -s -t /etc/init.d/iup[$$] "Provisioning is scheduled at $log_hour:$log_minute $log_interval" 2>/dev/console
}

start_service() {
	init_iup

	/sbin/iup -v &
}

stop_service() {
	if [ -f "/tmp/run/iup.pid" ]; then
		kill -9 $(cat /tmp/run/iup.pid)
		rm -rf /tmp/run/iup.pid
	fi
}

reload_service() {
	stop
	start
}

service_triggers() {
        procd_add_reload_trigger provisioning
}
