/* ---------------------------------------------------------------------- */
/*                                                                        */
/* [main.cpp]                      Klt                                */
/*                                                                        */
/* ---------------------------------------------------------------------- */
/*                                                                        */
/* Copyright (c) 1997,1998 by Lars Doelle <lars.doelle@on-line.de>        */
/*                                                                        */
/* This file is part of Klt, an X terminal.                           */
/*                                                                        */
/* The material contained in here more or less directly orginates from    */
/* kvt, which is copyright (c) 1996 by Matthias Ettrich <ettrich@kde.org> */
/*                                                                        */
/* ---------------------------------------------------------------------- */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#include <dcopclient.h>

#include <klocale.h>
#include <kaboutdata.h>
#include <kcmdlineargs.h>
#include <kdebug.h>
#include <kstandarddirs.h>
#include "klt.h"

static const char *description =
  I18N_NOOP("TRACT X terminal for use with KDE.");

static const char *version = "v0.2";


static KCmdLineOptions options[] =
{
   { "tn <terminal>",   I18N_NOOP("Specify terminal type as set in the TERM\nenvironment variable"), "klt" },
   { "noxft",           I18N_NOOP("Do not use XFT (Anti-Aliasing)"), 0 },
   { "schema <name>",   I18N_NOOP("Use given .schema file"), 0 },
   { "!e <command>",    I18N_NOOP("Execute 'command' instead of shell"), 0 },
   // WABA: All options after -e are treated as arguments.
   { "+[args]",         I18N_NOOP("Arguments for 'command'"), 0 },
   { 0, 0, 0 }
};

static bool has_noxft = false;

/* --| main |------------------------------------------------------ */
int main(int argc, char* argv[])
{

  setgid(getgid()); setuid(getuid()); // drop privileges

  // deal with shell/command ////////////////////////////
  const char* wname = PACKAGE;
  Klt *m;

	KAboutData aboutData("klt", I18N_NOOP("Klt"), version, description, KAboutData::License_GPL, "(C) 2001 LLS", 0, 0, "info@lls.com");
	aboutData.addAuthor( "Lady Lodge Systems Ltd", 0, "lls@lls.com" );

	KCmdLineArgs::init( argc, argv, &aboutData );
	KCmdLineArgs::addCmdLineOptions( options ); // Add our own options.
  //1.53 sec
  KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
  KCmdLineArgs *qtargs = KCmdLineArgs::parsedArgs("qt");
  has_noxft = !args->isSet("xft");
  TEWidget::setAntialias( !has_noxft );

  KApplication a;

  QString term = "";
  if(args->isSet("tn")) {
    term=QString::fromLatin1(args->getOption("tn"));
  }

	QStrList eargs;

  const char* shell = 0;

  if (!args->getOption("e").isEmpty())
  {
	shell = strdup(args->getOption("e"));
	 eargs.append(shell);

     for(int i=0; i < args->count(); i++) {
       eargs.append( args->arg(i) );
	 }
  }
  QString schema = "";
  if (args->isSet("schema"))
    schema = args->getOption("schema");

  //FIXME: more: font

  args->clear();

  ////////////////////////////////////////////////

  // Ignore SIGHUP so that we don't get killed when
  // our parent-shell gets closed.
  signal(SIGHUP, SIG_IGN);

  putenv((char*)"COLORTERM="); // to trigger mc's color detection

  if (a.isRestored() ) {
	//	Only restore with menu
	RESTORE(Klt("/usr/local/bin/menu",eargs,true));
    
  } else {

	if(eargs.isEmpty()) {
		m = new Klt("/usr/local/bin/menu",eargs,false);
	} else {
		m = new Klt("/bin/bash",eargs,false);
	}
    if (!schema.isEmpty()) {
      if (schema.right(7)!=".schema")
        schema+=".schema";
      m->setSchema(schema);
    }

//    ksm->klt = m;
    m->setColLin(80,24); // will use default height and width if called with (0,0)

    m->initFullScreen();
    m->show();
    m->run();
    m->setAutoClose(true);
  }

  int ret = a.exec();

 //// Temporary code, waiting for Qt to do this properly

  // Delete all toplevel widgets that have WDestructiveClose
//  QWidgetList *list = QApplication::topLevelWidgets();
//  QWidgetListIt it(*list);
//  QWidget * w;
//  while( (w=it.current()) != 0 ) {
//     ++it;
//     if ( w->testWFlags( Qt::WDestructiveClose ) )
//          delete w;
//  }

  return ret;
}
