/*
	Zmcf10.c
	
	Zmodem program for the CF2

*/


#include	<cfxbios.h>		// Persistor BIOS and I/O Definitions
#include	<cfxpico.h>		// Persistor PicoDOS Definitions

#include	<assert.h>
#include	<ctype.h>
#include	<errno.h>
#include	<float.h>
#include	<limits.h>
#include	<locale.h>
#include	<math.h>
#include	<setjmp.h>
#include	<signal.h>
#include	<stdarg.h>
#include	<stddef.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<time.h>

#include	<dirent.h>		// PicoDOS POSIX-like Directory Access Defines
#include	<dosdrive.h>	// PicoDOS DOS Drive and Directory Definitions
#include	<fcntl.h>		// PicoDOS POSIX-like File Access Definitions
#include	<stat.h>		// PicoDOS POSIX-like File Status Definitions
#include	<termios.h>		// PicoDOS POSIX-like Terminal I/O Definitions
#include	<unistd.h>		// PicoDOS POSIX-like UNIX Function Definitions

#include "zmodem.h"


/******************************************************************************\
**	main
\******************************************************************************/
void main(int argc, char **argv)
{
    static ZMODEM zmodem;
	char filename[80];

	// Identify the progam and build
	printf("\nProgram: %s: %s %s \n", __FILE__, __DATE__, __TIME__);
	// Identify the device and its firmware
	printf("Persistor CF%d SN:%ld   BIOS:%d.%d   PicoDOS:%d.%d\n", CFX,
		BIOSGVT.CFxSerNum, BIOSGVT.BIOSVersion, BIOSGVT.BIOSRelease, 
		BIOSGVT.PICOVersion, BIOSGVT.PICORelease);
	
	// No arguments?
	if((argc == 1 || argc > 3))
	{
		printf("\nUsage: zmodem /r /s [filename]");
		exit(1);
	}
 
	// Get the filename
	if(argv[1][0] == '/')
	{
		switch(argv[1][1])
		{
		case 'r':	// Receive
		    zmodemDefaults(&zmodem);
		    zmodemInit(&zmodem);
			zmodemReceive(&zmodem);
			zmodemTerm(&zmodem);
			break;

		case 's':	// Send
			if(argc == 3) 
			{
				strcpy(filename, argv[2]);
			}
			else 
			{
				printf("\nError: send requires filename");
				exit(1);
			}
		    zmodemDefaults(&zmodem);
		    zmodemInit(&zmodem);
			zmodemSend(&zmodem, filename);
			zmodemTerm(&zmodem);
			break;
		default:
			printf("\nUnrecognized argument");
		}
	}
	else printf("\nUnrecognized argument");
    
//	while(!kbhit());	// Pause before exiting to let me read the screen.
    return;	
	
	
	
// Various exit strategies
//	BIOSReset();			// full hardware reset
//	BIOSResetToPBM();		// full reset, but stay in Pesistor Boot Monitor
//	BIOSResetToPicoDOS();	// full reset, but jump to 0xE10000 (PicoDOS)
	return;

	}	//____ main() ____//

