/*************************************************************
/*
/* Program snmpset
/*
/* This program contains an example of how to use
/* the routines contained in the ksnmplib to perform
/* a set operation on a remote node.
/* Calls the KSNMPLIB routines.
/*
/* E. Pasqualucci 2-5-1996
/*
/* Modified for daqctl v2
/* E. Pasqualucci 23-5-1997
/*
/*************************************************************

/* standard include files */

#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <ctype.h>
#include <netdb.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"

/* environmental and global variables */

char *remote_node = (char *) NULL;
char *password = (char *) NULL;
char *variable_name = (char *) NULL;
char *variable_value = (char *) NULL;

int *variable_value_int = (int *) NULL;

int port = 2001;
int vartype = STRING;

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 'c':
                    arg++;
                    password = argv[arg];
                    break;
                case 'p':
                    arg++;
                    port = atoi (argv[arg]);
                    break;
                case 'h':
                    arg++;
                    remote_node = argv[arg];
                    break;
                case 'n':
                    arg++;
                    variable_name = argv[arg];
                    break;
                case 'v':
                    arg++;
                    variable_value = argv[arg];
                    if (isdigit ((int) variable_value[0]))
                        vartype = INTEGER;
                    break;
                case 'i':
                    vartype = IPADDRESS;
                    break;
                case 'o':
                    vartype = OBJID;
                    break;
                case 's':
                    vartype = STRING;
                    break;
                default:
                    printf("invalid option: -%c\n", argv[arg][1]);
                    break;
            }

    if (password == (char *) NULL &&
        remote_node == (char *) NULL &&
        variable_name == (char *) NULL &&
        variable_value == (char *) NULL)
    {
        fprintf (stderr, " Usage: snmpset %s%s\n",
                 "-h host -c community -n variable_name ",
                 "-v value [-i] [-o] [-s]");
        exit (0);
    }

    if (remote_node == (char *) NULL)
    {
        printf (" Please supply host name : ");
        scanf ("%s", remote_node);
    }

    if (password == (char *) NULL)
    {
        printf (" Please supply community name : ");
        scanf ("%s", password);
    }

    if (variable_name == (char *) NULL)
    {
        printf (" Please supply variable name : ");
        scanf ("%s", variable_name);
    }

    if (variable_value == (char *) NULL)
    {
        printf (" Please supply variable value: ");
        scanf ("%s", variable_value);
        if (isdigit ((int) variable_value[0]))
            vartype = INTEGER;
    }

    if (vartype == INTEGER)
    {
        variable_value_int = calloc ((size_t) 1, sizeof (int));
        *variable_value_int = atoi (variable_value);
        variable_value = (char *) variable_value_int;
    }
}

main (int argc, char **argv)
{
/*  This is an example of how the user can call the ksnmplib routines */
/*  to set a MIB variable on a remote node.                           */

    int ierr;
    int nvar = 1;
    struct snmp_pdu *cmd_pdu;
    struct snmp_session *ss;

    ksnmp_init();
    init_mib();

    init_options (argc, argv);

    if ((ss = ksnmp_session_init (remote_node, password, port)) ==
        (struct snmp_session *) NULL)
        fprintf (stderr, " Error in opening snmp session on node %s\n",
                 remote_node);

    cmd_pdu = ksnmp_set (ss, &variable_name, nvar, &vartype, &variable_value);
    cmd_pdu = ksnmp_get (ss, &variable_name, nvar);

    ksnmp_end();
}