#!/bin/sh

#  Copyright (c) 2003, Burton M. Strauss III - Burton@ntopsupport.com

#  Released under GPL...

#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#   
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#   
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software Foundation,
#    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

echo
echo "ntop rrd population model adjustment script"
echo
echo "This script should be run with ntop DOWN."
echo 
echo "It moves existing rrd files created under the original model, that"
echo "is in single level subdirectories like 192.168.1.1/xxxx.rrd into "
echo "multiple level subdirectiories such as 192/168/1/1/xxxx.rrd."
echo 
echo "This gets past OS limits on the number of files in a single subdirectory."
echo 
echo "This so-called 'large' model was a ./configure option in 2.2 releases, but"
echo "is the norm for 2.2.x development releases and in the future ntop 2.3."
echo 
echo 
echo "!!!!!            This script IS NOT reversable."
echo 
echo 
echo "If you've already created data using both models, this script will"
echo "not lose any data, but it will not be able to combine data from"
echo "different models either.  Those subdirectories which already exist"
echo "will be flagged with ERROR messages."
echo 
echo "Run it in the rrd directory, such as .../rrd/interfaces/eth0/hosts"
echo 

echo 
echo "Because it's not reversable, and ntop should be down,"
echo "I'm going to wait 20 seconds while you think about this."
echo
echo "If you don't want to do this press cntl+C now"
echo 
echo -n "20..."
sleep 5
echo -n "15..."
sleep 5
echo -n "10..."
sleep 1
echo -n "9..."
sleep 1
echo -n "8..."
sleep 1
echo -n "7..."
sleep 1
echo -n "6..."
sleep 1
echo -n "5..."
sleep 1
echo -n "4..."
sleep 1
echo -n "3..."
sleep 1
echo -n "2..."
sleep 1
echo -n "1..."
sleep 1
echo "OK... we're good to go!"
echo
echo
echo

for dir in `find . -type d -name "*.*.*.*" -maxdepth 1`; do
    olddir=`echo ${dir} | awk '{ print substr($1, 3) }'`
    newdir=`echo ${dir} | awk '{ gsub(/\\./, "/", $1); print substr($1, 3) }'`

    if test -d $newdir; then
        c=`ls -1 $newdir | wc -l`
        if test $c -gt 0; then
            echo "WARNING $newdir already exists - may have valuable data - skipping"
            continue
        else
            rc=0
        fi
    else
        mkdir -p $newdir
        rc=$?
        if test $rc -ne 0; then
            echo "ERROR mkdir -p $newdir returned $rc ... aborting"
            exit 1
        fi
    fi

    echo "    Moving $olddir to $newdir"

    mv --reply=no $olddir/* $newdir
    rc=$?

    if test $rc -eq 0; then
        rmdir $olddir
        rc=$?
        if test $rc -ne 0; then
            echo "   WARNING unable to remove directory $olddir ... continuing"
        fi
    else
        echo "ERROR mv --reply=no $olddir/* $newdir returned $rc ... aborting"
        exit 1
    fi

done
