/**********************************************************************
   FS_TINY.C
   Z-World, 2001
  
   This is a very minimal use of the DevMate
   filesystem. It only uploads a few files, to
   be made avaliable to a web server on the
   DevMate. This is a recommended style, that
   could be used with the VARIABLE support on
   the DevMate, to create dynamic webpages to
   show statistics or other status information.
**********************************************************************/

/*
 * get some sample data to send
 */
#ximport "samples/tcpip/http/pages/static.html"    index_html
#ximport "samples/tcpip/http/pages/rabbit1.gif"    rabbit1_gif

/*
 * Define this to format the FS on the DevMate. This may not work
 * if another service, such as logging, has a file open on the DevMate!
 */
//#define FORMAT

/*
 * Turn on debug messages, if this is uncommented
 */
//#define TC_FS_DEBUG

/*
 * Select the filesystem support
 */
#define USE_TC_FS

/*
 * Uncomment this line if this program will be loaded over ethernet. This will
 * force the DevMate communication to use serial port A, re-using the programming
 * link that was used to download the program.
 */
//#define USE_TC_LOADER

#use "tc_conf.lib"

/*
 * the file pointer, that will be used in all commands to the SP
 */
DMFile file;

/*
 * Send a file to the DevMate, and
 * return it's file ID
 */
int send_file(long data, char *name)
{
	auto int retval;
	auto long big_len;
	auto int length, offset, writesize;
#define BUFSIZE	64
	auto char buf[BUFSIZE];
	auto FNumber id;

	/* does the file allready exist? */
	if(TC_SUCCESS == devmate_fs_idlookupB(&file, name, &id, 0)) {
		/* it does - delete it */
		printf("File allready existed w/ id %d. Removing it...\n",id);
		devmate_fs_deleteB(&file, id, 0);
	}
	
	/* find the length of the */
	if(xmem2root((char *)&big_len, data, 4)) {
		// error
		exit(1);
	}
	length = (int)big_len;
	offset = 0;
	
	printf("Opening file '%s'...\n",name);

	/* open the file */
	while(TC_PENDING == (retval = devmate_fs_open(&file, 0, name, TC_FS_FLAGS_NEWID)))
		devmate_tick();
	if(TC_SUCCESS != retval) {
		// error - open failed
		exit(2);
	}

	printf("Sending the file...\n");

	/* send the file */
	while(offset < length) {
		writesize = length - offset;
		if(writesize > BUFSIZE)
			writesize = BUFSIZE;

		/* get one block of data */
		if(xmem2root(buf, data + 4 + offset, writesize)) {
			// error
			exit(3);
		}

		/* send that block to the target */
		retval = devmate_fs_append(&file, buf, writesize);
		if(-1 == retval) {
			// error! transaction is cancled!
			exit(4);
		}
		offset += retval;

		/* drive the communications */
		devmate_tick();
	}

	printf("Send complete - closing the file...\n");

	/* file is written - close it all! */
	while(TC_PENDING == (retval = devmate_fs_close(&file,&id)))
		devmate_tick();

	if(TC_SUCCESS != retval) {
		// error - close failed, and the transaction
		// has been rolled back
		exit(5);
	}

	printf("File '%s' sent correctly!\n",name);

	/* return the id of the created file */
	return id;
}

void main(void)
{
	int rv;
	long	timeout;
	FNumber index_file, logo_file, test_id;

	/*
	 * Standard initialization
	 */	
	devmate_init();

	/*
	 * Sync with the target
	 */
#ifdef FORMAT
	rv = devmate_fs_syncB(&file,TC_FS_FORMAT,0);
#else
	rv = devmate_fs_syncB(&file,0,0);
#endif
	if(TC_SUCCESS != rv) {
		printf("Error in init!\n");
		exit(-1);
	} else {
		printf("INIT was successfull!\n");
	}

	/*
	 * Send some data
	 */
	index_file = send_file(index_html,"/index.html");
	logo_file = send_file(rabbit1_gif,"/rabbit1.gif");

	/*
	 * The web page should be visible now at:
	 *   http://[devmate_ip]/index.html
	 */

	printf("All done!\n");	
}