#!/usr/bin/perl -w

# soucast userap modulu pro QoS na klfree debian
# ==============================================
# tento skript aktualizuje seznamy IP adres uzivatelu v /var/state/qos
# data ziskava z exportu z IS klfree

use lib "/etc/qos/scripts/userap_files";

use UserapCommon;

#$DBSERVER="https://work.is.klfree.net/xtom/is/export.php?export=bans&net=";

sub get_stamp ($) {
    my $fn = shift;
    my $stamp = '';
    if (open LF,"$fn") {
	$stamp = <LF>;
	(undef, $stamp) = split ("'",<LF>);
	close LF;
	return $stamp
    } else {
	return '';
    }
}

sub change_stamp ($$) {
    my $fn = shift;
    my $stamp = shift;
    open TF, ">$fn.tmp";
    if (open LF, "$fn") {
	while (<LF>) {
	    if ($_ =~ /stamp/) {
		print TF "<accounts stamp='$stamp'>\n";
	    } else {
		print TF $_;
	    }
	}
    }
    close TF;
    close LF;
    `mv $fn.tmp $fn`;
}


sub update_face {
    my ($face,$fface) = @_;
    print "       $face\[$fface\] : ";
    if (system("ip link show dev $fface 2>/dev/null >/dev/null")) {
        print "unknown device $fface\n";
        exit 0;
    }
    
    system("rm -f $DATADIR/qos-${face}.tmp 2>/dev/null");

    my $act_stamp = get_stamp("$DATADIR/qos-${face}.act");

    if (-r "$QOSBIN/scripts/userap_files/manual/subnets-$face") {
	#manualne definovane subnety 
	my $ADDR = `cat ${QOSBIN}/scripts/userap_files/manual/subnets-${face}`;
	$ADDR =~ s/#.*//g;
	@ADDR = split("\n",$ADDR);
    } else {
	#automaticky podle subnetu na interfacech 
	my $ADDR =`ip r show dev $fface | cut -f1 -d ' '`;
	@ADDR = split ("\n",$ADDR);
    }
    
    #stazeni exportu ze serveru
    my $aadr = join(",", @ADDR);
    if (system("wget --timeout=3 --tries=5 --no-check-certificate -O $DATADIR/qos.tmp \"${DBSERVER}$aadr\&stamp=$act_stamp\" 2>/dev/null" )) {
        print "===== FAIL ==== \n";
        return;
    }
    system("cat $DATADIR/qos.tmp >>$DATADIR/qos-${face}.tmp");


    if ( ! (-r "$DATADIR/qos-$face.tmp") ) {
        print "NO DATA!\n";
        return;
    }

    my $size = `wc -l $DATADIR/qos-$face.tmp | cut -f1 -d ' '`;
    if ($size == 0) {
	print "NO LINES!\n";
	return;
    }

    my $new_stamp = get_stamp("$DATADIR/qos-${face}.tmp");
    #test stamps first
    if ($new_stamp eq $act_stamp) {
        #equal - no change
        print "same stamp\n";
    } elsif (system("diff $DATADIR/qos-$face.tmp $DATADIR/qos-$face.act --ignore-matching-lines=stamp 2>/dev/null")) {
    	print "change!\n";
	`touch $DATADIR/faces/$face`;
	system("cp $DATADIR/qos-$face.tmp $DATADIR/qos-$face.act");
    } else {
        change_stamp("$DATADIR/qos-${face}.act",$new_stamp);	    
        print "only stamp change\n";
    }
    #nakopcit .tmp na .act
    system("rm -f $DATADIR/qos-$face.tmp 2>/dev/null");
    system("rm -f $DATADIR/qos.tmp 2>/dev/null");
}



# ==========================================================
# MAIN 
# ==========================================================

print "   is_updater.pl v:$UVERS updating:\n";

$tmpfc = `ls $DATADIR/faces/`;
@faces = split(" ", $tmpfc);
foreach (@faces) {
    my $face = $_;
    if ($face ne "") {
	my $fface = `cat $DATADIR/faces/$face`;
	chomp($fface);
	update_face($face,$fface);
    }
}
# ==========================================================
