/*
 * This file is part of an snmp query application.
 * This file contains file functions.
 *
 * Copyright 1990 by the Massachusetts Institute of Technology.
 *
 * For copying and distribution information, please see the file
 * <mit-copyright.h>.
 *
 * Tom Coppeto
 * MIT Network Services
 * 15 April 1990
 *
 *    $Source: /afs/.net.mit.edu/tools/src/simon/RCS/file.c,v $
 *    $Author: tom $
 *    $Locker: tom $
 *    $Log:	file.c,v $
 * Revision 1.4  91/02/21  13:54:10  tom
 * *** empty log message ***
 * 
 * Revision 1.3  90/07/27  15:19:07  tom
 * print stuff when reading/saving stuff
 * 
 * Revision 1.2  90/07/25  10:29:50  tom
 * checks to see if variable is specified in numeric form
 * 
 * Revision 1.1  90/07/20  21:17:35  tom
 * Initial revision
 * 
 *
 */

#ifndef lint
static char *rcsid = "$Header: /afs/.net.mit.edu/tools/src/simon/RCS/file.c,v 1.4 91/02/21 13:54:10 tom Exp Locker: tom $";
#endif
		
#include <simon.h>
#include <sys/stat.h>
#include <mit-copyright.h>

extern CLASS *ClassList;
static char  lbuf[BUF_SIZE];
static char  *prompt_for_filename();

#define READ  0
#define WRITE 1

void
cmd_set_logfile(argc, argv)
     int argc; 
     char **argv;
{
  
  bzero(cmdbuf, sizeof(cmdbuf));
  pname = argv[0];

  while(*++argv != (char *) NULL)
    {
      if(**argv != '-')
	{
	  if(*cmdbuf == '\0')
	    {
	      strcpy(cmdbuf, *argv);
	      continue;
	    }
	  else
	    fprintf(stderr, "You can only specify one logfile.\n");
	}
      else
	if(strcmp(*argv, "-help") != 0)
	  fprintf(stderr, "Unknown option \"%s\"\n", *argv);

      printf("usage: %s [<logfile>]\n", pname);
      return;
    }

  if(*cmdbuf == '\0')
    {
      get_prompted_input("new log file: ", cmdbuf, sizeof(cmdbuf));
      if(*cmdbuf == '\0')
	{
	  printf("OK, nothing changed.\n");
	  return;
	}
    }

  printf("New logfile set to %s.\n", cmdbuf);
  return;
}




  
void
cmd_tee_output(argc, argv)
     int argc; 
     char **argv;
{
  return;
}


void
cmd_cancel_tee(argc, argv)
     int argc; 
     char **argv;
{
  return;
}


void 
cmd_read_vardefs(argc, argv)
     int argc;
     char **argv;
{
  char *func;
  char *file = (char *) NULL;

  func = argv[0];

  while (*++argv != (char *) NULL)
    {
      if(**argv != '-')
        {
          if(file == (char *) NULL)
            {
              file = *argv;
              continue;
            }
          else
            fprintf(stderr,"Too many file names specified on command line.\n");
        }
      else
        if(strcmp(*argv, "-help") != 0)
          fprintf(stderr, "Unknown option \"%s\".\n", *argv);

      printf("usage: %s [<file name>]\n", func);
      return;
    }

  if((file = prompt_for_filename("filename: ", file, READ)) == (char *) NULL)
    {
      printf("Request cancelled.\n");
      return;
    }

  printf("reading variables from \"%s\"...", file);
  if(read_vardefs(file) == SUCCESS)
    printf("done.\n");
  else
    printf("failed.\n");

  return;
}




void 
cmd_read_agentdefs(argc, argv)
     int argc;
     char **argv;
{
  char *func;
  char *file = (char *) NULL;

  func = argv[0];

  while (*++argv != (char *) NULL)
    {
      if(**argv != '-')
        {
          if(file == (char *) NULL)
            {
              file = *argv;
              continue;
            }
          else
            fprintf(stderr,"Too many file names specified on command line.\n");
        }
      else
        if(strcmp(*argv, "-help") != 0)
          fprintf(stderr, "Unknown option \"%s\".\n", *argv);

      printf("usage: %s [<file name>]\n", func);
      return;
    }

  if((file = prompt_for_filename("filename: ", file, READ)) == (char *) NULL)
    {
      printf("Request cancelled.\n");
      return;
    }

  printf("reading agents from \"%s\"...", file);
  if(read_agentdefs(file) == SUCCESS)
    printf("done.\n");
  else
    printf("failed.\n");

  return;
}





void 
cmd_save_vardefs(argc, argv)
     int argc;
     char **argv;
{
  char *func;
  char *file = (char *) NULL;

  func = argv[0];

  while (*++argv != (char *) NULL)
    {
      if(**argv != '-')
        {
          if(file == (char *) NULL)
            {
	      file = *argv;
              continue;
            }
          else
            fprintf(stderr,"Too many file names specified on command line.\n");
        }
      else
        if(strcmp(*argv, "-help") != 0)
          fprintf(stderr, "Unknown option \"%s\".\n", *argv);

      printf("usage: %s [<file name>]\n", func);
      return;
    }

  if((file = prompt_for_filename("filename: ", file, WRITE)) == (char *) NULL)
    {
      printf("Request cancelled.\n");
      return;
    }

  printf("saving variables to \"%s\"...", file);
  if(save_vardefs(file) == SUCCESS)
    printf("done.\n");
  else
    printf("failed.\n");

  return;
}




void 
cmd_save_agentdefs(argc, argv)
     int argc;
     char **argv;
{
  char *func;
  char *file = (char *) NULL;

  func = argv[0];

  while (*++argv != (char *) NULL)
    {
      if(**argv != '-')
        {
          if(file == (char *) NULL)
            {
	      file = *argv;
              continue;
            }
          else
            fprintf(stderr,"Too many file names specified on command line.\n");
        }
      else
        if(strcmp(*argv, "-help") != 0)
          fprintf(stderr, "Unknown option \"%s\".\n", *argv);

      printf("usage: %s [<file name>]\n", func);
      return;
    }

  if((file = prompt_for_filename("filename: ", file, WRITE)) == (char *) NULL)
    {
      printf("Request cancelled.\n");
      return;
    }
    
  printf("saving agents to \"%s\"...", file);
  if(save_agentdefs(file) == SUCCESS)
    printf("done.\n");
  else
    printf("failed.\n");

  return;
}





int
read_vardefs(file)
     char *file;
{
  VARIABLE *variable;
  CLASS    *class;
  FILE     *fp;
  char     *c;
  char     *d;

  if((file == (char *) NULL) || (*file == '\0'))
    {
      fprintf(stderr, "No file specified.\n");
      return(ERROR);
    }

  if((fp = fopen(file, "r")) == (FILE *) NULL)
    {
      fprintf(stderr, "Unable to open file \"%s\" for reading.\n", file);
      return(ERROR);
    }

  while(fgets(lbuf, sizeof(lbuf), fp) != (char *) NULL)
    {
      if((*lbuf == '#') || (*lbuf == '\n'))
	continue;
      if((c = rindex(lbuf, '\n')) != (char *) NULL)
	*c = '\0';
      if((c = index(lbuf, '|')) == (char *) NULL)
	continue;
      *c++ = '\0';

      if(!is_class(lbuf))
	class = create_class(lbuf);
      else
	class = get_class(lbuf);

      if(class == (CLASS *) NULL)
	{
	  fprintf(stderr, "confusion in parsing\n");
	  break;
	}
      if((d = index(c, '|')) == (char *) NULL)
	continue;
      *d++ = '\0';

      if((variable = (VARIABLE *) malloc(sizeof(VARIABLE))) == 
	 (VARIABLE *) NULL)
	{
	  fprintf(stderr, "cannot allocate space for variable.\n");
	  return(ERROR);
	}
      bzero(variable, sizeof(VARIABLE));

      if((variable->variable = copy_string(c)) == (char *) NULL)
	{
	  fprintf(stderr, "cannot allocate space for variable.\n");
	  free(variable);
	  return(ERROR);
	}

      if(variable->variable[strlen(variable->variable)-1] == '*')
	{
	  variable->variable[strlen(variable->variable)-1] = '\0';
	  variable->mquery = TRUE;
	}

      if(index(variable->variable, '_') == (char *) NULL)
	{
	  variable->nvariable = variable->variable;
	  variable->variable = (char *) NULL;
	}

      if((c = index(d, '|')) != (char *) NULL)
	{
	  *c++ = '\0';
	  if(*d)
	    variable->abbrev = copy_string(d);
	}

      if((d = index(c, '|')) != (char *) NULL)
	*d++ = '\0';

      if(*c)
	variable->label  = copy_string(c);

      if(d != (char *) NULL)
	{
	  if(strcmp(d, "string") == 0)
	    variable->itype = STR;
	  else
	    variable->itype = INT;
	}

      if(add_variable(class, variable) != SUCCESS)
	{
	  free(variable->variable);
	  if(variable->abbrev)
	    free(variable->abbrev);
	  if(variable->label)
	    free(variable->label);
	  free(variable);
	  fclose(fp);
	  fprintf(stderr, "error adding variables from config file.\n");
	  return(ERROR);
	}
    }
  
  if(fclose(fp) < 0)
    return(ERROR);
  return(SUCCESS);
}







int
read_agentdefs(file)
     char *file;
{
  CLUSTER  *cluster;
  FILE     *fp;
  char     *c;
  int      status;

  if((file == (char *) NULL) || (*file == '\0'))
    {
      fprintf(stderr, "No file specified.\n");
      return(ERROR);
    }

  if((fp = fopen(file, "r")) == (FILE *) NULL)
    {
      fprintf(stderr, "Unable to open file \"%s\" for reading.\n", file);
      return(ERROR);
    }

  while(fgets(lbuf, sizeof(lbuf), fp) != (char *) NULL)
    {
      if((*lbuf == '#') || (*lbuf == '\n'))
	continue;
      if((c = rindex(lbuf, '\n')) != (char *) NULL)
	*c = '\0';
      if((c = index(lbuf, '|')) == (char *) NULL)
	continue;
      *c++ = '\0';
      if(!is_cluster(lbuf))
	cluster = create_cluster(lbuf);
      else
	cluster = get_cluster(lbuf);
      if(cluster == (CLUSTER *) NULL)
	{
	  fprintf(stderr, "confusion in parsing\n");
	  break;
	}

      cluster->agents = add_agent(cluster->agents, c, &status); 
      if(status != SUCCESS)
	{
	  free_cluster(cluster);
/*	  return(ERROR);*/
	}
    }
  
  if(fclose(fp) < 0)
    return(ERROR);

  return(SUCCESS);
}






int
save_vardefs(file)
     char *file;
{
  VARIABLE *vptr;
  CLASS    *cptr;
  FILE     *fp;
  
  if((fp = fopen(file, "w")) == (FILE *) NULL)
    {
      fprintf(stderr, "Unable to open file \"%s\" for writing.\n", file);
      return(ERROR);
    }
  
  cptr = ClassList;
  while(cptr)
    {
      vptr = cptr->vars;
      while(vptr)
	{
	  fprintf(fp,"%s|%s%s|%s|%s\n", cptr->name, vptr->variable, 
		  vptr->mquery ? "*"             : "", 
		  vptr->abbrev ? vptr->abbrev : "", 
		  vptr->label  ? vptr->label  : "");
	 vptr = vptr->next;
	}
      cptr = cptr->next;
    }

  if(fclose(fp) < 0)
    {
      perror("save_config");
      return(ERROR);
    }

  return(SUCCESS);
}

  




int
save_agentdefs(file)
     char *file;
{
  CLUSTER  *cptr;
  AGENT    *aptr;
  FILE     *fp;

  if(!ClusterList)
    {
      printf("nothing to save.\n");
      return(SUCCESS);
    }

  if((fp = fopen(file, "w")) == (FILE *) NULL)
    {
      fprintf(stderr, "Unable to open file \"%s\" for writing.\n", file);
      return(ERROR);
    }
  
  cptr = ClusterList;
  while(cptr)
    {
      aptr = cptr->agents;
      while(aptr)
	{
	  fprintf(fp,"%s|%s\n", cptr->name, aptr->name);
	  aptr = aptr->next;
	}
      cptr = cptr->next;
    }

  if(fclose(fp) < 0)
    {
      perror("save_config");
      return(ERROR);
    }

  return(SUCCESS);
}

  
      
  
static char *
prompt_for_filename(prompt, file, rw)
     char *prompt;
     char *file;
     int rw;
{
  char buf[BUF_SIZE];
  struct stat statbuf;
  int s;

  bzero(buf, sizeof(buf));
  while (TRUE)
    {
      if(file == (char *) NULL)
	get_prompted_input(prompt, buf, sizeof(buf));
      else
	{
	  strcpy(buf, file);
	  file = (char *) NULL;
	}

      if(*buf == '\0')
	return((char *) NULL);
      
      if(*buf == '?')
	{
	  printf("enter a file name or nothing to exit.\n");
	  continue;
	}
      
      s = stat(buf, &statbuf);
      if((s < 0)  && rw)
	return(buf);
      if((s >= 0) && !rw)
	return(buf);
      if((s < 0)  && !rw)
	{
	  fprintf(stderr, "%s not found.\n", buf);
	  continue;
	}
      printf("%s exists.. nuke it?", buf);		
      while(TRUE)
	{
	  get_prompted_input("", lbuf, sizeof(lbuf));
	  if(string_equiv(lbuf, "yes", 1))
	    return(buf);
	  if(string_equiv(lbuf, "no", 1))
	    break;
	  printf("yes or no.\n");
	}
    }
}
      
