#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/bin/cdat/cdat.pl 1.5 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2010,2011 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)75    1.5  src/bos/usr/bin/cdat/cdat.pl, cdat, bos720 7/14/11 17:40:11
use warnings;
use strict;
use POSIX;		# needed for exec

use lib '/usr/lib/cdat/';
use cdat;
use messages;

#
# Constants.
#
my $PERL = '/usr/bin/perl';
my $CMDPREFIX = 'cdat-';
# absolute path name where sub-commands are located
my $CMDPATH = '/usr/lib/cdat';

#
# List of supported sub-commands along with their associated script names.
#
my %handlers = (
    'init'		=> 'init',
    'discover-nodes'	=> 'discover-nodes',
    'access'		=> 'access',
    'delete'		=> 'delete',
    'check'		=> 'check',
    'collect'		=> 'collect',
    'show'		=> 'show',
    'list-nodes'	=> 'list-nodes',
    'list-types'	=> 'list-types',
    'archive'		=> 'archive'
);

######################################################################
# Function:	usage
# Purpose:	Display usage.
# Tasks:	Print usage and exit.
# Input:	None
# Output:	None
######################################################################
sub usage
{
    printf(STDERR catgets(MSG_CDAT_USAGE,
	"Usage: cdat sub-command [options]\n".
	"Available sub-commands:\n".
	"\tinit                Initialize the repository\n".
	"\tshow                Display the content of the repository\n".
	"\tcheck               Check consistency of the repository\n".
	"\tdelete              Remove collects from the repository\n".
	"\tdiscover-nodes      Find LPARs or WPARs from a list of HMCs or LPARs\n".
	"\tlist-nodes          Display the list of configured nodes\n".
	"\taccess              Manage remote nodes authentication\n".
	"\tcollect             Collect data from remote nodes\n".
	"\tlist-types          Display the list of supported collect types\n".
	"\tarchive             Create a compressed archive of collects\n"));
    exit(1);
}

######################################################################
# Function:	main
# Purpose:	Entry point of the cdat command.
# Tasks:	Parse command line and execute sub-command.
# Input:	Sub-command name and parameters.
# Output:	None
######################################################################
sub main
{
    # Make sure at least one parameter (sub-command) is specified
    usage() if (@ARGV < 1);

    my $sub = shift @ARGV;

    my $handler = $handlers{$sub};
    if (!defined($handler)) {
	printf(STDERR catgets(MSG_UNKNOWN_SUBCOMMAND,
	    "Unknown sub-command: '%s'.\n\n"), $sub);
	usage();
    }

    # Prepend prefix and check that sub-command script exists
    $handler = "$CMDPATH/$CMDPREFIX$handler";
    if (! -e "$handler") {
	printf(STDERR catgets(MSG_IS_MISSING,
	    "%s is missing.\n"), $handler);
	exit(1);
    }

    # Execute sub-command.
    # The sub-command options are passed directly to the script.
    # They are parsed in the script using GetOptions.
    exec($PERL, "-I$CMDPATH/", "--", $handler, @ARGV) or
    printf(STDERR catgets(MSG_CANNOT_EXECUTE,
        "%s: Cannot execute %s.\n"), $sub, $handler);
}

main;