#!/usr/bin/perl -w
###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007-2022  IPFire Team  <info@ipfire.org>                     #
#                                                                             #
# 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 3 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, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

use strict;

# Configuration options:
#
my  $device        = "$ARGV[0]";

our %mainsettings = ();
require '/var/ipfire/general-functions.pl';
&General::readhash("${General::swroot}/main/settings", \%mainsettings);

our $rrd_datadir   = $mainsettings{'RRDLOG'}."/";
our $event_datadir = $mainsettings{'RRDLOG'};
our $STEP          = 10;
our $tc_command    = "/sbin/tc";

# A trick is to set the environment PERL5LIB to include $GRAPHDIR
#  This is done by the init-script
# ($GRAPHDIR is obtained from /usr/local/etc/ADSL-optimizer.conf)
my $include_dir = '/var/ipfire/qos/bin';


# Create the $mainsettings{'RRDLOG'} if it doesn't exists
if ( ! -d $mainsettings{'RRDLOG'} ) {
    print "RRD-datadir not found, creating it: $mainsettings{'RRDLOG'} \n";
    my $status = system("mkdir $mainsettings{'RRDLOG'}");
    die "\nERROR cannot create \"$mainsettings{'RRDLOG'}\"\n" unless $status == 0;
}

# use POSIX;
#
#POSIX::setsid()
#    or die "Can't become a daemon: $!";

# The init scripts will do the right "daemon" thing...
# Become a daemon
print "Becoming a daemon...\n";
my $pid = fork;
exit if $pid;
die "Couldn't fork: $!" unless defined($pid);

my $time_to_die = 0;
sub signal_handler {
    $time_to_die = 1;
}
# Trap signals
$SIG{INT} = $SIG{TERM} = $SIG{HUP} = \&signal_handler;
$SIG{PIPE} = 'IGNORE';

our %classes_data;
our %classes_info;
require "$include_dir/parse-func.pl";
require "$include_dir/event-func.pl";
require "$include_dir/RRD-func.pl";

until ($time_to_die) {

    #print "Parsing tc statistics on $device\n";
    my $res = parse_class($device);
    if ( ! $res ) {
	print " Error when parsing classes on $device\n";
    }

    #print "Updating RRD data-files\n";
    $res = update_rrds();
    #if ( $res ) {
    #	print " Error updating RRDs: \"$res\"\n";
    #}

#    my $timestamp = time;
#    print "$timestamp\n";

    sleep($STEP);
}

print "tc-collector daemon exiting ... bye bye!\n";
