/*************************************************************
/*
/* Program toproc
/*
/* This program contains an example of how the user can
/* send a command to a process on a remote KLOE node
/* using the routines contained in the library ksnmplib.
/*
/* E. Pasqualucci 15-4-1996
/*
/* Modified for daqctl v2
/* E. Pasqualucci 23-5-1997
/*
/*************************************************************

/* standard include files */

#include <stdio.h>

/* snmp include files */

#include "snmp.h"
#include "asn1.h"
#include "snmp_impl.h"
#include "snmp_api.h"

/* kloe specific include files */

#include "netmap.h"
#include "ksnmplib_protos.h"

#include "template.h"
#include "dummy.h"

/* parameters */

#include "ksnmp.h"

#define MAX_MSG_LEN 512

/* environmental and global variables */

char *command = (char *) NULL;
char *remote_node = (char *) NULL;
char *remote_process = (char *) NULL;

extern char ownpname[];

void init_options (int argc, char **argv)
{
    int arg;
    int i;

    for(arg = 1; arg < argc; arg++)
        if (argv[arg][0] == '-')
            switch(argv[arg][1])
            {
                case 'p':
                    arg++;
                    remote_process = argv[arg];
                    break;
                case 'n':
                    arg++;
                    remote_node = argv[arg];
                    break;
                case 'c':
                    arg++;
                    command = argv[arg];
                    break;
                default:
                    printf("invalid option: -%c\n", argv[arg][1]);
                    break;
            }

    if (command == (char *) NULL ||
        remote_node == (char *) NULL ||
        remote_process == (char *) NULL)
    {
        fprintf (stderr, " Usage: toproc -p process -n node -c command\n");
        exit (0);
    }
}

main (int argc, char **argv)
{
/*  This is an example of how the user can call the library routines */
/*  to send a command to KLOE process on a remote node.              */

    int ierr;
    char *process;
    char message[MAX_MSG_LEN];

    strcpy (ownpname, "toproc");

    ksnmp_init();
    init_mib();
    init_options (argc, argv);
    Init_hostname ();

    process = (char *) calloc (strlen (remote_node) + strlen (remote_process)
                               + 1, sizeof (char));

    printf ("calling rc_send_command_to_a_process\n");
    printf ("command %s\n", command);
    printf ("node    %s\n", remote_node);
    printf ("process %s\n", remote_process);

    sprintf (process, "%s@%s", remote_process,
             remote_node);

    Add_Header (command, message);

    printf ("message %s\n", message);

    ierr = ksnmp_send_message (message, process,
                               (struct snmp_session *) NULL, KSNMP_ACK_WAIT);

    ksnmp_end();
}