/* Forem-File Tester by W.Zweygart */

#define VERSION "0.1"
#define FOR_DX 1

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <tos.h>
#include <ext.h>
#include <ctype.h>
#include <time.h>
#include "turbo.h"
#include "tin_door.c"

char mesage[30];
char datas[30];
char oldpath[64];
char conf_name[64];
CONFIG xmbuf;
int device;  /* AUX: */
char drive[3], dir[40], filename[9], ext[5];
char new_drivelist[40];
int main_level;
int main_function;		/* bit weise: 1 console, 2 listing 4 debug */
FILE *flist;

void fnsplit (const char *path,
              char *drive, char *dir, char *fname, char *ext)
{
	char *h_path;
	size_t len;

	/* I know it's not the fine way but I have to stay compatible */
	/* with the PC declaration */
	h_path = (char *)path;
	drive[0] = '\0';
	dir[0] = '\0';
	fname[0] = '\0';
	ext[0] = '\0';
	if (h_path[1] == ':') {
		strncpy(drive, h_path, 2);
		drive[2] = '\0';
		h_path += 2;
	}
	if (h_path[0] == '\\')
	{
		strncpy (dir, h_path, (len = (size_t)((strrchr(h_path, '\\') - h_path) + 1)) );
		dir[len] = '\0';
	}
	else
	{
		dir[0] = '\\';
		strncpy (dir+1, h_path, (len = (size_t)((strrchr(h_path, '\\') - h_path) + 1)) );
		dir[len+1] = '\0';
	}
	h_path += len;
	if (strchr(h_path, '.') == NULL) {
		strncpy(fname, h_path, 9);
		fname[8] = '\0';
	} else {
		strncpy(fname, h_path, (len = (size_t)(strchr(h_path, '.') - h_path)));
		fname[len] = '\0';
		h_path += len;
		strncpy(ext, h_path, 4);
		ext[4] = '\0';
	}
}

/*	search a file in the default path on every drive online in config.dat */
/* return 0 if found -1 otherwise */

int findfile (char *path, int cont) {
	static char fname[81], orig_path[81];
	static int drv;
	static struct ffblk ffb;	/* for find_first(), find_next() */
	int first;

	first = !cont;
	if (!cont) {
		drv = 0;
		strcpy (orig_path, path); /* save original path */
	}
	while (drv < strlen(new_drivelist)) {
		if (cont) {	/* find next one */
			if (!findnext(&ffb)) {
				strcpy(path, drive);
				strcat(path, dir);
				strcat(path, ffb.ff_name);
				fnsplit(path, drive, dir, filename, ext);
				return (0);
			} else {
				drv++;	/* continue on next drive */
				cont = 0;
				strcpy (path, orig_path); /* restore original path */
				if(!(drv < strlen(new_drivelist))) break;	/* not found */
			}
		}
		if (!cont) {	/* find first one on this drive */
			sprintf(fname, "%c:%s%s", new_drivelist[drv], xmbuf.defdir, path);
			fnsplit(fname, drive, dir, filename, ext);
			if (!findfirst(fname, &ffb, 0)) {
				strcpy(path, drive);
				strcat(path, dir);
				strcat(path, ffb.ff_name);	/* construct path of file found */
				fnsplit(path, drive, dir, filename, ext);
				return (0);
			} else drv++;
		if(!(drv < strlen(new_drivelist))) break;	/* not found */
		}
	} /* while */
	if (first) {
printf("ff_f: could not find file %s\r\n", orig_path);
	} else {
printf("ff_n: no more %s\r\n", orig_path);
	}
	return (-1);	/* not found */
}

void this_printf(int mask, const char *format, ...)
{
 va_list arg_point;
 char temp[255];

 va_start(arg_point, format);
 vsprintf(temp, format, arg_point);
 va_end(arg_point);

 if((mask & 1) && (main_function & 1)) cd_puts(temp, device);
 if((mask & 2) && (main_function & 2)) fprintf(flist, temp);
 if(((mask & 4) && !(mask & 1)) || (main_function == 0)) printf(temp);
}


void check_arg(int argc, char *argv[])
{	 /* Commandlineparameter cheken */
 if(argc>1) if(stricmp(argv[1],"LOCAL")==0) device=2;
 if(argc>2) if(sscanf(argv[2],"%d",&main_level)==0) main_level=32767;
 if(argc>3) if(sscanf(argv[3],"%d",&main_function)==0) main_function=0;
}

void
les_opt(void )
{
 FILE *fp;
 char tmp[82];
 int i;

 fp = fopen("TURBO.OPT","rb");
 if (fp > 0)
  {
  for(i=0; i<47; i++)
	{
	fgets(tmp,80,fp);
	if (i==4)
	  {
		strcpy(new_drivelist,strtok(tmp," "));
		this_printf(5,"Drive List :<%s>\n",new_drivelist);
	  }
	if (i==31)
	  {
		strcpy(mesage,strtok(tmp," "));
		this_printf(5,"Message Subdirectory :<%s>\n",mesage);
	  }
	if (i==34)
	  {
		strcpy(datas,strtok(tmp," "));
		this_printf(5,"Alternate Dir for Dat-Files :<%s>\n",datas);
	  }
	}
  fclose(fp);
  }
 else
  {
	this_printf(5," TURBO.OPT not found!\n");
  }
}

void
test_dir( char *direct)
{
 FILE *fp;
 TFILE dl_dir;
 char f_n_dll[65];
 short ft,i;
 long summe;
 short counter;

 this_printf(3,"\n=============================================================================\n",direct);
 this_printf(3,"\nFile Directory of %s\n\n",direct);

 sprintf(f_n_dll,"%s%s%s",datas,direct,".FIL");
 if (findfile(f_n_dll, 0))
  {
	this_printf(5,"Could not find %s.\n", f_n_dll);
	return;
  }
 else
  {
	summe = 0;
	counter = 0;
	this_printf(5," Turbo-File %s testing\n\n",f_n_dll);
	fp = fopen(f_n_dll,"rb");
	if (fp == 0)
	 {
		this_printf(5," Fehler!!\n");
		return;
	 }
	if (fp > 0)
	 {
	  do
	   {
			fread((char*)&dl_dir,sizeof(dl_dir),1,fp);
			ft=feof(fp);
			if(ft==0)
			 {
				if(dl_dir.flevel<=main_level)
				 {
					this_printf(3,"%-13s %6ld %2d-%2d-%2d [%2ld] ",
							dl_dir.fname,dl_dir.flength,
							dl_dir.fdate[1],dl_dir.fdate[0],dl_dir.fdate[2],
							dl_dir.frd_cnt);
					this_printf(3,"%s\n",&dl_dir.fdesc[0]);
					for(i=1; i<5; i++) if(strlen(dl_dir.fdesc[i])>1)
								this_printf(3,"\t\t\t\t   %s\n",&dl_dir.fdesc[i]);
					summe += dl_dir.flength;
					counter++;
				 }
		 	 }
		 } while(!ft);
	  fclose(fp);
	}
	this_printf(3,"\n %5d Files, %4ld KBytes\n",counter,summe/1024L);
  }
}

void
test_config(void)
{
 short i;
 long fl;

 this_printf(5,"Analyse des Forem-Files CONFIG.DAT\n\n");
 this_printf(5,"Zahl der Anrufer : %ld \n",xmbuf.callno);
 this_printf(5,"Zahl der Nachrichten  : %ld \n",xmbuf.nmess);
 this_printf(5,"Drives : %d \n",xmbuf.ndrives);
 this_printf(5,"File-Systeme : %d \n",xmbuf.ndirect);
 for(i=0; i<xmbuf.ndirect; i++)
  {
	this_printf(5,"No %3d : %-9s \n",i+1,xmbuf.subdir[i]);
  }
 this_printf(5,"Pwnum : %d \n",xmbuf.pwnum);
 this_printf(5,"Message Bases : %d \n",xmbuf.nbbs);
 for(i=0; i<=xmbuf.nbbs; i++)
	{
	this_printf(5,"z.Z Msgs %-2d next %-2d nconf max %-2d hmess rec %-2d Name %s \n",
	 xmbuf.mconf[i],xmbuf.cmess[i],xmbuf.nconf[i],xmbuf.hmess[i],
	 xmbuf.conf[i]);
	if(xmbuf.hmess[i]<0 || xmbuf.cmess[i]<0)
		this_printf(5,"Schwerer Fehler! Negative Msg-Nummer!\n\n");
	}
 this_printf(5,"Textcolor	 : %d \n",xmbuf.textcolor);
 this_printf(5,"Backcolor	 : %d \n",xmbuf.backcolor);
 this_printf(5,"Old Drives : %s \n",xmbuf.drvlist);
 this_printf(5,"Path  : %s \n",xmbuf.defdir);
 fl = xmbuf.flags;
 if ((fl&(1<<1))>0)
  {
	this_printf(5," Chat mode on\n");
  }
 if ((fl&(1<<2))>0)
  {
	this_printf(5," Log to disk\n");
  }
 if ((fl&(1<<3))>0)
  {
	this_printf(5," Guest login\n");
  }
 if ((fl&(1<<4))>0)
  {
	this_printf(5," Accounting\n");
  }
 if ((fl&(1<<5))>0)
  {
	this_printf(5," New User Acc\n");
  }
 if ((fl&(1<<7))>0)
  {
	this_printf(5," Color Monitor\n");
  }
 if ((fl&(1<<8))>0)
  {
	this_printf(5," Survey on\n");
  }
 if ((fl&(1<<9))>0)
  {
	this_printf(5," EMail delete after User\n");
  }
 if ((fl&(1<<10))>0)
  {
	this_printf(5," Mes to Sysop\n");
  }
 if ((fl&(1<<11))>0)
  {
	this_printf(5," Upload to Sysop\n");
  }
 if ((fl&(1<<12))>0)
  {
	this_printf(5," Bulletins on\n");
  }
 if ((fl&(1<<13))>0)
  {
	this_printf(5," Upload nocharge\n");
  }
 this_printf(5,"UL/DL : %d \n",xmbuf.counters[0]);
 this_printf(5,"timeout: %d \n",xmbuf.counters[1]);
 this_printf(5,"F-Mail : %d \n",xmbuf.counters[2]);
 this_printf(5,"FNmsgb : %d \n",xmbuf.counters[3]);
 this_printf(5,"FNreci : %d \n",xmbuf.counters[4]);
 this_printf(5,"Fnum  : %d \n",xmbuf.fnum);
}

void main(int argc, char *argv[])
{
 short num = 0;
 FILE *confp;
 char timestring[40];
 time_t tim = time(NULL);
 struct tm *tp;

 device = 1;
 if(argc<=1)
  {
	printf("Usage: [local|remote] level function\nCiao\n");
	exit(2);
  }
 check_arg(argc,argv);
 if (device == 2) printf("This is only local.\n\n");
 else printf("This is remote.\n\n");

 if(main_function & 2)
  {
   flist=fopen("temp.lst","w");
   if(flist==NULL) exit(-2);
  }
 tp = localtime(&tim);
 strftime(timestring, 40, " %a %d-%b-%Y  %H:%M",tp);
 this_printf(1,"Ee\n");
 this_printf(1,"TurboBoard BBS v2.x File-Lister\n");
 this_printf(2,"\nGenerated by Fil_Lis V. %s on %s\n\n", VERSION,timestring);
 this_printf(1,"V %s  %s\n", VERSION, __DATE__);
 this_printf(1,"\nCopyright by Wolfgang Zweygart.\n");
#if defined(LATTICE)
 this_printf(1,"Compiled with Lattice C5\n");
#elif defined(__PUREC__)
 this_printf(1,"Compiled with Pure C V%x.%02x\n",
		__PUREC__ / 0x100, __PUREC__ & 0xff);
#elif defined(__TURBOC__)
 this_printf(1,"Compiled with Turbo C V%x.%02x\n",
		__TURBOC__ / 0x100, __TURBOC__ & 0xff);
#endif
 this_printf(1,"Call microTalk for support.");
 this_printf(1,"  +49 711 628456");
 this_printf(1,"\n\n");

 les_opt();
 Dgetpath(oldpath,0);

 sprintf(conf_name,"%c:%s\\%sCONFIG.DAT",Dgetdrv()+'A',oldpath,datas);
 confp = fopen(conf_name,"rb");
 if (confp == 0)
 {
  sprintf(conf_name,"%c:%s\\CONFIG.DAT",Dgetdrv()+'A',oldpath);
  confp = fopen(conf_name,"rb");
 }
 if (confp > 0)
 {
  this_printf(5,"CONFIG-path	%s \n",conf_name);
  fread((char*)&xmbuf,sizeof(xmbuf),1,confp);
	for(num = 0; num < xmbuf.ndirect; num++)
			test_dir((char*)&xmbuf.subdir[num]);
  fclose(confp);
 }
 else this_printf(5,"\n %s nicht gefunden. Abbruch.",conf_name);
 if(main_function & 2) fclose(flist);
 exit(0);
}
