#!/usr/bin/perl

# Requires libs from client 2.8.3.2
# the --infile have one domain per line
# This script is released under GPL license
# author: kevin@bestkevin.com
# credits: OpenSRS that first developed the original script

BEGIN
{
    do "XXXX/OpenSRS.conf";

    # when running against live system, this must go through batch pool, rather
    # than the normal one

#    $OPENSRS{ REMOTE_HOST } = "horizon.opensrs.net";	# TEST
    $OPENSRS{ REMOTE_HOST } = "batch.opensrs.net";	# LIVE
}

use strict;
use lib $PATH_LIB;

use OpenSRS::XML_Client;
use Getopt::Long;

my ( $lock, $unlock, $infile, $csvfile );

GetOptions(
#    "lock"	=> \$lock,
#    "unlock"	=> \$unlock,
    "infile=s"	=> \$infile,
#    "csvfile=s"	=> \$csvfile,
) or usage();

my @domain_list = get_domain_list();
if ( not scalar @domain_list ) {
    usage( "No domains specified" );
}

my $XML_Client = OpenSRS::XML_Client->new( %OPENSRS );
$XML_Client->login;

open GOOD, ">logfile.good" or die "Can't open logfile.good for writing: $!";
open BAD, ">logfile.bad" or die "Can't open logfile.bad for writing: $!";
open SKIP, ">logfile.skip" or die "Can't open logfile.skip for writing: $!";

foreach my $fh ( \*GOOD, \*BAD, \*SKIP ) {
    my $oldfh = select $fh;
    $| = 1;
    select $oldfh;
}

foreach my $domain ( @domain_list ) {
    my $resp = $XML_Client->send_cmd(
	{
	'object' => 'DOMAIN',
	'attributes' => {
		'auto_renew' => undef,
		'custom_tech_contact' => 0,
		'link_domains' => 0,
		'handle' => 'process',
		'contact_set' => {
			'admin' => {
				'first_name' => '',
				'state' => '',
				'country' => '',
				'address1' => '',
				'address2' => '',
				'last_name' => '',
				'address3' => '',
				'city' => '',
				'fax' => undef,
				'postal_code' => '',
				'email' => '',
				'phone' => '',
				'org_name' => ''
				},
			'billing' => {
				'first_name' => '',
				'state' => '',
				'country' => '',
				'address1' => '',
				'address2' => '',
				'last_name' => '',
				'address3' => '',
				'city' => '',
				'fax' => undef,
				'postal_code' => '',
				'email' => '',
				'phone' => '',
				'org_name' => ''
				},
			'owner' => {
				'first_name' => '',
				'state' => '',
				'country' => '',
				'address1' => '',
				'address2' => '',
				'last_name' => '',
				'address3' => '',
				'city' => '',
				'fax' => undef,
				'postal_code' => '',
				'email' => '',
				'phone' => '',
				'org_name' => ''
				}
			},
			'reg_domain' => 'XXXXSourceDomain',
			'f_lock_domain' => '0',
			'domain' => $domain,
			'affiliate_id' => undef,
			'period' => '1',
			'reg_type' => 'new',
			'reg_username' => 'XXXX',
			'mldn' => undef,
			'nameserver_list' => [
				{
				 'sortorder' => '1',
				 'name' => 'XXXX'
				 },
				 {
				 'sortorder' => '2',
				 'name' => 'XXXX'
				 }
				],
			'reg_password' => 'XXXX',
			'encoding_type' => undef,
			'custom_nameserver' => '1'
			},
		'registrant_ip' => 'XXXX',
		'protocol' => 'XCP',
		'action' => 'SW_REGISTER'
		}
    );

    print "$domain: $resp->{ response_text }\n";

    if ( not $resp->{ is_success } ) {
	if ( $resp->{ response_code } == 450 ) {
	    # tld doesn't support locking, or status can't be updated
	    print SKIP "$domain: $resp->{ response_text }\n";
	} else {
	    # domain info not found, or invalid request
	    print BAD "$domain: $resp->{ response_text }\n";
	}
    } else {
	print GOOD "$domain: $resp->{ response_text }\n";
    }

    sleep 1;
}

$XML_Client->logout;

print "\nCompleted\n";

sub get_domain_list {
    my $fh;

    my $file = $infile || undef;

    if ( defined $file ) {
	open $fh, $file or usage( "Can't open $file for reading: $!" );
    } else {
	$fh = \*STDIN;
    };

    my @domain_list = <$fh>;

    @domain_list =
	map { s/(^\s+)|(\s+$)//g; /^(\S+)/; $1 }
	grep { /\S/ }
	@domain_list;

    return @domain_list;
}

sub usage {
    my $message = shift;

    print <<EOF;
$message

Usage:
  $0 <--infile=<filename>]

where:
    - if --infile is specified, file given should contain a list of
      domains to lock/unlock, one per line.
    - if --infile is not specified, the domain list will be
      accepted on standard input, and should consist of only a single domain
      per line

the following logfiles will be created
    logfile.good
	list of domains for which lock status successfully updated
    logfile.bad
	list of domains for which lock status could not be set due to internal
	problem (usually bad command, or domain not found)
    logfile.skip
	list of domains for which the lock status could not be set due to
	non-error condition (usually if the domain is with another reseller,
	TLD does not support domain locking, or the domain is locked at the
	registry level)
EOF

    exit;
}

