#!/usr/bin/perl

###############################################################################
## Copyright (c) 2007-2012 SUSE LINUX Products GmbH, Nuernberg, Germany.
###############################################################################

use strict;
use warnings;
use FindBin;
BEGIN { unshift @INC, "$FindBin::Bin/../www/perl-lib" }

use SMT::Utils;
use SMT::SCCSync;
use Getopt::Long;
use File::Basename;
use Data::Dumper;

use Locale::gettext ();
use POSIX ();     # Needed for setlocale()

POSIX::setlocale(&POSIX::LC_MESSAGES, "");

if(!SMT::Utils::dropPrivileges())
{
    print STDERR __("Unable to drop privileges. Abort!\n");
    exit 1;
}

my $vblevel  = LOG_ERROR|LOG_WARN|LOG_INFO1|LOG_INFO2;
my $debug = 0;
my $help  = 0;
my $logfile = "/dev/null";

my @guids = ();

my $optres = GetOptions ("guid|g=s"    => \@guids,
                         "logfile|L=s" => \$logfile,
                         "debug|d"     => \$debug,
                         "verboselevel|v=i" => \$vblevel,
                         "help|h"      => \$help
                        );

if($help || !$optres)
{
    print basename($0) . __(" [OPTIONS]\n");
    print __("Delete one or more registration from SCC and SMT\n");
    print "\n";
    print __("Options:\n");
    print "  --guid (-g)            : " . __("Unique ID of the client which should be deleted.\n");
    print "                           " . __("This option can be used multiple times.\n");
    print "  --debug (-d)           : " . __("enable debug mode\n");
    print "  --logfile (-L) <file>  : " . __("Path to logfile")."\n";
    exit 0;
}

$vblevel = LOG_ERROR|LOG_WARN|LOG_INFO1|LOG_INFO2|LOG_DEBUG|LOG_DEBUG2 if($debug);

if(@guids == 0)
{
    print STDERR __("No Unique ID provided.\n");
    exit 1;
}

if(!SMT::Utils::openLock("smt-delete-registration"))
{
    print __("Delete registration process is still running.\n");
    exit 0;
}

# open the logfile

my $LOG = SMT::Utils::openLog($logfile);

my $useragent = SMT::Utils::createUserAgent('log' => $LOG, 'vblevel' => $vblevel);
$useragent->protocols_allowed( [ 'https' ] );

my $cfg = undef;

eval
{
    $cfg = SMT::Utils::getSMTConfig();
};
if($@ || !defined $cfg)
{
    SMT::Utils::printLog($LOG, $vblevel, LOG_ERROR, sprintf(__("Cannot read the SMT configuration file: %s"), $@));
    SMT::Utils::unLockAndExit( "smt-delete-registration", 1, $LOG, $vblevel );
}

my $dbh = undef;

$dbh = SMT::Utils::db_connect();

if(!$dbh)
{
    if(!SMT::Utils::unLock("smt-delete-registration"))
    {
        print STDERR __("Cannot remove lockfile.\n");
    }
    die __("Cannot connect to database");
}

eval
{
    my $err = 1;
    if($cfg->val("NU", "ApiType", "NCC") eq "NCC")
    {
        SMT::Utils::printLog($LOG, $vblevel, LOG_ERROR, __("NCC is not supported by this version of SMT"));
    }
    else
    {
        my $sccreg = SMT::SCCSync->new(vblevel   => $vblevel,
                                       useragent => $useragent,
                                       log       => $LOG,
                                       dbh       => $dbh);
        $err = $sccreg->delete_systems(@guids);
    }
    if($err != 0)
    {
        SMT::Utils::unLockAndExit( "smt-delete-registration", 1, $LOG, $vblevel );
    }
};
if($@)
{
    print STDERR __("Failed to delete registration for id $@\n");
    SMT::Utils::unLockAndExit( "smt-delete-registration", 1, $LOG, $vblevel );
}

SMT::Utils::unLockAndExit( "smt-delete-registration", 0, $LOG, $vblevel );


#
# Manpage
#

=head1 NAME

smt delete-registration

=head1 SYNOPSIS

smt [help|--help|-h] delete-registration

smt delete-registration --guid|-g id1 [--guid|-g id2] ...

smt delete-registration --debug --guid id1

=head1 DESCRIPTION

I<smt delete-registration> deletes one or more registrations from SMT and SCC. It will deregister machines from the system.

=head1 OPTIONS

=head2 MACHINE GUID

=over

=item --guid|-g id

Deletes the machine with guid B<id> from the system. This parameter can be used multiple times.

=back

=head2 DEBUG

=over

=item --debug|-d

Enable debug mode for verbose logging.

=item --logfile|-L file

Write log messages to B<file>.

=back


=head1 AUTHORS and CONTRIBUTORS

Duncan Mac-Vicar Prett, Lukas Ocilka, Jens Daniel Schmidt, Michael Calmer

=head1 LICENSE

Copyright (c) 2007-2012 SUSE LINUX Products GmbH, Nuernberg, Germany.

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 2 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, write to the Free Software Foundation, Inc., 675 Mass
Ave, Cambridge, MA 02139, USA.

=cut

