#!/bin/sh /etc/rc.common
# Copyright (C) 2008 OpenWrt.org

START=99
LIB_D=/var/lib/vnstat
WWW_D=/www/vnstat
RUN_D=/var/run
PID_F=$RUN_D/vnstat.pid
VNSTATD_BIN=/usr/sbin/vnstatd

system_config() {
	local cfg="$1"

	config_get hostname "$cfg" hostname
	hostname="${hostname:-OpenWrt}"
}

do_download() {
	include /lib/network
	scan_interfaces

	local cfg="$1"

	config_get ifname "$cfg" ifname
	[ -n "$ifname" ] || return 0
	[ -e $LIB_D/$ifname ] && return 0

	config_get_bool enabled "$cfg" enabled '1'
	[ "$enabled" -gt 0 ] && {
		config_get remote_host "$cfg" remote_host
		[ -n "$remote_host" ] || return 0
		config_get remote_path "$cfg" remote_path
		[ -n "$remote_path" ] || return 0

		while [ ! -e $LIB_D/$ifname ]; do
			wget http://$remote_host/$remote_path/${hostname}_$ifname -O $LIB_D/$ifname 2>/dev/null
			[ -e $LIB_D/$ifname ] && {
				logger -t "vnstat" "Successfully downloaded ${ifname}'s vnStat database backup"
				[ -L $WWW_D/$ifname ] || ln -s $LIB_D/$ifname $WWW_D/$ifname
				return 0
			}
			sleep 30
		done
	}
}

start() {
	[ -d $LIB_D ] || mkdir -p $LIB_D
	[ -d $WWW_D ] || mkdir -p $WWW_D

	config_load system
	config_foreach system_config system

	config_load vnstat
	config_foreach do_download interface

	[ ! -f $PID_F ] && $VNSTATD_BIN -d
}

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

reload() {
	[ -f $PID_F ] && kill -HUP $(cat $PID_F)
}

