/* @(#)88    1.13.1.1  src/examples/type_mgr/server_args.c, examples.src, os2dce21.dss, 960602a.1 1/11/96 10:21:36 */
/*
 * 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      :  server_args.c                                       *
 *********************************************************************
 *                                                                   *
 *  Functions :  server_args()                                       *
 *                                                                   *
 *  Comments  :  Routines that read and interpret command line args. *
 *               Global variables are set, based on the cmd line     *
 *               args, and then used by the server process when      *
 *               setting itself up, etc...                           *
 *                                                                   *
 *********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#ifdef IBMOS2
# include <os2def.h>
#endif
#include "cust_if.h"
#include "type_mgr.h"
#include "util.h"
#include "server.h"
#include <gethost.h>

#define PRINT_USAGE \
    PRINT("\tUsage: %s -n [server name] -z\n",argv[0]); \

/*
 *  Global variables that are used throughout the program.  Any variables
 *  defined here should have extern definitions in server.h, so the
 *  global vars can be accessed anywhere in the server program.
 */

char       g_prog_tag[PROG_TAG_LEN] = "SERVER";
char       g_server_ns_entry[ENTRY_LEN];
char       g_object_ns_directory_path[ENTRY_LEN];
char       g_server_ns_directory_path[ENTRY_LEN];
boolean32  g_trace;

extern int getopt(int argc, char *argv[], char *opstring);


/*********************************************************************
 *   Function    :  server_args()                                    *
 *********************************************************************
 *                                                                   *
 *   Description :  Parse the command line arguments, using the      *
 *                  getopt() function.  Sets the appropriate         *
 *                  variables where necessary.  Variables can then   *
 *                  be used in other parts of the program.           *
 *                                                                   *
 *   Returns     :  (0) success, or appropriate error code.          *
 *                                                                   *
 *********************************************************************/

unsigned32 server_args( unsigned32 argc,
                        char *argv[] )
{
    extern char        *optarg;
    extern unsigned32  optind;
    unsigned32         c;
    unsigned32         error = 0;
    char               host_name[ENTRY_LEN];

    /*
     *  If no command line args, print the command usage.
     */

    if (argc < 2)
    {
        PRINT_USAGE;
        return(++error);
    }
    /*
     *  The namespace directory is pre-defined.
     */

    g_trace = FALSE;
    strcpy( g_object_ns_directory_path, NAME_SERVICE_PATH );
    sprintf( g_server_ns_directory_path, "%s%s",
                NAME_SERVICE_PATH, NAME_SERVICE_TM_SERVER_PATH );
    strcpy( g_server_ns_entry, "" );

    while ((c = getopt( argc, argv, "n:z" )) != EOF )
    {
        switch (c)
        {
            case 'n': /* server entry name */
                strncpy( g_prog_tag,
                         optarg,
                         PROG_TAG_LEN-1 );
                /* get the hostname */
                if( gethostname(host_name,sizeof(host_name))){
                        perror("gethostname");
                        exit(1);
                }

                /* check the server name and create the actual entry name.
                   Each server's entry name should contain the information
                   which host it is running. */

                if ( ( strlen( optarg ) +
                          strlen( host_name ) + 1 ) > ENTRY_LEN )
                {
                    PRINT("The server name is too long.\n");
                    error++;
                }
                else
                {
                    sprintf( g_server_ns_entry,
                             "%s@%s",
                             optarg,
                             host_name );
                }

                break;

            case 'z': /* tracing */
                g_trace = TRUE;
                break;

            case '?':
                error++;
                break;

            default:
                PRINT("Invalid command line arguments!\n");
                PRINT_USAGE;
                return(++error);

        } /* end switch */

        if ( error )
        {
            PRINT_USAGE;
            return(error);
        }

    } /* end while */

    /*
     *  A server ns entry name must always be specified.
     */

    if (!strcmp( g_server_ns_entry, "" ))
    {
        PRINT("You must specify a server ns entry!\n");
        PRINT_USAGE;
        return(++error);
    }

    return(error);

} /* end server_args() */

/* EOF server_args.c */

