#include <stddef.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>

#include "sysdep.h"
#include "bool.h"
#include "al.h"
#include "profile.h"
#include "a1proto.h"
#include "ruleset.h"
#include "OSinter.h"
#include "RSreg.h"
#include "memory.h"

static const char *rule_drop;
static const char *rule_lib;
static const char *anyone_name;
static const char *user_name;

static int initialized = 0;

Bool AlA1C_init(NOARGS)
{
  char *temp_buf;

  if (AlProfile_has_value(ALPROF_ANYONE_DROP_DIR) == Bool_FALSE ||
      AlProfile_has_value(ALPROF_ANYONE_LIB_DIR) == Bool_FALSE ||
      AlProfile_has_value(ALPROF_ANYONE_NAME) == Bool_FALSE)
    Al_fatal_error("Anyone server not configured in Lens Profile");
  
  rule_drop = AlProfile_get_CP_val(ALPROF_ANYONE_DROP_DIR);
  rule_lib = AlProfile_get_CP_val(ALPROF_ANYONE_LIB_DIR);
  anyone_name = AlProfile_get_CP_val(ALPROF_ANYONE_NAME);
  
  temp_buf = (char *)Memory_allocate(OSINTER_USER_NAME_LENGTH+1);
  if (OSinter_get_user_name(temp_buf) != Bool_TRUE)
    Al_fatal_error("Could not get current user name");
  user_name = temp_buf;

  initialized = 1;

  return Bool_TRUE; 
}

/* needs work to support multiple servers */

AlRuleSet AlA1C_read_rules(server_name) 
     const char *server_name;
{
  char wd[OSINTER_MAXPATHLEN+1];
  AlRuleSet temp_rs;

  assert(initialized); 
  assert(server_name != (const char *)NULL);

  if (strcmp(server_name,anyone_name) != 0) 
    Al_fatal_error1("Multiple servers not implemented. Only %s supported",
		    anyone_name);

  if (OSinter_get_cwd(&(wd[0])) == Bool_FALSE)
    Al_fatal_error("Could not determine current directory");
  if (OSinter_set_cwd(rule_lib) == Bool_FALSE)
    Al_fatal_error1("Could not change directory to %s",
		    rule_lib);
  
  
  temp_rs = AlRSreg_read_from_file(user_name);
  if (temp_rs == (AlRuleSet)NULL)
    return AlRuleSet_create("Anyone rules",
			    "anyone999rules", /* Obscure name; not used */
			    (time_t)0,
			    (time_t)0,
			    (Darray)NULL);
  OSinter_set_cwd(&(wd[0]));
  return temp_rs;
}


Bool AlA1C_write_rules(rs, server_name)
     AlRuleSet rs;
     const char *server_name;
{
  char wd[OSINTER_MAXPATHLEN+1];
  Bool temp_result;

  assert(initialized); 
  assert(server_name != (const char *)NULL);

  if (strcmp(server_name,anyone_name) != 0) 
    Al_fatal_error1("Multiple servers not implemented. Only %s supported",
		    anyone_name);

  if (OSinter_get_cwd(&(wd[0])) == Bool_FALSE)
    Al_fatal_error("Could not determine current directory");
  OSinter_set_cwd(rule_drop);

  temp_result = AlRSreg_write_to_file(rs, user_name);
  if (temp_result == Bool_FALSE)
    Al_fatal_error2("Could not write rules for user %s in %s", 
		    user_name, 
		    rule_drop);
#ifdef unix
  chmod(user_name, 0640);
#else
  assert(0);
#endif
  OSinter_set_cwd(&(wd[0]));
  return Bool_TRUE;
}

Bool AlA1C_delete_rules(server_name)
     const char *server_name;
{
  char wd[OSINTER_MAXPATHLEN+1];
  FILE *output_file;
  
  assert(initialized); 
  assert(server_name != (const char *)NULL);

  if (server_name != anyone_name) 
    Al_fatal_error1("Multiple servers not implemented. Only %s supported",
		    anyone_name);

  if (OSinter_get_cwd(&(wd[0])) == Bool_FALSE)
    Al_fatal_error("Could not determine current directory");
  OSinter_set_cwd(rule_drop);
  
  if ((output_file = fopen(user_name, "w")) == (FILE *)NULL)
    Al_fatal_error2("Could not write delete command for user %s in %s", 
		    user_name, 
		    rule_drop);
    
  fprintf(output_file, "%s\n", AlA1proto_MAGIC_DELETE_STRING);
  if (fclose(output_file) == EOF)
    Al_fatal_error2("Could not close delete command file for user %s in %s", 
		    user_name, 
		    rule_drop);
  
  OSinter_set_cwd(&(wd[0]));
  return Bool_TRUE;
}
