/*
 *  Copyright (C) 2001-2002, Richard J. Moore <rich@kde.org>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 */

#include <string.h>
#include <qregexp.h>
#include <kaboutdata.h>
#include <kapplication.h>
#include <kcmdlineargs.h>
#include <kdebug.h>
#include <klocale.h>

#include <kjs/interpreter.h>

#include "kjsembedpart.h"
#include "jsconsolewidget.h"
#include "securitypolicy.h"

static KCmdLineOptions options[] =
{
    { "console", I18N_NOOP("Displays the KJSEmbed console"), 0 },
    { "nogui", I18N_NOOP("Disables all GUI facilities"), 0 },
    { "exec", I18N_NOOP("Call application.exec() after running the script."), 0 },
    { "+[file]", I18N_NOOP("Script to execute, or '-' for stdin."), 0 },
    { 0, 0, 0 }
};

int main( int argc, char **argv )
{
    bool nogui = false;
    bool exec = false;

    // Search for nogui option
    for ( int i = 0 ; i < argc ; i++ ) {
	if ( strcmp( argv[i], "-nogui" ) == 0 )
	    nogui = true;
	if ( strcmp( argv[i], "--nogui" ) == 0 )
	    nogui = true;
    }

    // Cmd Line
    KAboutData about( "kjscmd", I18N_NOOP("KJSCmd"), "0.1",
		      I18N_NOOP("Provides access to KJS from the command line"),
		      KAboutData::License_LGPL, I18N_NOOP("(c) 2001-2002 Richard Moore") );
    about.addAuthor( "Richard Moore", 0, "rich@kde.org" );

    KCmdLineArgs::init( argc, argv, &about );
    KCmdLineArgs::addCmdLineOptions( options ); // Add our own options.
    KCmdLineArgs *args = KCmdLineArgs::parsedArgs();

    // Setup KApplication
    KApplication *app;
    if ( nogui ) {
	app = new KApplication( false, false );
    }
    else {
	app = new KApplication;
	app->connect( app, SIGNAL( lastWindowClosed() ), SLOT(quit()) );
    }

    // Setup Interpreter
    KJSEmbed::SecurityPolicy::setDefaultPolicy( KJSEmbed::SecurityPolicy::CapabilityAll );
    KJSEmbed::KJSEmbedPart *part = new KJSEmbed::KJSEmbedPart;
    part->publish( app, "application" );

    // Create Visible Console?
    if ( !args->count() || args->isSet( "console" ) ) {
	if ( !nogui ) {
	    kdDebug() << "Creating console" << endl;
	    KJSEmbed::JSConsoleWidget *console = part->view();
	    part->publish( console, "console" );

	    // Setup Window
	    console->resize( 600, 450 );
	    console->show();
	}
    }

    if ( args->isSet( "exec" ) || !args->count() )
	exec = true;

    if ( args->count() ) {
	kdDebug() << "Executing script file '" << args->arg(0) << "'" << endl;

	QString script;
	if ( args->arg(0) == QString("-") ) {
	    kdDebug() << "Executing script from stdin" << endl;
	    QTextStream ts( stdin, IO_ReadOnly );
	    script = ts.read();
	}
	else {
	    QString filename = args->arg(0);
	    kdDebug() << "Executing script " << filename << endl;
	    QFile file( filename );
	    if ( file.open( IO_ReadOnly ) ) {
		QTextStream ts( &file );
		script = ts.read();
	    }
	}

//	fprintf(stderr, "---SCRIPT---\n%s---SCRIPT---\n", script.local8Bit().data() );

	if ( script == QString::null )
	    return 1;

	bool ok = part->executeScript( script );
	if ( !ok )
	    return 1;
    }
    else {
	exec = true;
    }
    
    if ( !nogui && exec )
	return app->exec();
    else
	return 0;
}

// Local Variables:
// c-basic-offset: 4
// End:
