#!/usr/bin/perl
# move blocks listed with one org to another.  intended for orgs that have several bytestrings as names in WHOIS.
##
# $Id: orgmove 73 2025-09-05 20:04:46Z kdeugau $
# Copyright 2009-2010,2025 Kris Deugau <kdeugau@deepnet.cx>
# 
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version. 
# 
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
# 
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
##

use strict;
use warnings;
use DBI;

# push "the directory the script is in" into @INC
use FindBin;
use lib "$FindBin::RealBin/";

use DNSBL 3.0;

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

my $dnsbl = new DNSBL (configfile => "/etc/dnsbl/$cfgname.conf");
$dnsbl->connect;

$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;
