/*added option -d playername for deleting player with known name. Not delete *
 *only if flag NODELET is set.                                               *
 *Syntax is now: ./purgeplay [-d playername] playerfile                      *
 *                                                                           *
 *added by vladon vladon@pobox.sk                                            *
*/


/* ************************************************************************
*  file: purgeplay.c                                    Part of CircleMUD * 
*  Usage: purge useless chars from playerfile                             *
*  All Rights Reserved                                                    *
*  Copyright (C) 1992, 1993 The Trustees of The Johns Hopkins University  *
************************************************************************* */

#include "conf.h"
#include "sysdep.h"

#include "structs.h"
#include "utils.h"
#include <string.h>


void purge(char *flag, char *name, char *filename)
{
  FILE *fl;
  FILE *outfile;
  struct char_file_u player;
  int okay, num = 0;
  long timeout;
  char *ptr, reason[80];
  int found  = 0, 
      delete = 0; 

  /* check, if flag starts with minus */
  if ( flag[0] !=  '-') {
    /* if no, it is filename */
    strcpy(filename, flag);
  }
  else {
    /* yes, its starts with minus, but is it 'd'? */
    if (flag[1] != 'd') {
      printf("Invalid argument '%s'\n", flag);
      exit(0);
    }
    /* yes, it is 'd' and we can delete, and nothing else */
    else delete = 1;
  }

  if (!(fl = fopen(filename, "r+"))) {
    printf("Can't open '%s'\n", filename);
    exit(0);
  }
  
  outfile = fopen("players.new", "w");
  if (delete)
    printf("Deleting player: \n");
  else
    printf("Deleting players: \n");

  for (;;) {
    fread(&player, sizeof(struct char_file_u), 1, fl);
    if (feof(fl)) {
      fclose(fl);
      fclose(outfile);
      /* on end, if we can delete and not found :-( */
      if (delete  &&  !found) 
        printf("Can't find player %s. Try to use 'showplay'.\n", name);
      printf("Done.\n");
      exit(0);
    }
    okay = 1;
    *reason = '\0';
    

    /* if we can delete, then delete and nothing else */
    if (delete) {
      /* is name like we can delete? */
      if (!strcmp(player.name, name)) {
        /* Oooooh Yeah, founded :-) */
        found = 1;
        okay = 0;
        sprintf(reason, "Player deleted, as You want");
      }
    }
    else {
      for (ptr = player.name; *ptr; ptr++)
        if (!isalpha(*ptr) || *ptr == ' ') {
     	  okay = 0;
	  strcpy(reason, "Invalid name");
        }
      if (player.level == 0) {
        okay = 0;
        strcpy(reason, "Never entered game");
      }
      if (player.level < 0 || player.level > LVL_IMPL) {
        okay = 0;
        strcpy(reason, "Invalid level");
      }
    }

    /* now, check for timeouts.  They only apply if the char is not
       cryo-rented.   Lev 32-34 do not time out.  */

    timeout = 1000;

    if (!delete  &&  okay  &&  player.level <= LVL_IMMORT) {

      if (!(player.char_specials_saved.act & PLR_CRYO)) {
	if (player.level == 1)		timeout = 4;	/* Lev   1 : 4 days */
	else if (player.level <= 4)	timeout = 7;	/* Lev 2-4 : 7 days */
	else if (player.level <= 10)	timeout = 30;	/* Lev 5-10: 30 days */
	else if (player.level <= LVL_IMMORT - 1)
	  timeout = 60;		/* Lev 11-30: 60 days */
	else if (player.level <= LVL_IMMORT)
	  timeout = 90;		/* Lev 31: 90 days */
      } else
	timeout = 90;

      timeout *= SECS_PER_REAL_DAY;

      if ((time(0) - player.last_logon) > timeout) {
	okay = 0;
	sprintf(reason, "Level %2d idle for %3ld days", player.level,
		((time(0) - player.last_logon) / SECS_PER_REAL_DAY));
      }
    }

    if (player.char_specials_saved.act & PLR_DELETED) {
      okay = 0;
      sprintf(reason, "Deleted flag set, deleted");
    }

    /* Don't delete for *any* of the above reasons if they have NODELETE */
    if (!okay && (player.char_specials_saved.act & PLR_NODELETE)) {
      okay = 2;
      strcat(reason, "; NOT deleted, flag NODELETE is set");
    }

    if (okay) {
      fwrite(&player, sizeof(struct char_file_u), 1, outfile);
    }
    else
      printf("%4d. %-20s %s\n", ++num, player.name, reason);

    if (okay == 2)
      fprintf(stderr, "%-20s %s\n", player.name, reason);
  }

}



int main(int argc, char *argv[])
{
  char str[3][100];
  int  i = 0;
  
  /* initialize with blank str*/
  for (i = 0; i < 3; i++)
    strcpy(str[i], "");

  if (argc < 2)
    printf("Usage: %s [-d playername] playerfile-name\n", argv[0]);
  else {
     for (i = 1; i < argc; i++)
       strcpy(str[i-1], argv[i]);
     purge(str[0], str[1], str[2]);
  }

  return 0;
}

