/*======================================================================*
 *                                                                      *
 *            	Front-end for single-file transfer protocols			*
 *                                                                      *
 *======================================================================*/

#version        $10
#address        $7000

#include        runtime.h
#include        bbs_pro.h

#define		DISK_2		2
#define		MODEM		5


main()
{
	char selection;
	cr_lf();
	if (parm1 [0] == 0 || parm2 [0] == 0) {
		echose("Parameter error!");
		return;
	}
	strcpy (parm1, parm2);
	parse_filename();					/* put filename ONLY into parm_2 */
	if ( u_downs >= u_ups + 1 * u_dlratio ) {
		echose("Sorry, but you are over your ratio\for uploads:downloads");
		aborted = 1;
		return;
	}
	if (u_flags[0] & 32 > 0)					/* auto-Lmodem?	*/
		selection = 'L';
	else {
		echose (" [X]  Standard/CRC Xmodem");
		echose (" [W]  Windowed Xmodem");
		echose (" [Y]  Ymodem (Xmodem-1k)");
		echose (" [S]  SEAlink");
		echose (" [L]  Lmodem");
		echose (" [Q]  Abort transfer");
		cr_lf();
		echos  ("   Which protocol? ");
		while ( 1 ) {
			selection = toupper (get_key());
			if (selection == 'X' || selection == 'W' ||
				selection == 'Y' || selection == 'S' ||
				selection == 'L' || selection == 'Q') {
					echo (selection);
					cr_lf();
					break;
			}
		}
	}
	aborted = 0;
	case ( selection ) {
		'X'	:	overlay ("XMSEND");
		'W'	:	overlay ("WXSEND");
		'Y'	:	overlay ("YMSEND");
		'S'	:	overlay ("SEASEND");
		'L'	:	overlay ("LMSEND");
		'Q'	:	aborted = 1;
	}
	MIOsuspend();
	close (DISK_2);
	MIOresume();
	cr_lf();
	if (aborted)
		echose("File transfer aborted..");
	else {
		echose("File transfer completed!");
		++u_downs;
	}
}


/*
 *		Return TRUE if the user is over their download:upload ratio
 *		-----------------------------------------------------------
 */

over_download_ratio()
{
#asm {
		lda		u_downs
		sta		.remainder
		lda		u_downs+1
		sta		.remainder+1
		lda		#0
		sta		.result
		sta		.result+1
		sta		.divisor+1
		lda		u_dlratio
		sta		.divisor
.divloop:
		lda		.remainder+1
		cmp		.divisor+1
		bne		.noteq1
		lda		.remainder
		cmp		.divisor
.noteq1:
		bcc		.done
		inc		.result
		bne		.novf1
		inc		.result+1
.novf1:	lda		.remainder
		sec
		sbc		.divisor
		sta		.remainder
		lda		.remainder+1
		sbc		.divisor+1
		sta		.remainder+1
		jmp		.divloop
.done:
		lda		.result+1
		cmp		u_ups+1
		bne		.noteq2
		lda		.result
		cmp		u_ups
.noteq2:
        bcc		.allow
		lda		#1
		ldx		#0
		rts
.allow:	lda		#0
		ldx		#0
		rts
.divisor:	ds.b	2
.remainder:	ds.b	2
.result:	ds.b	2
	}
}



parse_filename()
{
	byte x, y;
	x = length (parm1);
	while ( x ) {
		if (parm1 [x] == '<' || parm1 [x] == ':' ||
			parm1 [x] == '>')
			break;
		--x;
	}
	++x;
	parm2 [0] = 0;
	for (y = 1; x <= length(parm1); ++x) {
		parm2 [y++] = parm1 [x];
		parm2 [0]   = parm2 [0] + 1;
	}
}
