use Time::Local;
use LWP::UserAgent;

$historyfile = '/home/tf98/bin/IPhistory.txt';
$scratchfile = '/home/tf98/bin/wgetOutput.txt';
$wgetURL     = 'http://admin:*****@10.0.0.2:8080/doc/home.htm';
$loginURL    = 'https://cp.buydomains.com/cgi-bin/login.cgi?domain=tomfotherby.com&password=*************';

$current_IP  = &getCurrentIP();
$previous_IP = &getPreviousIP();

print "Current WAN IP= $current_IP";

if ($current_IP eq $previous_IP) {
    print " (same as when last checked)\n";
} else {
    print " (a new IP address from BT oldIP=$previous_IP)\n";
    $loginPage = &loginToBuyDomains($loginURL);
    #&updateURLforwarding;
    &updateDNS($loginPage,$current_IP);
    &saveIPaddress;
}

#########################################################################

sub getPreviousIP {

    open IPHIST, $historyfile or die "Cannot open $historyfile for read :$!";
    my @lines = reverse(<IPHIST>);
    close(IPHIST);

    foreach $line (@lines) {
	if ($line =~ /(\S+)\s/) {
	    return $1;
	}
    }
}

#########################################################################

sub getCurrentIP {
    exec `wget -q -O $scratchfile '$wgetURL'`;

    open WGETOUT, $scratchfile or die "Cannot open $scratchfile for read :$!";
    while($currentline = <WGETOUT>) {
	if ($currentline =~ /<TD>(.*?)\.(.*?)\.(.*?)\.(.*?)<\/TD>/) {
	  $current_IP = "$1.$2.$3.$4";
	  last;
        }
    }
    close(WGETOUT);
    return $current_IP;
}

#########################################################################

sub saveIPaddress {
    ($wkday,$month,$day,$time,$year) = split(/\s+/, localtime);
    open IPHIST, ">>$historyfile" or die "Cannot open $historyfile for append :$!";
    print IPHIST "$current_IP $day/$month/$year-$time\n";
    close(IPHIST);
}

#########################################################################

sub loginToBuyDomains {
    my ($loginURL) = @_;
    my $pageHTML = "";
    print "Logging into buydomains.com.\n";
    
    exec `wget -q -O $scratchfile '$loginURL'`;

    open WGETOUT, $scratchfile or die "Cannot open $scratchfile for read :$!";
    while($currentline = <WGETOUT>) {
	$pageHTML .= $currentline;
    }
    close WGETOUT;
    return $pageHTML;
}

#########################################################################

sub updateDNS {
    my ($loginPageHTML,$newIP) = @_;
    my $urlForDNS = "";
    @lines = split(/\n/,$loginPageHTML);
    
    foreach $currentline (@lines) {
	if ($currentline =~ /Advanced DNS <A HREF=\'(.*?)\'>\[edit\]/) {
	    $urlForDNS = $1;
	    last;
	}
    }
    if ($urlForDNS eq "") {
	print "Couldn't find DNS forwarding URL\n";
    } else {
	$urlForDNS = "https://cp.buydomains.com/$urlForDNS";
	print "Found DNS forwarding URL. Going to \"$urlForDNS\"\n";

	exec `wget -q -O $scratchfile '$urlForDNS'`;
	
#<input type="hidden" name="advanced" value="0">
#<input type=hidden name=update_a_host value="tomfotherby.com">tomfotherby.com</td>
#<input type=text size=20 name=update_a_dest value="01.152.153.76"></td>
#<input type=hidden name=update_a_id value=534600>
#<input type=checkbox name=delete value=534600></td>
#<input type=text size=30 name=a_host></td>
#<input type=text name=a_dest></td>
#<input type=text size=30 name=mx_host></td>
#<input type=text size=30 name=mx_dest></td>
#<input size=3 type=text name=mx_pri></td>
#<input type=submit name=action value='Modify'></td></tr>
#<input type=hidden name=domain value=tomfotherby.com>
#<input type=submit name=action value='Create'>

	print "Form properties: \n";
	open WGETOUT, $scratchfile or die "Cannot open $scratchfile for read :$!";
	while($currentline = <WGETOUT>) {
	    if ($currentline =~ /name=update_a_host value="(.*?)">/) {
		$update_a_host = $1;
		print " update_a_host = $update_a_host\n";
	    }
	    if ($currentline =~ /name=update_a_dest value="(.*?)">/) {
		$oldDNS = $1;
		print " update_a_dest = $oldDNS\n";
	    }
	    if ($currentline =~ /name=update_a_id value=(.*?)>/) {
		$update_a_id = $1;
		print " update_a_id = $update_a_id\n";
	    }
	    if ($currentline =~ /name=domain value=(.*?)>/) {
		$domain = $1;
		print " domain = $domain\n";
	    }
	}
	close WGETOUT;

	if ($update_a_host eq "" || $update_a_id eq "" || $domain eq "") {
	    print "ERROR: couldn't find all variables needed for form!\n";
	} else {
	    print "Old DNS forwarding IP: $oldDNS\n";
	    print "Changing to: $newIP\n";
	    $urlForDNS = "$urlForDNS&update_a_host=$update_a_host&update_a_dest=$newIP&update_a_id=$update_a_id&action=Modify";
	    print "Using: \"$urlForDNS\"\n";
	    exec `wget -q -O $scratchfile '$urlForDNS'`;
	}
    }
}

#########################################################################

sub updateURLforwarding {
    print "Logging into buydomains.com.\n";

    $newURL= "http://$current_IP";

    exec `wget -q -O $scratchfile '$loginURL'`;

    open WGETOUT, $scratchfile or die "Cannot open $scratchfile for read :$!";
    while($currentline = <WGETOUT>) {
	if ($currentline =~ /URL Forwarding to (.*?)\">Frame \/ URL \/ IP Forwarding<\/SPAN> <A HREF=\'(.*?)\'/) {
	    $oldURL = $1;
	    $changeURL = "https://cp.buydomains.com$2&type=URL6&url1=$newURL&action=update_urlfw_3";
            exec `wget -q -O $scratchfile '$changeURL'`;
	    print " *Old forwarding URL= \"$oldURL\"\n";
	    print " *New forwarding URL= \"$newURL\"\n";
	    last;
	}
    }
    close(WGETOUT);
}
