/* 
 * This file is part of DesktopBSD, see the README file.
 *
 * Author: Peter Hofer <ph@desktopbsd.net>
 *         (C) 2004, 2005
 *
 * Copyright: BSD, see the COPYING file included in this distribution
 *
 */

#include "wndmain.h"

#include <err.h>

#include <dbsdglobal.h>
#include <dbsdkiconloader.h>
#include <translation.h>
#include <variables.h>

#include <kuniqueapplication.h>
#include <kcmdlineargs.h>
#include <kaboutdata.h>

int main(int argc, char *argv[]) {
   /* Not root - run using kdesu */

   if(geteuid() != 0) {
      char *args[argc + 4];
      
      int n = 0;
      args[n++] = "kdesu";
      args[n++] = "--";

      int i;
      for(i = 0; i < argc; ++i)
        args[n++] = argv[i];

      args[n++] = "--nofork";
      args[n] = NULL;
   
      execv("/usr/local/bin/kdesu", args);
      
      // Shouldn't be reached
      err(1, "Couldn't execute kdesu");
   }

	KAboutData  about("dbsd-pkgmgr", "DesktopBSD Package Manager",
							"", NULL, KAboutData::License_BSD);

	KCmdLineArgs::init(argc, argv, &about);
	KUniqueApplication::addCmdLineOptions();

   	if(!KUniqueApplication::start()) {
   		errx(2, "The package manager is already running.");
		return 2;
	}

	KUniqueApplication a;

	char *user = getenv("KDESU_USER");
	Variables::setValue("pkgmgr.user", ((user != NULL) ? user : "root"));

	DBSDGlobal::setIconLoader(new DBSDKIconLoader);
	Translation::installTranslator();
	
	wndMain w;
	w.initModule();

	a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
	return a.exec();
}
