
/* SUpl v1.1 - (c) S.Gillibrand 1996-7 - Digital Design */
/* Compiled/Coded With DICE */

/*                                                                     ---- */
/* 1.0 - First "messy" release.                                             */
/* 1.1 - Updated code, reworked it a bit and speeded things up :)           */
/*                                                                     ---- */


#include <stdio.h>
#include <string.h>
#include <transamiga.h>
#include <ctype.h>

struct TransBase *TransBase=NULL;
struct BBSLine line;
int bbsline=0;

main(ac,av)
char *av[];
{

	char outstr[100], filename[26], dupecheck[26];
	int index, lg;

	bbsline=atol(av[1]);

	sprintf(dupecheck,"%s\n",av[3]);

	if ((TransBase=(struct TransBase *)OpenLibrary("transamiga.library",0L))==NULL)
	{
		Out("*** Error: Couldn't open transamiga.library!\n");
		close_all();
		exit(10);
	}

	if (ObtainLine(bbsline,&line))
	{
		sprintf(outstr,"*** Error: Line %d is not currently active!\n",bbsline);
		Out(outstr);
		close_all();
		exit(10);
	}

	if (ac < 4) {
		Out("\nSUpl v1.0 - (c) S.Gillibrand 1996 - Digital Design - Part Of Upl!\n\n");
		Out("Usage: \n\n");
		Out("	SUpl <BBS Line> <Path Of File Created With DUpl> <Filename To Check>\n");
		Out("	Case Doesn't Matter In The Filename Field\n\n");
		close_all();
		exit(1);
	}

	sprintf(outstr,"\n[0m[1mSUpl v1.1 - (c) S.Gillibrand 1996-7 - Digital Design - Part Of Upl!\n\nPlease Wait, Checking For Duplicate Filename [[0m[7m%s[0m[1m]...[0m",av[3]);
	Out(outstr);

	FILE *searchf=fopen(av[2],"r");

	if (searchf==NULL) {
		sprintf(outstr,"\n\nUnable to open dupe check file: %s\n\n",av[2]);
		Out(outstr);
		close_all();
		exit(10);
	}

	lg=strlen(dupecheck);
	dupecheck[lg]='\0';
	for (index=0;index<lg;index++) {
		if (dupecheck[index]>='a' && dupecheck[index]<='z')
			dupecheck[index]=dupecheck[index]-32;
	}

	if (access("T:Upl_Dupe",0)==0)
		remove("T:Upl_Dupe");

	do {
		fgets(filename, sizeof(filename), searchf);
		lg=strlen(filename);
		filename[lg]='\0';
		for (index=0;index<lg;index++) {
			if (filename[index]>='a' && filename[index]<='z')
				filename[index]=filename[index]-32;
		}
		if (strcmp(filename,dupecheck)==0) {
			Out("\nDuplicate filename found!\n");
			creat("T:Upl_Dupe");
			close_all();
			exit(0);
		}
	}while(!feof(searchf));

	fclose(searchf);

	Out("\nFilename not found on BBS filebase.\n");

	close_all();
	exit(0);

}

Out(char *str)
{
	char temp[3000];
	convert_lf(temp,str,strlen(str));
	ANSIWrite(&line,temp);
	SerWrite(&line,temp);
}

close_all()
{
	ConAbortRead(&line);

	printf("[0m");

	ReleaseLine(&line);
	if (TransBase) CloseLibrary(TransBase);
}

convert_lf(char *buf, char *str, LONG len)
{
	LONG i, ii=0;
	
	for (i=0; i<len; i++)
	{
		buf[ii]=str[i];
		if (buf[ii]=='\n')
		{
			buf[ii]='\r'; buf[ii+1]='\n';
			ii++;
		}
		ii++;
	}
	buf[ii]='\0';
}

