/*
 * src/wimpwm.c - main module
 *
 * Copyright (C) 1999, 2000 Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
 *
 * This 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, or (at your option)
 * any later version.
 * 
 * This software 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 package; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.  
 */

#if HAVE_CONFIG_H
# include <config.h>
#endif 

#if HAVE_GETOPT_H
# include <getopt.h>
#else
#define __EXTENSIONS__
#endif

#include <assert.h>

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>

#include <libsizzle/libsizzle.h>

#include <version.h>

#include "libpath.h"

#include "alloc.h"
#include "wimpwm.h"
#include "events.h"
#include "display.h"
#include "menus.h"
#include "workset.h"

char program_name[] = "wimpwm";

static char version_string[] = __wimpwm_version;

char * program_path = NULL;

char window_menu_title[128] = "Windows";
char application_menu_title[128] = "Applications";
char working_set_menu_title[128] = "Working Sets";
zzz_scm_t application_list = NULL;
zzz_scm_t working_set_list = NULL;

/*
 * Print a usage help message to stderr.
 */
static void 
usage (void)
{
  fprintf (stderr, "usage: %s [OPTION]...\n"
	   "\n"
#if HAVE_GETOPT_H
	   "  -h, --help               display this help and exit\n"
	   "  -v, --version            display version information and exit\n"
	   "  -d, --display=DISPLAY    set display to run on\n"
#else /* not HAVE_GETOPT_H */
	   "  -h           display this help and exit\n"
	   "  -v           display version information and exit\n"
	   "  -d DISPLAY   set display to run on\n"	   
#endif /* not HAVE_GETOPT_H */
	   "\n"
	   "Report bugs to mgrabmue@cs.tu-berlin.de\n",
	   program_name);
}

/*
 * Print version information to stderr.
 */
static void 
version (void)
{
  fprintf (stderr, "%s %s\n", program_name, version_string);
}

/*
 * Print an error message to STDERR.  FMT is a printf-like format string,
 * which describes how the remaining parameters will be formatted.
 */
void
error (char *fmt, ...)
{
  va_list argp;

  fprintf(stderr, "%s: ", program_name);
  va_start(argp, fmt);
  vfprintf(stderr, fmt, argp);
  va_end(argp);
}

int
read_user_init_file (void)
{
  char name_buffer[2048];
  char * p;

  name_buffer[0] = '\0';
  p = getenv("HOME");
  if (p)
    {
      strncpy (name_buffer, p, sizeof (name_buffer));
      name_buffer[sizeof (name_buffer) - 1] = '\0';
      if (name_buffer[strlen (name_buffer) - 1] != '/')
	{
	  strncat (name_buffer, "/", sizeof (name_buffer));
	  name_buffer[sizeof (name_buffer) - 1] = '\0';
	}
    }
  strncat (name_buffer, ".wimpwm", sizeof (name_buffer));
  name_buffer[sizeof (name_buffer) - 1] = '\0';

/*    fprintf (stderr, "init file: %s\n", name_buffer); */
  
  return zzz_evaluate_file (zzz_toplevel_env, name_buffer);
}

static result_t
read_init_files (void)
{
  char name_buffer[2048];

  strncpy (name_buffer, LIBRARY_DIR, sizeof (name_buffer));
  name_buffer[sizeof (name_buffer) - 1] = '\0';
  strncat (name_buffer, "/", sizeof (name_buffer));
  name_buffer[sizeof (name_buffer) - 1] = '\0';
  strncat (name_buffer, "system.wimpwm", sizeof (name_buffer));
  name_buffer[sizeof (name_buffer) - 1] = '\0';
  
/*    fprintf (stderr, "system init file: %s\n", name_buffer); */
  zzz_evaluate_file (zzz_toplevel_env, name_buffer);

  return read_user_init_file ();
}

/*
 * This is the real main program of wimpwm. Parameters ARGC and ARGV
 * are passed in from main().
 */
static int
inner_main (int argc, char * argv[])
{
  argc = argc;
  argv = argv;

  zzz_bind_string_variable ("window-menu-title", window_menu_title,
			    sizeof (window_menu_title), 0);
  zzz_bind_string_variable ("application-menu-title", application_menu_title,
			    sizeof (application_menu_title), 0);
  zzz_bind_string_variable ("working-set-menu-title",
			    working_set_menu_title,
			    sizeof (working_set_menu_title), 0);
  zzz_bind_scm_variable ("application-list", &application_list, 0);
  zzz_bind_scm_variable ("working-set-list", &working_set_list, 0);

  define_display_variables ();

  version ();

/*    fprintf(stderr, "Allocated bytes: %d\n", allocated_bytes); */

  read_init_files ();

/*    fprintf(stderr, "Allocated bytes: %d\n", allocated_bytes); */

  if (initialize_display() == RESULT_SUCCESS)
    {
      init_menus ();
      adopt_windows ();
      setup_worksets ();
      process_events ();
      return 0;
    }
  else
    return 1;
}

/*
 * Parse the command line options, initialize all modules and call
 * the processing stage.  Finally, free all used resources and return 
 * an exit code to the calling process.
 */
int
main (int argc, char * argv[])
{
  int arg;
  int return_val;
#if HAVE_GETOPT_H
  int index;
  static struct option wimpwm_options[] =
  {
    {"help", no_argument, NULL, 'h'},
    {"version", no_argument, NULL, 'v'},
    {"display", required_argument, NULL, 'd'},
    {NULL, 0, NULL, 0}
  };
#endif /* HAVE_GETOPT_H */

  program_path = argv[0];

#if HAVE_GETOPT_H
  while ((arg = getopt_long (argc, argv, "hvd:", wimpwm_options,
			     &index)) != EOF)
#else
  program_path = argv[0];
  while ((arg = getopt (argc, argv, "hvd:")) != EOF)
#endif /* not HAVE_GETOPT_H */
    {
      switch (arg)
	{
	case 'h':
	  usage ();
	  exit (0);
	  break;

	case 'v':
	  version ();
	  exit (0);
	  break;

	case 'd':
	  display_string = optarg;
	  break;

	default:
	  usage ();
	  exit (1);
	}
    }

  if (!display_string)
    {
      display_string = getenv("DISPLAY");
    }

/*    fprintf(stderr, "Allocated bytes: %d\n", allocated_bytes); */

  return_val = zzz_run (inner_main, argc, argv);

/*    fprintf(stderr, "Allocated bytes: %d\n", allocated_bytes); */

  return return_val;
}
