#!/usr/bin/perl
#
# change-nameserver -- update nameserver records at DomainPeople
# 
use HTTP::Cookies;
use LWP;

my $domain = $ARGV[0];
my $user = 'username_here';
my $pass = 'password_here';

die "Usage: $0 domain\n" unless $domain;

# Get login cookie

print "Logging in...\n";
my $ua = LWP::UserAgent->new;
my $jar = HTTP::Cookies->new( file => '/tmp/dptest.cookies' );
$ua->cookie_jar($jar);

my $lreq = new HTTP::Request(POST =>
    'https://www.domainpeople.com/userAdmin/Login');

$lreq->content_type('application/x-www-form-urlencoded');
$lreq->content(esubmit=Login&DOMAIN=$user&PASSWORD=xrmt773");
$lreq->push_header(Referer => 'https://www.domainpeople.com/userAdmin/Login');

$jar->add_cookie_header($lreq);
my $lres = $ua->request($lreq);

die ($lres->content() . "\n\n\n\n" .  $lres->status_line()) if $lres->is_error;
print "Ok, got login cookie:\n";
$jar->extract_cookies($lres);
print $jar->as_string;

print "\nSelecting $domain:\n";
my $dreq = new HTTP::Request(GET =>
    "https://www.domainpeople.com/userAdmin/userList?domain_name=$domain&type=domain&language=en&PartnerID=startsite&SiteID=newsite");

$jar->add_cookie_header($dreq);
my $dres = $ua->request($dreq);

die ($dres->content() . "\n\n\n\n" . $dres->status_line()) if $dres->is_error;

print "OK.\n\nUpdating nameservers:\n";

my $nreq = new HTTP::Request(POST =>
    'https://www.domainpeople.com/userAdmin/UpdateNameServers');

$nreq->content_type('application/x-www-form-urlencoded');
$nreq->content('language=en&PartnerID=startsite&submit=true&nameServer=ns1.e-smith.com&ip=&nameServer=ns2.e-smith.com&ip=&nameServer=ns3.e-smith.com&ip=&nameServer=ns4.e-smith.com&ip=');

$jar->add_cookie_header($nreq);
my $nres = $ua->request($nreq);

die ($nres->content() . "\n\n\n\n" . $nres->status_line()) if $nres->is_error;

print "OK. Check output in $domain.out.\n";

open (D, "> $domain.out") or die "Can't open $domain.out: $!\n";
print D $nres->content();

