/*
 *  Login.app
 * 
 *  Copyright (c) 1997-2000 Per Liden
 * 
 *  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.
 *
 *  This program 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 
 *  USA.
 */

#include <stdio.h>
#include <unistd.h>
#include "panel.h"
#include "preferences.h"
#include "version.h"
#include "util.h"
#include "config.h"

static void startServer(char* loginAppCommand, Preferences* pref) {
   char** param;
   int    serverArgs;
   int    i;

   for(serverArgs = 0; pref->xserver[serverArgs] != NULL; serverArgs++);

   param = alloc(sizeof(char*) * (5 + serverArgs));
   param[0] = XINIT;
   param[1] = loginAppCommand;
   param[2] = "--client";
   param[3] = "--";
   for(i = 0; pref->xserver[i] != NULL; i++) {
      param[4 + i] = pref->xserver[i];
   }
   param[4 + serverArgs] = 0;

   execvp(param[0], param);

   printError("X server could not be started");
   exit(0);
}

int main(int argc, char** argv) {
   int client = 0;

   /* Check if program is run as root */
/*    if (getuid() != 0) {
      printError("only root can run this program");
      exit(0);
      }
*/
   /* Parse command line arguments */
   if (argc > 1) {
      int i;
      for(i = 1; i < argc; i++) {
         if (!strcmp(argv[i], "--checkconfig") || !strcmp(argv[i], "-c")) {
            checkConfiguration(argv[0]);
            exit(0);
         } else if (!strcmp(argv[i], "--client")) {
            client = 1;
         } else if (!strcmp(argv[i], "--version") || !strcmp(argv[i], "-v")) {
            printf("Login.app version %s\n", VERSION);
            exit(0);
         } else {
            printf("Login.app version %s, Copyright (c) 1997-2000 Per Liden\n\n", VERSION);
            puts("options:");
            puts("    -c, --checkconfig         Parse configuration and show result");
            puts("    -v, --version             Print version and exit");
            exit(0);
         }
      }
   }
    
   if (client) {
      startPanel(getApplicationPath(argv[0]), readConfiguration(argv[0]));
   } else {
      startServer(argv[0], readConfiguration(argv[0]));
   }

   return 0;
}
