#include <stdio.h>
#include <string.h>
#include "sap_moni.h"
#include "sap_moni_ccm.h"
#include "agnt_util.h"


/*=======================================================================
  (C) Copyright SAP AG, 2002

         
 Author  : Dostert/SAP
 Project : Get data from R/3 using XMB
 Using   : RFCSDK 
 ========================================================================*/
void usage(char *progname) {
        #define BL printf(



         BL"Agent \n\n");
         BL"Syntax: %s tid thr_g2y thr_r2y thr_y2g thr_y2r tid_specific <RFC-Template>\n\n",  progname     );


         BL"       <RFC-Template> defined in %s\n", AGENT_LOGIN_FILE  );
}

int main(int argc, char **argv) {
	RFC_HANDLE rfc_handle;
	int thr_g2y,
            thr_r2y,
            thr_y2g,
            thr_y2r;	
	//char * variante = "NAGIOS";
	char tid_specific = ' ';

	BAPITID tid;
	char *ptr;	

	if (argc < 7) {
		usage(argv[0]);
		return AG_INVALID_ARGUMENTS;
	}



	if( sapmon_connect(argv[7], &rfc_handle) >=  AG_FIRST_ERROR) {
		printf("Could not log on\n");
		return AG_CALL_INVALID;
	}

	thr_g2y = atoi(argv[2]);
	thr_r2y = atoi(argv[3]);
	thr_y2g = atoi(argv[4]);
	thr_y2r = atoi(argv[5]);

	if (strcmp(argv[6], "0") == 1)
		tid_specific = 'X';  

 	 ptr = (char *)url_decode(argv[1]);

         memset(&tid, ' ', sizeof(tid));

         strncpy(tid.Mtsysid, ptr, 8);
	 ptr += 8;
         strncpy(tid.Mtmcname, ptr, 40);
	 ptr += 40;
         strncpy(tid.Mtnumrange, ptr, 3);
	 ptr += 3;
         strncpy(tid.Mtuid, ptr, 10);
	 ptr += 10;
         strncpy(tid.Mtclass, ptr,3);
	 ptr += 3;
         strncpy(tid.Mtindex, ptr, 10);
	 ptr += 10;
         strncpy(tid.Extindex, ptr, 10);
	

	bapi_system_mte_setperfprop(	&rfc_handle, 
				 	&tid,
					&thr_g2y,
					&thr_r2y,
					&thr_y2g, 
					&thr_y2r,
					&tid_specific,
					NULL);
					//variante);


	//Log off
	sapmon_disconnect(&rfc_handle);	
	
	return SAP_OK;
}
