/*
OS QTH function for WA7MBL BBS and Buckmaster Publishing HAM CD ROM

Syntax:
OSQTH <port#> <call of requestor> QTH call1 call2 call2...

Where <port#> is a single char in the range 1-8 and indicates
the port # where the reply is to be sent.  If ECHO is on, the reply
will also appear on the display.  This data is automatically stuffed by the
WA7MBL OS function.  To run from the BBS type:

>OS QTH WA4ONG N1BIC ...

And the reply will be sent back to the same port requesting it.  The
OS function CANNOT be run from the local SYSOP screen.

This code and all associated data may be freely exchanged for non-pecuniary
purposes.  Services or efforts that compete in any way with services or
products offered commercially by Buckmaster are expressly forbidden.


WA4ONG
*/

#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
char            CR = 0x0D;
char						echo;
int							dpm[] = {31,28,31,30,31,30,31,31,30,31,30,31};
char            bigst[255], filename[90], sucess, ports[8], dfilepath[80],
								lfilepath[80], DV;
long            size, low, high, middle, records, number;
FILE           *config, *calls, *logfile;

extern unsigned _heaplen = {2000};
extern int directvideo = {0};


#include "os.h"


main(int argc, char *argv[])
{
	int             i;
	int             ch, result;
	long            secs;
	char           *asc_time,temp[20];
	if (argc < 3)
		return (1);

	config = fopen("oscfg.bin", "r");
	if (config == NULL) {
		fclose(config);
		printf("OS QTH config file ""OSCFG.BIN"" open failed!\n");
		return (1);
	}
	fread(&DV,1,1,config);
	fread(&echo,1,1,config);
	fread(ports,8,1,config);
	fread(dfilepath,80,1,config);
	fread(lfilepath,80,1,config);
	fclose(config);
	comport = atoi(argv[1]) - 1;
	sprintf(filename, "%sport%.1ios.log", lfilepath,comport + 1);
	logfile = fopen(filename, "a");
	time(&secs);
	fprintf(logfile, "%s request by %s on port %.1i on %s", argv[3], argv[2], comport+1, ctime(&secs));
	if (stricmp("qth", argv[3]) == 0) {
		if (argc < 5) {
			fprintf(logfile, "Too few parameters");
			fclose(logfile);
			return (1);
		}
		sucess = 0;
		chdir("e:\\");
		i = open(dfilepath, O_RDONLY);
		size = filelength(i);
		close(i);
		records = size / 129;
		calls = fopen(dfilepath, "r");
		if (calls == NULL) {
			fclose(calls);
			fprintf(logfile, "CD Rom data file open failed!\n");
			fclose(logfile);
			return (1);
		}

		for (i = 4; i < argc; i++) {
			result = get_qth(argv[i], bigst);
			outstr("\n");
			outstr(bigst);
			fprintf(logfile, "%s qth ", argv[i]);
			if (result == 0)
				fprintf(logfile, "not found\n");
			else
				fprintf(logfile, "found\n");
		}
		if (sucess != 0) {
			sprintf(bigst, "\nFrom the BUCKMASTER PUBLISHING CD-ROM Directory (c) 1989\n");
			outstr(bigst);
		}
		fclose(calls);
		fclose(logfile);
		fclose(config);
		delay(2000);
		return (0);
	}			/* if qth */
	if (stricmp("time", argv[3]) == 0) {
		sprintf(bigst, "%s", ctime(&secs));
		outstr(bigst);
		fclose(logfile);
		delay(2000);
		return (0);
	}
	sprintf(bigst, "Unknown OS function - %s\n", argv[3]);
	outstr(bigst);
	fclose(logfile);
	delay(2000);
	return (1);
}

outstr(char *st)
{
	int             i;
	i = 0;
	if (st[0] == 0)
		return;
	do {
		if (st[i] == 10) {
			outSIO(CR);
			if(echo == 1){
				putch(CR);
				putch(10);
			}
		} else {
			outSIO(st[i]);
			if(echo == 1) {
				putch(st[i]);
			}
		}
	} while (st[++i] != 0);
}

int
get_qth(char call[], char buffer[])
{
	int             result;
	char            buff1[10];
	result = search_format(call, buff1);
	if (result == 0) {
		sprintf(buffer, "%s is not a valid call format\n", call);
		return(0);
	}
/*	printf("%.6s",buff1);*/
	result = search_record(buff1);
	if (result != 0) {
		get_frecord(number, buffer);
		sucess++;
		return(1);
	} else
		sprintf(buffer, "%s not found\n", call);
	return(0);
}

int
search_format(char call[], char outb[])
{
	int             result, call_length, i;
	call_length = strlen(call);
	if ((call_length < 4) || call_length > 6)
		return (0);
	i = 0;
	result = -1;
	do {
		if ((call[i] & 0x70) == '0')
			result = i;
		i++;
	} while ((i < 7) && (result == -1));
	if ((result < 1) || result > 2)
		return (0);
	strcpy(outb, "      ");
	strcpy(outb, &call[result]);
	i = strlen(outb);
	outb[i] = ' ';
	if (result == 1) {
		outb[4] = ' ';
		outb[5] = call[0];
	}
	if (result == 2) {
		outb[4] = call[0];
		outb[5] = call[1];
	}
	return (1);
}

int
search_record(char call[])
{
	char            done, buff[8], gotit;
	int             result;
	low = -1;
	high = records;
	gotit = done = 0;
	do {
		if (high > low + 1)
			middle = (high + low) >> 1;
		else {
			low = middle = high;
			done++;
		}
		get_irecord(middle, buff);
		result = strnicmp(call, &buff[0], 6);
		if (result == 0) {
			done++;
			gotit++;
			number = middle;
		}
		if (result < 0)
			high = middle;
		if (result > 0)
			low = middle;
	} while (done == 0);
	return (gotit);
}

get_frecord(long number, char buffer[512])
{
	char            buff[130], fname[12], mi[3], lname[21], jr[5], jrs[2], street[36], city[21]
								 ,state[3], zip5[6], zip4[6], dot[3], callp[3], calln[2], callsuf[4], class[2]
								 , temp[4], dob[3];
	float			  		aa,bb,dd;
	int							emon,eday,eyear,edoy,i,done;
	dot[1] = ' ';
	dot[2] = 0;;
	jrs[1] = 0;
	get_record(number, buff);
	strim(&buff[30], 11, fname);
	strim(&buff[41], 1, mi);
	if (mi[0] == 0)
		dot[0] = 0;
	else
		dot[0] = '.';
	strim(&buff[6], 20, lname);
	strim(&buff[26], 4, jr);
	if (jr[0] == 0)
		jrs[0] = 0;
	else
		jrs[0] = ' ';
	strim(&buff[0], 2, callp);
	if (callp[0] == ' ') {
		callp[0] = callp[1];
		callp[1] = 0;
	}
	strim(&buff[2], 1, calln);
	strim(&buff[3], 3, callsuf);
	strim(&buff[126], 1, class);
	strim(&buff[42], 35, street);
	strim(&buff[77], 20, city);
	strim(&buff[97], 2, state);
	strim(&buff[99], 5, zip5);
	strncpy(temp,&buff[116],2);
	temp[2]=0;
	eyear=atoi(temp);
	strncpy(temp,&buff[118],3);
	temp[3]=0;
	edoy=atoi(temp);
	if((eyear%4) == 0)
	dpm[1] = 29;
	else
	dpm[1] = 28;
	i=0;
	done=0;
	eday = edoy;
	do{
		if (eday <= dpm[i]) done=1;
		else eday = eday - dpm[i];
		i++;
	} while (done == 0);
	emon = i;
	strncpy(dob,&buff[114],2);
	dob[2]=0;
	/*
	 * zip4[0] = 0; strim(&buff[141],4,&zip4[1]); if(zip4[1] != 0)
	 * zip4[0]='-';
	 */
	sprintf(buffer, "%s %s%s%s%s%s - %s%s%s %s (%s) %i/%i/%i\n%s\n%s, %s %s\n"
		,fname, mi, dot, lname, jrs, jr, callp, calln, callsuf, class, dob, emon,
		eday, eyear, street, city, state, zip5);
}
strim(char inb[], int count, char outb[])
{
	char            done;
	int             i;
	done = 0;
	strncpy(outb, inb, count);
	outb[count] = 0;
	i = count;
	do {
		if (outb[--i] == ' ')
			outb[i] = 0;
		else
			done++;
	} while ((i != 0) & (done == 0));
}

get_record(long number, char buff[])
{
	fseek(calls, 129 * number, 0);
	if(DV==1){
		DV_Nice();
		DV_Crit();
	}
	dropdtr();
	fread(buff, 129, 1, calls);
	raisedtr();
	if(DV==1){
		DV_Uncrit();
		DV_Nice();
	}
}



get_irecord(long number, char buff[])
{
	char buff1[130];
	fseek(calls, 129 * number, 0);
	if(DV==1){
		DV_Nice();
		DV_Crit();
	}
	dropdtr();
	fread(buff1, 129, 1, calls);
	raisedtr();
	if(DV==1){
		DV_Uncrit();
		DV_Nice();
	}
	buff[0] = buff1[2];
	buff[1] = buff1[3];
	buff[2] = buff1[4];
	buff[3] = buff1[5];
	buff[4] = buff1[0];
	buff[5] = buff1[1];
	buff[6] = 0;
}
