#!/usr/bin/perl

use strict;
use warnings;
use LATMOS::Accounts;
use Getopt::Long;
use Pod::Usage;

=head1 NAME

    la-sql-rename-host - rename an network host

=head1 SYNOPSIS

    la-sql-rename-host [--no-cname] from to

Example : la-rename-host foo bar

=cut

GetOptions(
    'c|config=s'        => \my $config,
    'b|base=s'          => \my $base,
    'no-cname'          => \my $nocname,
    'help'              => sub { pod2usage(0) },
) or pod2usage();

=head1 OPTIONS

=over 4

=item -c|--config configdir

Use this configuration directory instead of the default one.

=item -b|--base basename

Query this specific base instead of the default one.

=item --no-cname

By default the old name is added as CNAME for compatibility, this option don't
add previous name as CNAME.

=back

=cut

my $LA = LATMOS::Accounts->new($config, noacl => 1);
my $labase = $LA->base($base);
$labase && $labase->load or die "Cannot load base";

$labase->unexported(1);

my ($from, $to) = @ARGV or pod2usage();

if ($labase->rename_nethost($from, $to, addcname => (!$nocname))) {
    $labase->commit;
    print "Host succefully rename from $from to $to\n";
    exit(0);
} else {
    warn "Error while renaming $from\n";
    exit(1);
}
