#include "rs.h"
#include "ru.h"
#include "df.h"
#include "RSreg.h"
#include "end.h"
#include "rem.h"
#include "profile.h"
#include "a1client.h"
#include "al.h"
#include "string.h"
#include "ruleeditor.h"
#include "repop.h"

/*--------------------------------------------------*/
void rs_Init(argc,argv)
int *argc;
char **argv;
{
  int i;
  Bool ForceIt, AsRuleSet;
  ForceIt=Bool_FALSE;
  AsRuleSet=Bool_FALSE;  /* are we getting an anyone server rule set */

  /* check to see that there are enough args */
  if (*argc<2) {
    printf("ruleeditor: Missing rule set or anyone server client name.\n");
    ruleeditor_Usage();
    ruleeditor_Cleanup(-1);
  }
  
  /* see if this is an anyone server rule set */
  for (i=1;i<*argc;i++) {
    if (strcmp("-as",argv[i])==0) {
      if (*argc>(i+1) && argv[i+1][0]!='-')  {
	RE_debug1("As client from command line =%s\n",argv[i+1]);
	AsClient_GR=argv[i+1]; 
	AsRuleSet=Bool_TRUE;
      }
      else  {
	if (AlProfile_has_value(ALPROF_DEF_ANYONE)==Bool_TRUE) {
	  AsClient_GR=AlProfile_get_CP_val(ALPROF_DEF_ANYONE); 
	  RE_debug1("As client at profile=%s\n",AsClient_GR);
	  AsRuleSet=Bool_TRUE;
	}
	else {
	  Al_warning("ruleeditor: anyone server not specified on command line and not in profile.");
	  ruleeditor_Cleanup(-1);
	}
      }
    }
  }
  
  if (AsRuleSet==Bool_FALSE) {     /* we are doing a regular rule set stuff */
    /* see if there's a force flag */ 
    for (i=1;i<*argc;i++) {
      if (strcmp("-f",argv[i])==0)
	ForceIt=Bool_TRUE;
    }
    
    /* see if its the first arg */
    if (argv[1][0]!='-')
      RuleSetName_GR=strcpy(Memory_allocate(strlen(argv[1])+1),argv[1]);
    
    /* else look for it */
    else 
      for (i=1;i<*argc;i++) {
	if (strcmp("-rs",argv[i])==0 && *argc>i) {
	  if (RuleSetName_GR!=NULL)
	    Al_fatal_error("ruleeditor:only one rule set may be specified.\n");
	  RuleSetName_GR=strcpy(Memory_allocate(strlen(argv[i+1])+1),
				argv[i+1]);
	}
      }

    /* make sure that we have a rule set at this point */
    if (RuleSetName_GR==NULL)  {
      printf("Rule Editor: Missing rule set\n");
      ruleeditor_Usage();
      ruleeditor_Cleanup(-1);
    }

    if (AlRSreg_load(RuleSetName_GR)==Bool_TRUE)
      RuleSet_GR=AlRSreg_find(RuleSetName_GR);
    else {
      int answerI;
      char answerC;
      if (ForceIt==Bool_FALSE) {
	char buff[200];
	sprintf(buff,
"The Rule Set %s does not exist.\n\
Do you wish to create it?",
		RuleSetName_GR);

	/*
	 * TEST OUT THE REPOP QUERY 
	 */
	if (repop_Query(NULL,"Rule Editor",buff,
			"O.K.,Cancel")==0) {
	  RuleSet_GR=AlRuleSet_create(NULL,RuleSetName_GR,
				      (time_t)0,(time_t)0,
				      (Darray)NULL);
	  if (AlRSreg_register(RuleSet_GR)==Bool_FALSE) {
	    Al_warning("ruleeditor:unable to register the new rule set.Aborting.\n");
	    ruleeditor_Cleanup(-1);
	  }
	  AlRuleSet_set_desc(RuleSet_GR,"<no description>");
	}
	else 
	  ruleeditor_Cleanup(0);
      }
      else {
	RuleSet_GR=AlRuleSet_create(NULL,RuleSetName_GR,
				    (time_t)0,(time_t)0,
				    (Darray)NULL);
	if (AlRSreg_register(RuleSet_GR)==Bool_FALSE) {
	  Al_warning("ruleeditor: unable to register the new rule set.Aborting.\n");
	  ruleeditor_Cleanup(-1);
	}
	AlRuleSet_set_desc(RuleSet_GR,"<no description>");
      }
    }
  }

  else {   /*=== looking for an anyone server */
    RE_debug("check point1\n");
    RuleSetName_GR=AsClient_GR;
    AlA1C_init();
    RE_debug1("as client name=%s\n",AsClient_GR);
    if ((RuleSet_GR=AlA1C_read_rules(AsClient_GR))==NULL) {  
      printf("ruleeditor: unable to read your rulesets off the anyone server \
%s\n",AsClient_GR); 
      ruleeditor_Cleanup(-1);
    }
  }
}

