/* @(#)90    1.15  src/examples/type_mgr/tm_admin.c, examples.src, os2dce21.dss, 960602a.1 2/16/96 11:41:07 */
/*
 * COMPONENT_NAME:  examples.src
 *
 * FUNCTIONS:
 *
 * ORIGINS: 27
 *
 * (C) COPYRIGHT International Business Machines Corp. 1992, 1994
 * All Rights Reserved
 * Licensed Materials - Property of IBM
 *
 * US Government Users Restricted Rights - Use, duplication or
 * disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *
 */

/*********************************************************************
 *  File      :  tm_admin.c                                          *
 *********************************************************************
 *                                                                   *
 *  Functions :  main()                                              *
 *                                                                   *
 *  Comments  :  RPC management application program.  This file      *
 *               serves as a skeleton that gives the management      *
 *               program a consistent look and feel.                 *
 *                                                                   *
 *********************************************************************/

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#ifdef IBMOS2
# include <os2def.h>
#endif
#include "type_mgr.h"
#include <dce/dce_error.h>
#include <dce/rpc.h>
#include <util.h>
#include <bind_util.h>
#include "tm_admin_if.h"
#include "tm_admin.h"

/*
 *  Status Messages.
 */

char *tm_msg[] =
{
"Success",
"Failure",
"No match",
"No more entries",
"Object already exists",
"Coding error",
"Object database created",
"Object database restarted",
"Object database initialization in progress",
"Object database unititialized"
};

/*
 *  Global variable used to identify the process.  It can be overridden
 *  by the -n <name> command line argument.
 */

char  g_prog_tag[PROG_TAG_LEN] = "TM_ADMIN";

extern unsigned32 admin_calls( rpc_binding_handle_t *binding_h );
extern unsigned32 admin_args( unsigned32 argc, char *argv[] );


/*********************************************************************
 *   Function    :  main()                                           *
 *********************************************************************
 *                                                                   *
 *   Description :  Skeleton control of the tm_admin program. Issues *
 *                  calls to sub-routines that obtain a binding      *
 *                  handle, and make the RPC management calls.       *
 *                                                                   *
 *   Returns     :  SUCCESS / FAILURE                                *
 *                                                                   *
 *********************************************************************/

int main( unsigned32  argc,
                 char        *argv[] )
{
    unsigned32           status = 0;
    boolean32            done = FALSE;
    rpc_binding_handle_t binding_handle;
    binding_ctx_handle_t binding_context;
    char                 entry_name[ENTRY_LEN];

    /*
     *  Parse the command line arguments.  The main program does not
     *  deal with them.  See tm_admin_args.c for how cmd line args are
     *  used.
     */

    if (status = admin_args( argc, argv ))
    {
        goto QUICK_EXIT;
    }

    /*
     *  If this is an operation to list the available servers, then
     *  no RPC call is actually made.  Therefore we can bypass all
     *  of the binding procedures and just make the request to list
     *  the servers.
     */

    if ( g_operation == LIST_SERVER_MODE )
    {
        status = admin_calls( &binding_handle );
        goto QUICK_EXIT;
    }

    /*
     *  Create a full pathname to the server entry we want
     *  to lookup in the namespace.
     */

    sprintf( entry_name,
             "%s%s",
             g_server_ns_directory_path,
             g_server_ns_entry );

    /*
     *  Start the process of obtaining a binding handle.
     */

    if (status = get_binding_begin( entry_name,
                                    tm_admin_v1_0_c_ifspec,
                                    NULL,
                                    TRUE,
                                    NULL,
                                    &binding_context ))
    {
        goto CLEANUP;
    }

    /*
     *  We cycle thru the available binding handles, trying each one
     *  until either:  1) the RPC calls succeed, or 2) all of the
     *  binding handles have been tried.
     */

    while (!done)
    {
        if (status = get_binding_next( binding_context,
                       NULL,
                                       &binding_handle ))
        {
            goto CLEANUP;
        }

        if ((status = admin_calls( &binding_handle )) == rpc_s_ok)
        {
            done = TRUE;
        }

        rpc_binding_free( &binding_handle,
                          &status );

        CHECK_STATUS( "rpc_binding_free()\n",
                      status,
                      RETURN );
    } /* end while */

CLEANUP:

    /*
     *  Free up the binding context.
     */

    if (status = get_binding_done( &binding_context ))
    {
        /* ??? */
    }

QUICK_EXIT:

    if ( status )
    {
        status = 1;
    }

    return( status );

} /* end main() */

/* EOF tm_admin.c */

