/*************************************************************
/*
/* Program rget
/*
/* This program contains an example of how the user can
/* retrieve the value of a variable of a
/* remote process in the KLOE DAQ environment.
/* Using the option -u,
/* a command is sent to the remote process to update the
/* value written in the remote shared memory. The more
/* To read both remote and local variable use the general
/* utility getvalue.
/*
/* Usage:
/*
/*       rget [-u] process@node variable_name
/*
/* Created for daqctl version 2
/* E. Pasqualucci 6-5-1997
/*
/* 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"

/* Process template include files */

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

/* kloe specific include files */

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

/* global variables */

extern char ownpname[];

char update = FALSE;
char command[] = "update ";

char *variable = (char *) NULL;
char *remote_node = (char *) NULL;
char *process = (char *) NULL;
char *msg;

char **rvar_name = (char **) NULL;

struct snmp_session *sess = (struct snmp_session *) NULL;

void rget_help (void)
{
    fprintf (stderr, " Usage: rget [-u] process@node variable_name\n");
    exit (0);
}

void rget_init_options (int argc, char **argv)
{
    register int i;
    register int arg;
    char *tmp[2];

    for (i=0 ; i < 2 ; ++i)
        tmp[i] = (char *) NULL;

    for (arg = 1, i = 0; arg < argc; arg++)
        if (argv[arg][0] == '-')
            switch(argv[arg][1])
            {
                case 'u':
                    update = TRUE;
                    break;
                case 'h':
                    rget_help();
                    break;
                default:
                    printf ("invalid option: -%c\n", argv[arg][1]);
                    break;
            }
        else
	{
            tmp[i++] = argv[arg];
            if (i > 2)
                printf ("warning: too many parameters\n");
        }

    process = tmp[0];
    variable = tmp[1];

    if (variable == (char *) NULL || process == (char *) NULL)
        rget_help();
}

void rget_init (void)
{
    msg = (char *) calloc (sizeof(command) + sizeof(variable) + 1,
                           sizeof(char));

    strcpy (msg, command);
    strcat (msg, variable);

    if (!strchr (process, (int) '@'))
    {
        if (Attach_Table() == -1)
        {
            printf("Error in Attach_Table()\n");
            exit(0);
        }
    }
    else
    {
        ksnmp_init();
        init_mib();
    }

    strcpy (ownpname, "rget");
}

void rget_do (void)
{
    register int i;
    short stpe;
    char *value;
    byte_seq *bseq;

    if (update)
        if (Send_Message (process, msg) != SUCCESS)
            ErrorSet (0, "rget_do", "Error: Remote variable was not updated");

    ksnmp_vars_read_variable (process, variable, &stpe, &value);

    printf ("variable type %d\n", stpe);

    switch (stpe)
    {
        case INTEGER:
            printf ("variable value %d\n", *(int *) value);
            break;
        case REAL:
            printf ("variable value %f\n", *(float *) value);
            break;
        case BYTESEQ:
            bseq = (byte_seq *) value;
            printf ("variable size %d\n", bseq->size);
            printf ("variable value (first bytes) %x %x %x %x\n",
                    bseq->btext[0], bseq->btext[1],
                    bseq->btext[2], bseq->btext[3]);
            break;
        case STRING:
            printf ("variable value %s\n", value);
            break;
    }
}

void rget_end (void)
{
    ksnmp_end();
}

main (int argc, char **argv)
{
/*  This is an example of how to retrieve a variable of a process */

    rget_init_options (argc, argv);
    rget_init();
    rget_do();
    rget_end();
}