/*
 *
 * botmain.c -- mainline for ACBot
 *
 * This code is copyright 1995 by James A. Cherry, jac@doe.carleton.ca.
 * It is not to be redistributed or modified without his permission.
 *
 */

#include "funddefs.h"
#include "globals.h"
#include "socket.h"
#include "botutil.h"
#include "proto.h"

void process_args( argc, argv )
    int argc;
    char **argv;
{
    int i;

    DoomName = POSLDOOM;
    Logging = LOG_YES;
    strcpy( Password, "***" );
    for( i = 1; i < argc; i++ ) {
	if( strcmp( argv[i], "m" ) == 0 ) DoomName = MARLDOOM;
	if( strcmp( argv[i], "w" ) == 0 ) DoomName = WISDOOM;
	if( strcmp( argv[i], "p" ) == 0 ) DoomName = POSLDOOM;
	if( strcmp( argv[i], "d" ) == 0 ) DoomName = DDDOOM;
	if( strcmp( argv[i], "-ly" ) == 0 ) Logging = LOG_YES;
	if( strcmp( argv[i], "-ln" ) == 0 ) Logging = LOG_NO;
    }
    if( DoomName == MARLDOOM ) strcpy( PlrName, "ACBot" );
    if( DoomName == WISDOOM ) strcpy( PlrName, "ACBot" );
    if( DoomName == POSLDOOM ) strcpy( PlrName, "ACBotPro" );
    if( DoomName == DDDOOM ) {
	strcpy( PlrName, "DDBot" );
	strcpy( Password, "***" );
    }
}

int main( argc, argv )
    int argc;
    char **argv;
{
    int leave, status;
    int i;

    process_args( argc, argv );
    if( Logging == LOG_YES ) {
	if( DoomName == POSLDOOM ) {
	    LogFile = fopen( "plogfile", "w" );
	} else if( DoomName == DDDOOM ) {
	    LogFile = fopen( "dlogfile", "w" );
	} else if( DoomName == WISDOOM ) {
	    LogFile = fopen( "wlogfile", "w" );
	} else if( DoomName == MARLDOOM ) {
	    LogFile = fopen( "mlogfile", "w" );
	}
    }
    ReadWords();
    IgnoreSmaller = 1;
    ReadBlacklist();
    InitStuff();
    if( DoomName == POSLDOOM ) {
	SockP = ClientInit( "synge.math.utoronto.ca", 7777, 2 );
    } else if( DoomName == WISDOOM ) {
	SockP = ClientInit( "next7.cas.muohio.edu", 8888, 2 );
    } else if( DoomName == MARLDOOM || DoomName == DDDOOM ) {
	SockP = ClientInit( "eel.st.usm.edu", 7777, 2 );
    }
    if( SockP == -1 ) {
	LPrint( "%s\n", SocketError ); 
	ExitWindow();
	exit( 20 );
    }
    leave = 0;

    InitBoard();

    while( leave == 0 ) {
	usleep( 500000 );
	CurrTicks++;
	if( CurrState == AWAIT_CONNECT && CurrTicks == 120 )
	    break;
	if( CurrState > IDLE_FOR_GAME && CurrTicks == 3000 )
	    WarnOfExpiry();
	if( CurrState > IDLE_FOR_GAME && CurrTicks == 3600 )
	    Expire();
	status = DoRead( SockP );
	if( status < 0 && status != READ_NODATA ) {
	    if( status == READ_EOF ) {
		leave = 1;
		continue;
	    } else {
		LPrint( "*** Socket error: %s\n", SocketError );
		LPrint( "*** Read error, code %d\n", status );
		
	    }
	} else if( status != READ_NODATA ) {
	    ProcessReadData();
	}
	CheckCommandQueue();
	if( LookingForBoard != LFB_NONE ) {
	    LookForBoard();
	    goto skipstates;
	}
	if( CurrState >= IDLE_FOR_GAME )
	    GenericMessageCheck();
	for( i = 0; i < NUM_STATES; i++ ) {
	    if( StateTable[i].state == CurrState ) {
		(*StateTable[i].function)();
	    }
	}
      skipstates:
    }
    
    LPrint( "Closing socket\n" ); 
    ExitWindow();
    return( 0 );
}
