#include "ruleeditorP.h"
#include "ru.h"
#include "memory.h"

main(argc,argv)
int argc;
char** argv; 
{
  int i;
  extern void XtMainLoop();
  if (argc==1) {
    ruleeditor_Usage();
    ruleeditor_Cleanup(0);
  }
  for (i=1;i<argc;i++) 
    if (strcmp(argv[i],"-h")==0 || 
	strcmp(argv[i],"-help")==0 ) {
      ruleeditor_FullUsage();
      ruleeditor_Cleanup(0);
    }

  Al_init();
  XtInitialize("ruleeditor","LEns", NULL, NULL, 
	       &argc, argv);  
  AlUI_Initialize(NULL);
  mt_Init(&argc,argv);
  rs_Init(&argc,argv);
  ru_Init(&argc,argv);
  re_OpenEditor(Bool_FALSE,
		Rule_GR,
		ruleeditor_Save,
		ruleeditor_Quit,
		&argc,
		argv,
		ruleeditor_GetTitle(),
		NULL,
		NULL);
  XtMainLoop();
}

/*======================================================================*/
static char * Ruleeditor_full_help= "\
usage: ruleeditor [-rs] <rule set> [-rn <i>] [-mt <message type>] [-f]\n\
usage: ruleeditor [-rs] <rule set> [-ia <i>] [-ib <i>] [-mt <message type>] [-f]\n\
usage: ruleeditor [-rs] -d <rule number> \n\
usage: ruleeditor -as <name> [-ia <i>] [-ib <i>] [-mt <message type>]\n\
       The rule set is required.\n\
       i is understood to stand for an integer\n\
       -mt is to override the default message type.\n\
       -ia create a new rule and insert after the specified rule number.\n\
       -ib create a new rule and insert before the specified rule number.\n\
       -ia and -ib are mutually exclusive.\n\
       -rn edit the existing rule at the specified index.\n\
       if neither -rn, -ia, or -ib are specified, a new rule is created\n\
          to be added to the end of the ruleset.\n\
       -f (force) don't prompt the user if a new rule set must be created.\n\
       -d delete the specified rule number in the ruleset.\n\
       -as specifies a connection to the anyone server by the name <name>\n\
          Note that when connecting to an anyone server a rule set is not\n\
          specified because a user may only have one ruleset per server.\n";

const char * Ruleeditor_help="\
usage: ruleeditor { [-rs] <rule-set> | -as <anyone-server> } [ { -rn <i>|\n\
                  -ia <i> | -ib <i> } ] [-mt <message-type] [-f].\n\
       ruleeditor [-rs] <rule-set> -d <i>.\n";

/*--------------------------------*/
NORET ruleeditor_Usage()
{
  FILE *hf;
  char c;
  
  printf("%s",Ruleeditor_help);
/* 
  if ((hf=fopen("ruleeditor.hlp","r"))!=NULL)
    while ((c=fgetc(hf))!=EOF)  putchar(c);
  else
    fprintf(stderr,"Sorry, there's no help available for the ruleeditor\n");
*/
}

/*--------------------------------------------------*/
NORET ruleeditor_FullUsage()
{
  printf("%s",Ruleeditor_full_help);
}

/*--------------------------------------------------*/
NORET ruleeditor_Cleanup(x)
int x;
{
  Al_cleanup();
  exit(x);
}


/*--------------------------------------------------*/
NORET ruleeditor_Save(rei,Rule,UData)
reid rei;
AlRule Rule;
VOIDP UData;
{
  switch (EditOrCreate_GR) {
  case RU_EDIT:
    AlRule_mark_changed(Rule);
    AlRuleSet_put_rule_at_index(RuleSet_GR,Rule,RuleNumber_GR-1);
    AlRuleSet_remove_rule(RuleSet_GR,RuleNumber_GR);
    break;
  case RU_CREATE:
    AlRule_set_name(Rule,"<no name>");
    AlRuleSet_add_rule_to_end(RuleSet_GR,Rule);
    EditOrCreate_GR=RU_EDIT;
    RuleNumber_GR=AlRuleSet_number_of_rules(RuleSet_GR);
    break;
  case RU_IA:
    AlRule_set_name(Rule,"<no name>");
    if (AlRuleSet_number_of_rules(RuleSet_GR)==RuleNumber_GR)
      AlRuleSet_add_rule_to_end(RuleSet_GR,Rule);
    else 
      AlRuleSet_put_rule_at_index(RuleSet_GR,Rule,RuleNumber_GR);
    EditOrCreate_GR=RU_EDIT;
    RuleNumber_GR++;
    break;
  case RU_IB:
    AlRule_set_name(Rule,"<no name>");
    AlRuleSet_put_rule_at_index(RuleSet_GR,Rule,RuleNumber_GR-1);
    EditOrCreate_GR=RU_EDIT;
    break;
  default:
    printf("parsing error in ed.c ^^^10\n");
    break;
  }

  ruleeditor_UpdateTitle(rei);
  
  if (AsClient_GR!=NULL)
    AlA1C_write_rules(RuleSet_GR,AsClient_GR); 
  else
    AlRSreg_save(RuleSetName_GR);
}


/*--------------------------------------------------*/
NORET ruleeditor_Quit(rei,UD)
reid rei;
VOIDP UD;
{
  re_CloseEditor(rei);
  ruleeditor_Cleanup(0);
}


/*--------------------------------------------------*/
char* ruleeditor_GetTitle()
{
  char* Doing;

  Doing=(char *)Memory_allocate(120);
  switch (EditOrCreate_GR) {
  case RU_EDIT:
    sprintf(Doing,"EDITING rule  %d  of rule set  %s",
	    RuleNumber_GR,
	    RuleSetName_GR);
    break;
  case RU_CREATE:
    sprintf(Doing,"CREATING new rule to append to rule set  %s",
	    RuleSetName_GR);
    break;
  case RU_IA:
    sprintf(Doing,"CREATING rule to insert after rule  %d  in rule set  %s",
	    RuleNumber_GR,
	    RuleSetName_GR);
    break;
  case RU_IB:
    sprintf(Doing,"CREATING rule to insert before rule  %d  in rule set  %s",
	    RuleNumber_GR,
	    RuleSetName_GR);
    break;
  default:
    printf("bad parsing in d1.c ^^^10\n");
    break;
  }
  return(Doing);
}

/*--------------------------------------------------*/
NORET ruleeditor_UpdateTitle(rei)
reid rei;
{
  char* Doing;

  Doing=(char *)Memory_allocate(120);
  switch (EditOrCreate_GR) {
  case RU_EDIT:
    sprintf(Doing,"EDITING rule  %d  of rule set  %s",
	    RuleNumber_GR,
	    RuleSetName_GR);
    break;
  case RU_CREATE:
    sprintf(Doing,"CREATING new rule to append to rule set  %s",
	    RuleSetName_GR);
    break;
  case RU_IA:
    sprintf(Doing,"CREATING rule to insert after rule  %d  in rule set  %s",
	    RuleNumber_GR,
	    RuleSetName_GR);
    break;
  case RU_IB:
    sprintf(Doing,"CREATING rule to insert before rule  %d  in rule set  %s",
	    RuleNumber_GR,
	    RuleSetName_GR);
    break;
  default:
    printf("bad parsing in d1.c ^^^10\n");
    break;
  }

  re_UpdateTitle(rei,Doing);
}
