#!/bin/sh
# vnstatd-stop - Stops "vnstatd" and handles related tasks
# License:  BSD-style (for this file only)
# Revision: 100322

#---------------------------------------------------------------------
# Program parameters.

PREFIX=__META_PREFIX__
SBINDIR=$PREFIX/sbin
CLIENT=vnstat                   # Client EXE name without path
SERVER=vnstatd                  # Server EXE name without path
PARENTDIR=/var/lib
DATADIR=$PARENTDIR/$CLIENT
MAXBACKUPS=5                    # Maximum number of backups saved

#---------------------------------------------------------------------
# Initial setup.

whoami=`whoami`
if [ "x$whoami" \!= "xroot" ]; then
    echo Error: vnstatd-stop must be run as root
    exit 1
fi

export PATH=$SBINDIR:$PATH

#---------------------------------------------------------------------
# Check to see if daemon is running.

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

if [ "x$n" = "x0" ]; then
    exit 1
fi

#---------------------------------------------------------------------
# Ask the daemon to stop.

killall --signal SIGTERM $SERVER

#---------------------------------------------------------------------
# Make sure that the daemon stops.

n=`ps ax | grep "$SERVER -d" | grep -v grep | wc -l`
if [ "x$n" \!= "x0" ]; then
    pause250ms

    n=`ps ax | grep "$SERVER -d" | grep -v grep | wc -l`
    if [ "x$n" \!= "x0" ]; then
        pause250ms

        n=`ps ax | grep "$SERVER -d" | grep -v grep | wc -l`
        if [ "x$n" \!= "x0" ]; then
            killany --wait "$SERVER -d"
        fi
    fi
fi

#---------------------------------------------------------------------
# Make backups and check them.

# Note:  "vnstat -d -i ..." may say "Not enough data available" or "no
# data available". In this context, the first message seems to be nor-
# mal and the second one seems to indicate corruption.

EPOCH=`epoch`
BACKUPDIR=$DATADIR.$EPOCH

if [ -d $DATADIR ]; then
    if [ \! -d $BACKUPDIR ]; then
        rm -fr $BACKUPDIR                           || exit 1
        cp -a $DATADIR $BACKUPDIR                   || exit 1

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

            n=`$CLIENT -d -i $device 2>&1 | \
                grep -i no.data.available | wc -l`

            if [ "x$n" \!= "x0" ]; then
                echo Error: $CLIENT database is corrupted
                rm   -fr $BACKUPDIR                 || exit 1
                good=`ls $PARENTDIR | \
grep "^$CLIENT\.[0-9]" | tail -1`

                if [ "x$good" \!= "x" ]; then
                    echo vnstatd-cycle: Restoring from backup
                    pushd $PARENTDIR >& /dev/null   || exit 1
                    mv $DATADIR $DATADIR-bad.$EPOCH || exit 1
                    cp -a $good $DATADIR            || exit 1
                    popd             >& /dev/null   || exit 1
                fi

                exit 1
            fi
        done
    fi
fi

#---------------------------------------------------------------------
# Expire old backups.

# The "1 2 3 ..." sequence  used  here is  arbitrary.  Any sequence of
# roughly the right length should work.  The exact length isn't criti-
# cal, but it should probably be greater than or equal to MAXBACKUPS.

for x in 1 2 3 4 5
do
    pushd $PARENTDIR >& /dev/null               || exit 1
    n=`ls | grep "^$CLIENT\.[0-9]" | wc -l`

    if [ $n -gt $MAXBACKUPS ]; then
        rm -fr `ls | grep "^$CLIENT\.[0-9]" | head -1`
    fi
    popd             >& /dev/null               || exit 1
done
