#!/bin/sh
# vnstatd-start - Sets up (and starts) "vnstatd"
# License:  BSD-style (for this file only)
# Revision: 100322

DATADIR=/var/lib/vnstat
PREFIX=__META_PREFIX__
BINDIR=$PREFIX/bin
SBINDIR=$PREFIX/sbin
CLIENT=vnstat                   # Client EXE name without path
SERVER=vnstatd                  # Server EXE name without path
export PATH=$SBINDIR:$BINDIR:$PATH

mkdir -p  $DATADIR || exit 1
chmod 755 $DATADIR || exit 1

for device in `$CLIENT --iflist`
do
    if [ "x$device" = "xdummy0" ]; then continue; fi
    if [ "x$device" = "xlo"     ]; then continue; fi

    if [ \! -f $DATADIR/$device ]; then
        $CLIENT --update -i $device >& /dev/null
    fi
done

n=`ps ax | grep "$SERVER -d" | grep -v grep | wc -l`

if [ "x$n" = "x0" ]; then
    $SERVER -d
fi
