/*
 *   Filename checktool.c
 *   Author C M King
 *   Version 1.1
 *   Date    05 Nov 1993
 *
*/

#include <stdio.h>
#include <rpc/rpc.h>
#include<sys/param.h>
#include<errno.h>
#ifdef SVR4
#include<string.h>
#else
#include<strings.h>
#endif
#include<malloc.h>
#include<ctype.h>

#include "checker.h"
#include "checker_proc.h"

int main(argc,argv)
int argc;
char **argv;
{
  CLIENT *cl;
  FILE *filedes;
  char *server;
  char *dir;

  access_struct *res;
  namelist tmp;
  int nodate = FALSE;
  int verbose = FALSE;
  char c; /* for command line options */
  int x; /* a counter */
  char *testfile;
  char path[MAXPATHLEN];
  char *filepath;

  if (argc < 3) {
      (void) fprintf(stderr, "usage: %s host pathname\n",argv[0]);
      exit(1);
    }

  /* assumes that arguments go before flags */

  server = argv[argc - 2];
  dir = argv[argc - 1];

  /* parse arguments*/
  while ((c = getopt(argc, argv, "dv:")) != -1)
      switch (c) {
      case 'd' : {
	  nodate = TRUE;
	  (void) printf("Not taking date into account\n");
	  break;
	}
      case 'v' : {
	  verbose = TRUE;
	  (void) printf("Verbose output\n");
	  break;
	}
      }
  
  if ((filedes = fopen(dir, "r")) == NULL)    {
      perror("error on fopen");
      exit(1);
    }

  if ((testfile = (char *) malloc(sizeof(char) * NAMELEN)) == NULL) {
      perror("malloc failed for testfile");
      exit(1);
    }

  if ((filepath = (char *) malloc(sizeof(char) * NAMELEN)) == NULL) {
      perror("malloc failed for filepath");
      exit(1);
    }

  if ((cl = clnt_create(server, DIRPROG, DIRVERS, "tcp")) == NULL) {
      clnt_pcreateerror(server);
      exit(1);
    }
  /* set default value of path */
  (void) strcpy(path, "/");

  while (fgets(testfile, NAMELEN, filedes) != NULL)    {
    /* Check that lines do not start with spaces */
    if (isspace(testfile[0])) {
       (void) fprintf(stderr, "********** line %s \t\tstarts with a space\n", testfile);
       continue;
    }

    /* The first character that is  a # is a comment */
    /* % is used to denote that a path is being defined */

    switch (testfile[0]) {
    case '#' : {
          continue;
        }
    case '%' : {
          for(x=1;!isspace(testfile[x]); x++)  {
      	path[x - 1] = testfile[x];
            }
          path[x - 1] = NULL;
          continue;
        }
     default :
          break;
    }

    /* append the pathname to the description of the file */
    (void) sprintf(filepath, "%s/%s", path, testfile);
     
    if (nodate == TRUE)  /* put a + into the date field */
      testfile[strlen(filepath) - 2] = NO_DATE_CHECK;

    /* Call remote procedure access_1 on the server */
    if ((res = access_1(&filepath, cl)) == NULL)  {
          clnt_perror(cl, server);
          exit(1);
        }

    /* on error : print out the path */
    if (res->errno != 0)         {
       (void) fprintf(stderr, "error in comms");
        exit(1);
      }
   
    for(tmp = res->access_struct_u.list; tmp != NULL; tmp = tmp->next) {
       if (tmp->result != 0)	     {
           (void) printf("%s\n", tmp->err_string);
         }
       else { /* things went well */
           if (verbose)
               (void) printf("no problem with %s\n", tmp->err_string );
	       break;
	    }
	 }
    }
   return(0);
}

