/*******************************************************************************

Copyright (c) 1997-2004, Perforce Software, Inc.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1.  Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

2.  Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTR
IBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

/*******************************************************************************
 * Name		: p4clientapi.cc
 *
 * Author	: Tony Smith <tony@perforce.com> or <tony@smee.org>
 *
 * Description	: Ruby bindings for the Perforce API. Main interface to the
 * 		  Perforce API.
 *
 ******************************************************************************/
#include <ruby.h>
#include <clientapi.h>
#include "p4result.h"
#include "clientuserruby.h"
#include "p4clientapi.h"



/*******************************************************************************
 * Our Ruby classes.
 ******************************************************************************/
extern VALUE 	cP4;	// Base P4 class
extern VALUE 	eP4;	// Exception class

P4ClientApi::P4ClientApi()
{
    initCount = 0;
    debug = 0;
    server2 = 0;
    tagged = 0;
    exceptionLevel = 2;
}

P4ClientApi::~P4ClientApi()
{
    if ( initCount )
    {
	Error e;
	client.Final( &e );
	// Ignore errors
    }
}

void 
P4ClientApi::SetDebug( int d )
{
    debug = d;
    ui.SetDebug( d ? d - 1 : 0 );
}

//
// connect to the Perforce server.
//

VALUE 
P4ClientApi::Connect()
{
    if ( debug )
	fprintf( stderr, "[P4] Connecting to Perforce\n" );

    if ( initCount )
    {
	rb_warn( "P4#connect - Perforce client already connected!" );
	return Qtrue;
    }

    Error	e;

    client.Init( &e );
    if ( e.Test() && exceptionLevel )
	Except( "P4#connect", &e );

    if ( e.Test() )
	return Qfalse;

    initCount++;
    return Qtrue;
}


//
// Disconnect session
//
VALUE 
P4ClientApi::Disconnect()
{
    if ( debug )
	fprintf( stderr, "[P4] Disconnect\n" );

    if ( ! initCount )
    {
	rb_warn( "P4#disconnect - not connected" );
	return Qtrue;
    }
    Error	e;
    client.Final( &e );

    initCount--;
    return Qtrue;
}

void 
P4ClientApi::Tagged()
{
    client.SetProtocol( "tag", "" );
    tagged = 1;
}

void 
P4ClientApi::ParseForms()
{
    client.SetProtocol( "tag", "" );
    client.SetProtocol( "specstring", "" );
    tagged = 1;
}

//
// Run returns the results of the command. If the client has not been
// connected, then an exception is raised but errors from Perforce
// commands are returned via the Errors() and ErrorCount() interfaces
// and not via exceptions because one failure in a command applied to many
// files would interrupt processing of all the other files if an exception
// is raised.
//

VALUE 
P4ClientApi::Run( const char *cmd, int argc, char * const *argv )
{
    // Save the entire command string for our error messages. Makes it
    // easy to see where a script has gone wrong.
    StrBuf	cmdString;
    cmdString << "\"p4 " << cmd;
    for( int i = 0; i < argc; i++ )
        cmdString << " " << argv[ i ];
    cmdString << "\"";

    if ( debug )
	fprintf( stderr, "[P4] Executing %s\n", cmdString.Text()  );

    if ( ! initCount && exceptionLevel )
	Except( "P4#run", "not connected." );

    if ( ! initCount )
	return Qfalse;

    // Clear out any results from the previous command
    ui.Reset();

    RunCmd( cmd, &ui, argc, argv );

    P4Result &results = ui.GetResults();

    if ( results.ErrorCount() && exceptionLevel )
	Except( "P4#run", "Errors during command execution", cmdString.Text() );

    if ( results.WarningCount() && exceptionLevel > 1 )
	Except( "P4#run", "Warnings during command execution",cmdString.Text());

    return results.GetOutput();
}


//
// RunCmd is a private function to work around an obscure protocol
// bug in 2000.[12] servers. Running a "p4 -Ztag client -o" messes up the
// protocol so if they're running this command then we disconnect and
// reconnect to refresh it. For efficiency, we only do this if the 
// server2 protocol is either 9 or 10 as other versions aren't affected.
//
void
P4ClientApi::RunCmd( const char *cmd, ClientUser *ui, int argc, char * const *argv )
{
    client.SetArgv( argc, argv );
    client.Run( cmd, ui );

    // Have to request server2 protocol *after* a command has been run. I
    // don't know why, but that's the way it is.

    if ( ! server2 )
    {
	StrPtr *pv = client.GetProtocol( "server2" );
	if ( pv )
	    server2 = pv->Atoi();
    }

    if ( tagged && StrRef( cmd ) == "client" && server2 >= 9 && server2 <= 10  )
    {
	if ( argc && ( StrRef( argv[ 0 ] ) == "-o" ) )
	{
	    if ( debug )
		printf( "Resetting client to avoid 2000.[12] protocol bug\n" );

	    Error e;
	    client.Final( &e );
	    client.Init( &e );

	    // Pass any errors down to the UI, so they'll get picked up.
	    if ( e.Test() ) 
		ui->HandleError( &e );
	}
    }
}




// Raises an exception or returns Qfalse on bad input

VALUE
P4ClientApi::SetInput( VALUE input )
{
    if ( debug )
	fprintf( stderr, "[P4] Received input for next command\n" );

    if ( ! ui.SetInput( input ) )
    {
	if ( exceptionLevel )
	    Except( "P4#input", "Error parsing supplied data." );
    	else
	    return Qfalse;
    }
    return Qtrue;
}

void
P4ClientApi::GCMark()
{
    if ( debug >= 3 )
	fprintf( stderr, "[P4] Ruby asked us to do garbage collection\n" );

    // We don't hold Ruby objects. But our UI does.
    ui.GCMark();
}

void
P4ClientApi::Except( const char *func, const char *msg )
{
    StrBuf	m;
    m << "[" << func << "] " << msg;
    rb_raise( eP4, m.Text() );
}

void
P4ClientApi::Except( const char *func, const char *msg, const char *cmd )
{
    StrBuf m;

    m << msg;
    m << "( " << cmd << " )";
    Except( func, m.Text() );
}

void
P4ClientApi::Except( const char *func, Error *e )
{
    StrBuf	m;

    e->Fmt( &m );
    Except( func, m.Text() );
}

