#!/usr/bin/perl

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

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

use SMT::CLI;
use SMT::Utils;
use File::Basename;

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

sub help
{
    print "$FindBin::Bin/smt " . __("show help\n");
    opendir(DIR, $FindBin::Bin) || die "can't opendir '$FindBin::Bin': $!";
    my @files = grep { /^smt-.+/ && -x "$FindBin::Bin/$_" } readdir(DIR);
    closedir DIR;

    print __("Subscription Management Tool (SMT). (c)2007-2015 SUSE LINUX GmbH, Nuernberg, Germany.\n\n");

    foreach my $cmd (@files)
    {
        $cmd =~ s/smt\-(.+)/$1/;

        print basename($0) . " $cmd\n";
    }

    print __("\nUse smt help <command> for more help\n");
    exit 0;
}

sub executeCommand
{
  my $name = shift;
  my @options = @_;

  my $exec = "$FindBin::Bin/smt-$name";
  my $cmd = "$exec ";

  # quote all the options, otherwise whithespace causes problems
  foreach (@options)
  {
    $_ =~ s/\'/\\\'/;
    $cmd .= "'$_' ";
  }

  #print "execute '$cmd'\n";
  if ( -e $exec )
  {
      exec($cmd);
  }
  else
  {
    die "$cmd command not installed\n";
  }
}

if (defined $ARGV[0] )
{
    my $cmd = shift(@ARGV);
    #print "cmd: $cmd\n";
    if ( $cmd eq "help" || $cmd eq "-h" || $cmd eq "--help")
    {
        if (defined $ARGV[0] )
        {
            my $targethelp = shift(@ARGV);
            executeCommand($targethelp,"-h");
        }
        else
        {
            help();
        }
    }
    else
    {
        executeCommand($cmd, @ARGV)
    }
}
else
{
    help();
}

exit 0;

#
# Manpage
#


=head1 NAME

smt - "Subscription Management Tool" for SLE Maintenance

=head1 SYNOPSIS

smt [help|--help|-h] <subcommand> [options]

=head1 DESCRIPTION

smt calls various subcommands to administrate your SMT Server.


=head1 OPTIONS

=head2 help --help -h

If this option is used without subcommand, it shows the list of available subcommands.
If this option is used with a subcommand, it shows the help text of the subcommand.

=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

