#include "microdot.h"

#include <constructor.h>

#ifndef NO_RFC

#include "uuconv.h"
#include "hd1:cl5/net/rfc/rmail.c"
#include "hd1:cl5/net/rfc/rnews.c"

#include "ogre.h"
#include "ogre_protos.h"

#if 0
char *ng2board( char *ngname )
{
	static char tng[ 80 ];

	strcpy( tng, "/" );
	strncat( tng, ngname, 78 );
	tng[ 79 ] = 0;

	return( tng );
}
#endif

LONG lastoffset;

static char *cur_command;
static char *cur_file;
static char *cur_sys;

CONSTRUCTOR_P(initcc,500)
{
	cur_command = AllocMem( 1024 + 512, MEMF_CLEAR );
	if( cur_command )
	{
		cur_file = cur_command + 1024;
		cur_sys = cur_file + 256;
		return( 0 );
	}
	outofmem();
	return( 1 );
}

DESTRUCTOR_P(initcc,500)
{
	if( cur_command )
		FreeMem(cur_command, 1024 + 512 );
}

static void testfilename( void )
{
	BPTR testlock;
	char *p, *p2;
	char temp[ 256 ];
	int gotp = 0;

	testlock = Lock( cur_file, SHARED_LOCK );
	if( testlock )
	{
		UnLock( testlock );
		return;
	}

	p = cur_file;
	p2 = temp;

	while( *p )
	{
		if( gotp && isupper( *p ) )
			*p2++ = *p;
		if( *p == '.' )
			gotp = TRUE;
		*p2++ = *p++;
	}
	*p2 = 0;

	strcpy( cur_file, temp );
}

static void processXfile( char *name )
{
	BPTR fromfile = Open( name, MODE_OLDFILE );
	char *p;
	char toname[ 64 ];
	int rc = 1;
	int pri;

	cur_command[ 0 ] = 0;
	cur_file[ 0 ] = 0;
	cur_sys[ 0 ] = 0;
	tempfile[ 0 ] = 0;

	if( !fromfile )
		return;

	while( FGets( fromfile, linebuffer, 2047 ) )
	{
		clnl( linebuffer );

		switch( toupper( linebuffer[ 0 ] ) )
		{
			case 'C':
				strncpy( cur_command, stpblk( &linebuffer[ 1 ] ), 1023 );
				break;

			case 'U':
				p = stpblk( &linebuffer[ 1 ] );
				while( !isspace( *p ) )
					p++;
				strncpy( cur_sys, stpblk( p ), 255 );
				p = strchr( cur_sys, '!' );
				if( p )
					*p = 0;
				break;

			case 'F':
				strncpy( cur_file, stpblk( &linebuffer[ 1 ] ), 255 );
				testfilename();
				break;
		}
	}
	Close( fromfile );

	sprintf( toname, "sortin.%lx", prefs.counter++ );

	addlog( 0, "executing %s (%s, data %s, to %s)", name, cur_command, cur_file, toname );

	set_cmd( "%s (%s)", name, cur_command );
	set_file( cur_file, CLU_GetFileLen( cur_file ) );

	pri = 'S';
	if( !strnicmp( cur_command, "rnews", 5 ) )
	{
		rc = rnews( cur_file, toname );
	}
	else if( !strnicmp( cur_command, "frnews", 5 ) )
		rc = rnews( cur_file, toname );
	else if( !strnicmp( cur_command, "rmail", 5 ) )
	{
		pri = 'P';
		rc = rmail( cur_file, toname, stpblk( &cur_command[ 5 ] ) );
	}
	else if( !strnicmp( cur_command, "frmail", 5 ) )
	{
		pri = 'P';
		rc = rmail( cur_file, toname, stpblk( &cur_command[ 5 ] ) );
	}
#if 0
	else if( !strnicmp( cur_command, "rlharc", 6 ) )
		rc = dorlharc( cur_file );
#endif
	else if( !strnicmp( cur_command, "rsmtp", 5 ) )
		rc = bsmtp( cur_file );
	else if( !strnicmp( cur_command, "rgsmtp", 6 ) )
		rc = cbsmtp( cur_file );
	else if( !strnicmp( cur_command, "rcsmtp", 6 ) )
		rc = cbsmtp( cur_file );
	else addlog( 0, "unknown command %s!", cur_command );

	DeleteFile( cur_file );
	DeleteFile( name );
	DeleteFile( tempfile );
}

static struct ogwin *ogw;

char gzipfullpath[ 256 ];

void convrfc( void )
{
	struct Window *iw;
	int rc;
	char buff[ 4096 ], *p;

	if( !checkprograminpathfull( "Gzip", gzipfullpath ) )
	{
		askreq( "\"GZip\" nicht gefunden!\nDas Program \"GZip\" muß im Pfad liegen und aufrufbar sein.", "Abbruch" );
		return;
	}

	ogw = ogreInitWindow( scr, WFLG_RMBTRAP, NULL, "MicroDot-UUXQT" );

	ogreAddGroup( ogw, 0, OGFRAME_NONE, NULL );
	ogreAddText( ogw, 0, "Kommando:", 0, "", 50, TRUE );
	ogreAddText( ogw, 1, "    Data:", 1, "" , 50, TRUE );
	ogreAddText( ogw, 2, "  Status:", 2, "" , 50, TRUE );
	ogreAddFuelGauge( ogw, 3, 3, 100 );
	iw = ogreOpenWindow( ogw );

	pushdir( prefs.transferdir );

	for(;;)
	{
		rc = makefilelist( "", "X.#?", buff, 4096 );

		if( rc < 1 )
			break;

		p = buff;

		while( rc-- )
		{
			char *p2 = strchr( p, 0 ) + 1;
			set_fg( 0 );
			set_status( "Suche X.-Dateien..." );
			set_cmd( "-" );
			set_file( NULL, 0 );
			processXfile( p );
			p = p2;
		}
	}

	popdir();
	if( iw )
		ogreExitWindow( ogw );
}

static UWORD fmtfunc[] = { 0x16c0, 0x4e75 };
void __stdargs set_cmd( STRPTR str, ... )
{
	char buffer[ 256 ];

	RawDoFmt( str, &str + 1, (APTR)fmtfunc, buffer );
	ogreSetStringValue( ogw, 0, buffer );
}
void __stdargs set_status( STRPTR str, ... )
{
	char buffer[ 256 ];

	RawDoFmt( str, &str + 1, (APTR)fmtfunc, buffer );
	ogreSetStringValue( ogw, 2, buffer );
}

void set_file( STRPTR name, int bytes )
{
	char buffer[ 128 ];

	if( !name )
	{
		ogreSetStringValue( ogw, 1, "-" );
		return;
	}

	sprintf( buffer, "%s, %lD Bytes", name, bytes );
	ogreSetStringValue( ogw, 1, buffer );
	ogreSetMaxVal( ogw, 3, bytes );
}

void set_fg( int p )
{
	ogreSetValue( ogw, 3, p );
}

void __stdargs addlog( int dummy, char *str, ... )
{
	char buffer[ 256 ];

	RawDoFmt( str, &str + 1, (APTR)fmtfunc, buffer );
	printlog( "%s\n", buffer );	
}

#endif
