#ifdef DEBUG
/*----------------------------------------------------------------------
     Initialize debugging - open the debug log file

  Args: none

 Result: opens the debug logfile for dprints

   Opens the file "~/.pine-debug1. Also maintains .pine-debug[2-4]
   by renaming them each time so the last 4 sessions are saved.
  ----*/
void
init_debug()
{
    char nbuf[5];
    char newfname[MAXPATH+1], filename[MAXPATH+1];
    int i;

    if(!debug)
      return;

    for(i = NUMDEBUGFILES - 1; i > 0; i--){
        build_path(filename, ps_global->home_dir, DEBUGFILE);
        strcpy(newfname, filename);
        sprintf(nbuf, "%d", i);
        strcat(filename, nbuf);
        sprintf(nbuf, "%d", i+1);
        strcat(newfname, nbuf);
        (void)rename_file(filename, newfname);
    }

    build_path(filename, ps_global->home_dir, DEBUGFILE);
    strcat(filename, "1");

    debugfile = fopen(filename, "w");
    if(debugfile != NULL){
	time_t now = time((time_t *)0);
	if(debug > 7)
	  setbuf(debugfile, NULL);
	dprint(1, (debugfile, "Debug output of the Pine program (at debug"));
	dprint(1, (debugfile, " level %d).  Version %s\n%s\n",
		  debug, pine_version, ctime(&now)));
    }
}


/*----------------------------------------------------------------------
     Try to save the debug file if we crash in a controlled way

  Args: dfile:  pointer to open debug file

 Result: tries to move the appropriate .pine-debugx file to .pine-crash

   Looks through the four .pine-debug files hunting for the one that is
   associated with this pine, and then renames it.
  ----*/
void
save_debug_on_crash(dfile)
FILE *dfile;
{
    char nbuf[5];
    char crashfile[MAXPATH+1], filename[MAXPATH+1];
    int i;
    struct stat sbuf;
    ino_t our_debug_ino;
    dev_t our_debug_dev;
    time_t now = time((time_t *)0);

    if(!debug)
      return;

    fprintf(dfile, "\nsave_debug_on_crash: Version %s: debug level %d\n",
	pine_version, debug);
    fprintf(dfile, "\n                   : %s\n", ctime(&now));

    build_path(crashfile, ps_global->home_dir, ".pine-crash");

    if(fstat(fileno(dfile), &sbuf) != 0)
      return;
    
    our_debug_dev = sbuf.st_dev;
    our_debug_ino = sbuf.st_ino;

    fprintf(dfile, "\nAttempting to save debug file to %s\n", crashfile);
    fprintf(stderr,
	"\n\n       Attempting to save debug file to %s\n\n", crashfile);

    fclose(dfile);

    for(i = 1; i <= NUMDEBUGFILES; i++){
        build_path(filename, ps_global->home_dir, DEBUGFILE);
        sprintf(nbuf, "%d", i);
        strcat(filename, nbuf);
	if(stat(filename, &sbuf) != 0)
	  continue;
	/* This must be the current debug file */
	if(sbuf.st_dev == our_debug_dev && sbuf.st_ino == our_debug_ino){
	    rename_file(filename, crashfile);
	    break;
	}
    }
}


#define CHECK_EVERY_N_TIMES 100
#define MAX_DEBUG_FILE_SIZE 200000L
/*
 * This is just to catch runaway Pines that are looping spewing out
 * debugging (and filling up a file system).  The stop doesn't have to be
 * at all precise, just soon enough to hopefully prevent filling the
 * file system.  If the debugging level is high (9 for now), then we're
 * presumably looking for some problem, so don't truncate.
 */
int
do_debug(debug_fp)
FILE *debug_fp;
{
    static int counter = CHECK_EVERY_N_TIMES;
    static int ok = 1;
    long filesize;

    if(debug < 9 && ok && --counter <= 0){
	if((filesize = fp_file_size(debug_fp)) != -1L)
	  ok = (unsigned long)filesize < (unsigned long)MAX_DEBUG_FILE_SIZE;

	counter = CHECK_EVERY_N_TIMES;
	if(!ok){
	    fprintf(debug_fp, "\n\n --- No more debugging ---\n");
	    fprintf(debug_fp,
		"     (debug file growing too large - over %ld bytes)\n\n",
		MAX_DEBUG_FILE_SIZE);
	    fflush(debug_fp);
	}
    }
    return(ok);
}
#endif /* DEBUG */


