#!/usr/bin/perl -w

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

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

use Getopt::Long;
use File::Basename;

use Text::ASCIITable;
use Locale::gettext();
use POSIX();

use SMT::CLI;
use SMT::Utils;
use SMT::Client;

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 $logfile	= "/dev/null";
my $help	= 0;
# No command defined, use fallback
my $command	= $ARGV[0] || '';
my $hostname	= undef;
my $verbose = 0;
my $guid = undef;
my $severity = undef;

Getopt::Long::Configure( 'no_auto_abbrev');
my $optres = Getopt::Long::GetOptions
(
 'help|h'		=> \$help,
 'debug|d'		=> \$debug,
 'logfile|L=s'	=> \$logfile,
 'hostname|n=s'	=> \$hostname,
 'verbose|v'	=> \$verbose,
 'guid|g=s' => \$guid,
 'severity|s=s' => \$severity
);

# No command defined, but some arguments are, using fallback
if (defined $command && $command =~ /^-/)
{
    $command = '';
}

if($help || !$optres)
{
    print basename($0). " status [--hostname <hostname>]\n";
    print "\n";
    print __("Options:")."\n";
    print "--help         -h         ".__("This help.")."\n";
    print "--debug        -d         ".__("Enable debug mode.")."\n";
    print "--hostname     -n <name>  ".__("Filter the result by the hostname (hostname starting with <name>).")."\n";
    print "--guid         -g <guid>  ".__("Only show information for GUID <guid>.")."\n";
    print "--severity     -s <level> ".__("Only show information if patches for <level> are available.")."\n";
    print "                          ".__("<level> may be a value of: 'packagemanager', 'security', 'recommended' or 'optional'")."\n";
    print "--verbose      -v         ".__("Show details on patches.")."\n";
    print "--logfile      -L         ".__("Log to a specified file.")."\n";
    exit 0;
}

# get a lock
#if(!SMT::Utils::openLock("smt-client"))
#{
#    print __("Other smt-client process is still running.\n");
#    exit 2;
#}

# open the logfile
$vblevel = LOG_ERROR|LOG_WARN|LOG_INFO1|LOG_INFO2|LOG_DEBUG|LOG_DEBUG2 if($debug);
my $log = SMT::Utils::openLog($logfile);

# connect to database
my ($cfg, $dbh) = SMT::CLI::init();
if (! defined $dbh) {
    print __("Cannot connect to database");
    exit 3;
}

my $clients = SMT::Client->new({'dbh' => $dbh});

sub doesHostnameMatch ($) {
    my $check_hostname = shift;

    return (substr ($check_hostname, 0, length($hostname)) eq $hostname);
}

if ($command eq '' || $command eq 'status')
{
    my $serverityFilter = undef;
    if ( defined $severity )
    {
        $serverityFilter = '>0:::' if ( $severity eq 'packagemanager');
        $serverityFilter = ':>0::' if ( $severity eq 'security');
        $serverityFilter = '::>0:' if ( $severity eq 'recommended');
        $serverityFilter = ':::>0' if ( $severity eq 'optional');
        if ( ! defined $serverityFilter )
        {
            print __("Unknown severity level.")."\n";
            exit 2;
        }
    }

    my $filter = { 'selectAll' => '' };
    ${$filter}{'GUID'} = $guid if ( defined $guid && $guid ne '' );
    ${$filter}{'PATCHSTATUS'} = $serverityFilter if ( defined $serverityFilter );

    my $client_info = $clients->getClientsInfo($filter);
    # skip rendering if an empty hash is detected
    if ( keys %{$client_info})
    {
	my $t = new Text::ASCIITable;

	if ($verbose) {
	    $t->setCols(
		__("GUID"),
		__("Hostname"),
		__("Patch Status"),
		__("Security\nPatches"),
		__("Package Manager\nPatches"),
		__("Recommended\nPatches"),
		__("Optional\nPatches"),
		__("Patch Status Date"),
		__("Last Contact")
	    );
	} else {
	    $t->setCols(
		__("GUID"),
		__("Hostname"),
		__("Patch Status"),
		__("Patch Status Date")
	    );
	}

	my ($key, $status_label);

	foreach my $key (sort ({$client_info->{$a}->{HOSTNAME} cmp $client_info->{$b}->{HOSTNAME}} keys %{$client_info})) {
	    next if (defined $hostname && ! doesHostnameMatch($client_info->{$key}->{HOSTNAME}));

	    # Function returns two values
	    ($status_label) = SMT::Client::getPatchStatusLabel($client_info->{$key});

	    if ($verbose) {
		$t->addRow(
		    $client_info->{$key}->{GUID},
		    $client_info->{$key}->{HOSTNAME},
		    $status_label,
		    (defined $client_info->{$key}->{PATCHSTATUS_S} ? $client_info->{$key}->{PATCHSTATUS_S}:__("Unknown")),
		    (defined $client_info->{$key}->{PATCHSTATUS_P} ? $client_info->{$key}->{PATCHSTATUS_P}:__("Unknown")),
		    (defined $client_info->{$key}->{PATCHSTATUS_R} ? $client_info->{$key}->{PATCHSTATUS_R}:__("Unknown")),
		    (defined $client_info->{$key}->{PATCHSTATUS_O} ? $client_info->{$key}->{PATCHSTATUS_O}:__("Unknown")),
                    $client_info->{$key}->{PATCHSTATUS_DATE},
		    $client_info->{$key}->{LASTCONTACT}
		);
	    } else {
		$t->addRow(
		    $client_info->{$key}->{GUID},
		    $client_info->{$key}->{HOSTNAME},
		    $status_label,
                    $client_info->{$key}->{PATCHSTATUS_DATE}
		);
	    }
	}

	print $t->draw();
    }
}
else
{
    SMT::Utils::printLog($log, $vblevel, LOG_ERROR, sprintf(__("Unknown command: %s"), $command));
}

# release the lock
#SMT::Utils::unLockAndExit( "smt-client", 0, $log, $vblevel );

exit 0;

########################################################################################
#
# Manpage
#

=head1 NAME

smt-client

=head1 SYNOPSIS

smt-client [COMMAND] [OPTION [OPTION ...]]

=head1 DESCRIPTION

C<smt-client> shows information about registered clients. The information includes the GUID, hostname, patch status and the timestamps of the patch status and the last contact of the client with the SMT server.

=head1 COMMAND

Currently only the command B<status> is supported. It is the default and can be omitted.

=head1 OPTIONS

=head2 BASIC OPTIONS

=over

=item -h, --help

Shows the help screen.

=item -v, --verbose

Shows detailed client information.
The patch status is explained in detail and shows the number of patches for the package manager, security, recommended and optional patches. The last contact date is also shown in verbose mode.

=item -d, --debug

Enables debug mode.

=item -L, --logfile <file>

Logs will be written to <file>.

=back

=head2 FILTER OPTIONS

=over

=item -h, --hostname <name>

Filters the result by the hostname. Only entries with a hostname beginning with <name> will be listed.

=item -g, --guid <guid>

Filters the result by the GUID. Only the entry with the GUID <guid> will be listed.

=item -s, --severity <level>

Filters the result by the patch status information. The value <level> may be one of B<packagemanager>, B<security>, B<recommended> or B<optional>.
Only entries are listed that have patches for the respective level.

=back

=cut

=head1 AUTHORS and CONTRIBUTORS

Lukas Ocilka, Jens Daniel Schmidt

=cut


=head1 LICENSE

Copyright (c) 2009-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
