/*
* Copyright (C) 2001-2009, CompHEP Collaboration
* -----------------------------------------------------
*/
#include <unistd.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <stdarg.h>

#include "chep_limits.h" 
#include "chep_crt/include/chep_crt.h"

#include "unix_utils.h"
#include "syst.h"
#include "files.h"

char *outputDir = "";
longstr pathtocomphep = "";
longstr pathtolhapdf = "";

void 
copyfile (char *namefrom, char *nameto)
{
#define BLOCKSIZE 1024
  FILE *filefrom;
  FILE *fileto;
  const int _blocksize = BLOCKSIZE;
  char s[BLOCKSIZE];
  int size;
  filefrom = fopen (namefrom, "rb");
  fileto = fopen (nameto, "wb");
  if ((filefrom == NULL) || (fileto == NULL))
    return;

  do
    {
      size = fread (s, 1, _blocksize, filefrom);
      fwrite (s, 1, size, fileto);
    }
  while (size == _blocksize && size != 0);

  fclose (fileto);
  fclose (filefrom);
  return;
}

void 
nextFileName (char *f_name, char *firstname, char *ext)
{
  int tabnum = 0;
  midstr lname;
  FILE *f;

  for (;;)
    {
      tabnum++;
      sprintf (f_name, "%s%s%d", outputDir, firstname, tabnum);
      sprintf (lname, "%s%s", f_name, ext);
      f = fopen (lname, "r");
      if (f)
	fclose (f);
      else
	return;
    }
}
