/** UnDUPE.C - Clean the dupefile of unwanted data
 ** Works as far as i know
 ** evilution @ efnet
 ** 02-25-99
 **/

#include <strings.h>
#include <stdio.h>
#include <sys/time.h>
#include <errno.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

#define DELETE_USER 1
#define PERGE_FILE 2
#define OLDER_THAN 3

struct dupefile {
  char filename[256];
  time_t timeup;
  char uploader[25];
};

// read in the data from the config file. I dont know how this handles spaces..

char * read_conf_datapath ( char *rootpath, char *confpath ) {
  FILE *fp;
  char datapath[1024];
  char temp[1024];
  int size;
  if((fp = fopen(confpath, "r")) == NULL)
    return 0;

  while (fscanf(fp, "%s", temp) == 1) {
    if (strcmp(temp, "rootpath") == 0) {
      fscanf(fp, "%s", rootpath);
    }
    if (strcmp(temp, "datapath") == 0) {
      fscanf(fp, "%s", datapath);
      strcat(rootpath, datapath);
      return rootpath;
    }
  }
fclose(fp);
return 0;
}
 
int main (int argc, char *argv[]) {
  // Variables
  FILE *fp, *fp2;
  char dupename[1024], datapath[1024], 
       data2[1024], dupefile[1024];
  
  int days = 0;
  time_t timeinfo = time( NULL );
  // setting default config file
  char config_file[255] = "/etc/glftpd.conf";
  
  struct dupefile buffer;
  int x, toDo;
  
  // User better supply a few options
  
  if ( argc < 2 ) {
    printf("USEAGE: %s [switch]\n", argv[0]);
    printf("Switches: -f [filename]\n");
    printf("\t  -u [username]\n");
    printf("\t  -r [alt-config]\n");
    printf("\t  -d [days old]\n");
    return 0;
  }
  x = 1;

  // Command line parsing
  while ( argc > x ) {

    if (argv[x][0] != '-') { /* nothing here */ } else {
      switch ( argv[x][1] ) {
        case 'r':
	        strcpy(config_file, argv[x+1]);
	        break;
        case 'u':
	        toDo = DELETE_USER;
	        strcpy(dupename, argv[x+1]);
		printf("Clearing DUPEFILE of %s \n", argv[x+1]);
		break;
        case 'f':
	        toDo = PERGE_FILE;
		strcpy(dupename, argv[x+1]);
		printf("Trying to clear %s \n", argv[x+1]);
	        break;
        case 'd':
	        toDo = OLDER_THAN;
		days = atoi(argv[x+1]);
		break;

      }
    }
    x++;
  }
  
  // Get data from the conf file
  // this is VERY poor.. i think i will have to redo it.
  // I dont think this handles spaces very well..

  read_conf_datapath(datapath, config_file);
  sprintf(dupefile, "%s/logs/dupefile", datapath);
  
  // This is my kludge to update the file. Just write a new one. Copy it over the old
  
  sprintf(data2, "%s2", dupefile);
  
  
  // Opening the data files
  printf("%s\n", dupefile);
  if((fp = fopen(dupefile, "r+b")) == NULL) {
    printf("Unable to open dupefile");
    return 0;
  }
  if((fp2 = fopen(data2, "w+b")) == NULL)
    return 0;
  
  // If you wanna perge a file, do it here

  if (toDo == PERGE_FILE) {  
    // While the file is still open
    while (!feof(fp)) {
      
      if (fread(&buffer, sizeof(struct dupefile), 1, fp) < 1)
	break;
      // If we found the file, delete it
      if (strcmp(buffer.filename, dupename) == 0) {
	fflush(fp);
	printf("Dupe! Clearing!!\n");
      }
      // if not, write it to the new file
      if (strcmp(buffer.filename, dupename) != 0)
	if (fwrite(&buffer, sizeof(struct dupefile), 1, fp2) < 1)
	  break;
    }
  }
  
  // this code is the same, cept looking for user, not file.  
  if (toDo == DELETE_USER) {
    while(!feof(fp)) {
      if (fread(&buffer, sizeof( struct dupefile), 1, fp) < 1)
	break;
      if (strcmp(buffer.uploader, dupename) == 0) {
	fflush(fp);
	printf(".");
      }
      if (strcmp(buffer.uploader, dupename) != 0) {
	if (fwrite(&buffer, sizeof(struct dupefile), 1, fp2) < 1)
	  break;
      }
    }
  }
 
  if (toDo == OLDER_THAN ) {
    while(!feof(fp)) {
      if (fread(&buffer, sizeof( struct dupefile), 1, fp) < 1)
	break;
      if ( buffer.timeup <  (timeinfo - 86400*days) ) {
	fflush(fp);
	printf(".");
      } else {
	if (fwrite(&buffer, sizeof(struct dupefile), 1, fp2) < 1)
	  break;
      }
    }
  }

  fclose(fp);
  fclose(fp2);
  // My HUGE cludge that works quite well. 
  // Read in the old data file, write to a new data file. 
  // After everything is done, and sucessful. Move the new one over the old one.
  // Hey, it works.
 if (rename(data2, dupefile) != 0)
   printf("Error renaming new dupefile\n");
 printf("\n");
 return 0;
}


