/***************************************************************************
                          tdesudo.cpp  -  description
                             -------------------
    begin                : Sam Feb 15 15:42:12 CET 2003
    copyright            : (C) 2003 by Robert Gruber <rgruber@users.sourceforge.net>
                           (C) 2007 by Martin Böhm <martin.bohm@kubuntu.org>
                                       Anthony Mercatante <tonio@kubuntu.org>
                                       Canonical Ltd (Jonathan Riddell <jriddell@ubuntu.com>)
                           (C) 2011 by Timothy Pearson <kb9vqf@pearsoncomputing.net>

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

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <cstdlib>

#include <tdecmdlineargs.h>
#include <tdeaboutdata.h>
#include <tdelocale.h>

#include "tdesudo.h"
#include <tdemessagebox.h>
#include <kdesktopfile.h>
#include <kiconloader.h>
#include <kicontheme.h>
#include <tdeglobal.h>
#include <kstandarddirs.h>
#include <kprocess.h>
#include <kdebug.h>
#include <tqfile.h>
#include <tqfileinfo.h>

static const char *description =
	I18N_NOOP("TdeSudo");
// INSERT A DESCRIPTION FOR YOUR APPLICATION HERE


static TDECmdLineOptions options[] =
{
	{ "u <runas>", I18N_NOOP("sets a runas user"), 0 },
	{ "c <command>", I18N_NOOP("The command to execute"), 0 },
	{ "s", I18N_NOOP("Forget passwords"), 0 },
	{ "i <icon name>", I18N_NOOP("Specify icon to use in the password dialog"), 0},
	{ "d", I18N_NOOP("Do not show the command to be run in the dialog"), 0},
	{ "p <priority>", I18N_NOOP("Process priority, between 0 and 100, 0 the lowest [50]"), 0},
	{ "r", I18N_NOOP("Use realtime scheduling"), 0},
	{ "f <file>", I18N_NOOP("Use target UID if <file> is not writeable"), 0},
	{ "t", I18N_NOOP("Fake option for TDE's TdeSu compatibility"), 0 },
	{ "n", I18N_NOOP("Do not keep password"), 0},
	{ "nonewdcop", I18N_NOOP("Use existing DCOP server"), 0},
	{ "comment <dialog text>", I18N_NOOP("The comment that should be displayed in the dialog"),  0},
	{ "noignorebutton", I18N_NOOP("Do not display « ignore » button"), 0 },
	{ "+command", I18N_NOOP("The command to execute"), 0 },
	TDECmdLineLastOption
};

int main(int argc, char **argv)
{
	TDEAboutData aboutData("tdesudo", I18N_NOOP("TdeSudo"),
		VERSION, description, TDEAboutData::License_GPL,
		"(c) 2007-2008, Anthony Mercatante", 0, 0, "tonio@ubuntu.com");
	aboutData.addAuthor("Robert Gruber",0, "rgruber@users.sourceforge.net");
	aboutData.addAuthor("Anthony Mercatante",0, "tonio@ubuntu.com");
	TDECmdLineArgs::init(argc, argv, &aboutData);
	TDECmdLineArgs::addCmdLineOptions(options); // Add our own options.

	TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();

	TDEApplication a;

	TQString executable;
	TQStringList executableList;
	TQString arg;
	TQString command;
	TQStringList commandlist;
	TQString icon;

	bool withIgnoreButton = args->isSet("ignorebutton");

	if (args->isSet("c")) {
		executable = args->getOption("c");
	}
	else {
		if (args->count()) {
			command = args->arg(0);
			commandlist = TQStringList::split(TQChar(' '), command);
			executable = commandlist[0];
		}
	}

	if (executable.stripWhiteSpace().isEmpty()) {
		kdError() << I18N_NOOP("You must provide the name of the executable you want to run as an argument to tdesudo") << endl;
		exit(1);
	}

	/* We have to make sure the executable is only the binary name */
	executableList = TQStringList::split(" ", executable);
	executable = executableList[0];

	executableList = TQStringList::split("/", executable);
	int i = executableList.count() - 1;
	executable = executableList[i];

	/* Kubuntu has a bug in it - this is a workaround for it */
	TDEGlobal::dirs()->addResourceDir("apps","/usr/share/applications/kde");
	TDEGlobal::dirs()->addResourceDir("apps","/usr/share/applications");

	TQString deskFilePath = TDEGlobal::dirs()->findResource("apps",executable + ".desktop");
	KDesktopFile desktopFile(deskFilePath,true);

	/* icon parsing */
	if (args->isSet("i"))
		icon = args->getOption("i");
	else
	{
		TQString iconName = desktopFile.readIcon();
		icon = TDEGlobal::iconLoader()->iconPath(iconName, -1* TDEIcon::StdSizes(TDEIcon::SizeHuge), true);
	}

	/* generic name parsing */
	TQString name = desktopFile.readName();
	TQString genericName = desktopFile.readGenericName();

	if (!name.isEmpty())
	{
		if (!genericName.isEmpty())
		name += " - " + genericName;
	}
	else if (!genericName.isEmpty())
		name = genericName;
	else
		name = executable;

	TdeSudo *tdesudo = new TdeSudo(0,0,icon,name,withIgnoreButton);

	a.setMainWidget(tdesudo);
	args->clear();

	return a.exec();
}
