#!/usr/bin/perl
# move blocks listed with one org to another.  intended for orgs that have several bytestrings as names in WHOIS.

use strict;
use warnings;
use DBI;

use DNSBL;

my $dnsbl = new DNSBL;

# default DB info - all other settings should be loaded from the DB.
my $dbhost = "localhost";
my $dbname = "dnsbl";
my $dbuser = "dnsbl";
my $dbpass = "spambgone";

die "Usage: orgmove <config name> <old orgid> <new orgid>\n" if !$ARGV[2];
my $cfgname = shift @ARGV;

# Load a config ref containing DB host, name, user, and pass info based on
# from the server name + full script web path.  This allows us to host
# multiple instances without having to duplicate the code.
# This file is a Perl fragment to be processed inline.
if (-e "/etc/dnsbl/$cfgname.conf") {
  my $cfg = `cat /etc/dnsbl/$cfgname.conf`;
  ($cfg) = ($cfg =~ /^(.+)$/s);         # avoid warnings, failures, and general nastiness with taint mode
  eval $cfg;
}

my $dbh = $dnsbl->connect($dbhost, $dbname, $dbuser, $dbpass);

$dbh->{AutoCommit} = 0;

my $sth1 = $dbh->prepare("update blocks set orgid=? where orgid=?");
my $sth2 = $dbh->prepare("delete from orgs where orgid=?");

print "updating orgid $ARGV[0] to $ARGV[1]...\n";
$sth1->execute($ARGV[1],$ARGV[0]) or die "died horribly: $!\n";
print "failed org change ($ARGV[0] -> $ARGV[1]): ".$sth1->errstr."\n" if $sth1->err;
$sth2->execute($ARGV[0]);
print "failed org delete ($ARGV[0]): ".$sth2->errstr."\n" if $sth2->err;

$dbh->commit;
