#include "assert.h"   /* must be first for use eith hc2 */
#include "mt.h"
#include "MTreg.h"
#include "df.h"
#include "assert.h"
#include "profile.h"
#include "stddef.h"
#include "string.h"
#include "al.h"
#include "ruleeditor.h"
#include "profile.h"

/*--------------------------------------------------*/
void mt_Init(argc,argv)
int *argc;
char **argv;
{
  int i;
  char *MTN=NULL;
  char* DMTName=NULL;
  AlMsgType MT;

  for (i=1;i<*argc;i++) {
    if (strcmp("-mt",argv[i])==0) {
      if (*argc>i) {
	MTN=argv[i+1];
/*	RapCArgs_remove(i,argc,argv);
	RapCArgs_remove(i+1,argc,argv); */
      }
      else {
	ruleeditor_Usage();
	ruleeditor_Cleanup(-1);
      }
    }
  }

  /* forst try to get the default msg type */
  if (AlProfile_has_value(ALPROF_DEFRMT)) {
    DMTName=AlProfile_get_CP_val(ALPROF_DEFRMT);
    DefaultMsgType_GR=AlMTreg_get_obj_with_name(DMTName);
  }
  else {
    Darray MTnames=Darray_create();
    AlMTreg_get_all_msg_type_names(MTnames);
    if (Darray_len(MTnames)>0) {
      DefaultMsgType_GR=AlMTreg_get_obj_with_name((char*)
						  Darray_get(MTnames,
							     (unsigned)0));
    }
  }

  if (MTN!=NULL) {
    MT=AlMTreg_get_obj_with_name(MTN);
    if (MT==NULL) {
      Al_warning1("ruleeditor: Message Type '%s' not found.\n",MTN);
      if (DefaultMsgType_GR!=NULL) {
	Al_warning1("ruleeditor: Using default Message Type %s.\n",
		    CurrentMsgTypeName_GR);
	CurrentMsgTypeName_GR=DMTName;
	CurrentMsgType_GR=DefaultMsgType_GR;
	LoadMsgType_GR=Bool_TRUE;
      }
      else
	Al_fatal_error("ruleeditor: could not find Default Message Type.\n\
Check your .lens-defaults file for the 'DefaultRuleMessageType' entry.\n");
    }
    else {
      CurrentMsgTypeName_GR=MTN;
      CurrentMsgType_GR=MT;
      LoadMsgType_GR=Bool_TRUE;
    }
  }

  else
    LoadMsgType_GR=Bool_FALSE;
}

