#!/usr/bin/perl -wT

use strict;

$ENV{PATH} = "/sbin";
my $command = "ifconfig -a";
my $cache = "/var/opt/tog-pegasus/cache/ipaddr";
my ($hw, $inet);

#
# Get fresh interface information.
#
open(FH, "$command |") or die "Could not run \"$command\" ($!)";

my %ipaddr;
$/ = "\n\n";
while (<FH>) 
  { 
    $ipaddr{$1} = $2 if /HWaddr ([\w:]*).*inet addr:(\d+\.\d+\.\d+\.\d+)/s; 
  }
close FH;
$/ = "\n";

#
# Read old interface info from our cache.
#
if (open(FH, "$cache"))
  {
    while (<FH>) 
      { 
	($hw, $inet) = split;

        # If the IP address has changed, output the old and new address.
        if (exists $ipaddr{$hw} && $ipaddr{$hw} ne $inet)
          { print "$inet $ipaddr{$hw}\n"; }
      }
    close FH;
  }

#
# Write new info to cache file.
#
open(FH, "> $cache") or die "Could not open $cache ($!)";
print FH "$hw $inet\n" while ($hw, $inet) = each(%ipaddr);
close FH;

exit 0;
