/*==========================================================================*\
 |  This file is part of setiherder (http://www.sackheads.org/setiherder/)  |
 |  which was written by Brian L. Naylor <brian+seti@sackheads.org> and is  |
 |  under the GNU General Public License.  You should have recieved a copy  |
 |  of the GPL with this source code.  If not, we'd like to know about it.  |
\*--------------------------------------------------------------------------*/

#include <stdio.h>
#include <gtk/gtk.h>  

#include "shtypes.h"
#include "shproc.h"
#include "shwin.h"
#include "shclients.h"
#include "shutil.h"
#include "shrc.h"

/*------------------------------------------------------------------------*\
 | GLOBALS                                                                |
\*------------------------------------------------------------------------*/
int         total_complete=0,
            session_complete=0,
            setdbgflags = DBG_DEFAULT_FLAGS,
            gtk_initialized = 0;
float       total_cpu=0;

int        clist_host_width,      /* some widget sizings that are         */
           clist_account_width,   /* user-configurable                    */
           clist_state_width,
           clist_units_width,
           clist_complete_width,
           main_window_width,
           main_window_height;

char *sh_state_list[] = { 
  "Stopped",
  "Working",
  "Transferring",
  "Crashed",
  "Not responding",
  "Waiting"
};

void print_usage(char c)
{
  printf("Usage: setiherder [-g]\n");
  printf("          -g : turn on debug diagnostics\n");
  printf("\n(that's it for now :)\n\n");
}

int main(int argc, char *argv[])
{
  sh_rc rc;
  int   i;
  extern GtkWidget *clist;

  /* initialize globals */
  sh_client_idx = -1;

  clist_host_width     = SHWIN_DEFAULT_HOST_WIDTH;
  clist_account_width  = SHWIN_DEFAULT_ACCOUNT_WIDTH;
  clist_state_width    = SHWIN_DEFAULT_STATE_WIDTH;
  clist_units_width    = SHWIN_DEFAULT_UNITS_WIDTH;
  clist_complete_width = SHWIN_DEFAULT_COMPLETE_WIDTH;
  main_window_width    = SHWIN_DEFAULT_MAIN_WINDOW_WIDTH;
  main_window_height   = SHWIN_DEFAULT_MAIN_WINDOW_HEIGHT;
  
  clist = NULL;
  
  /* 
   * initialize gtk 
   */
  gtk_init(&argc, &argv); 

  /* 
   * parse remaining arguments 
   */
  for(i=1; i<argc; i++)
  {
    /* XXX learn to use getopt! :) XXX */
    if(argv[i][0] == '-')
    {
      switch(argv[i][1]) {
        case 'g': 
          setdbgflags = (setdbgflags | DBG_DIAG);
          break;
        /* 
         * XXX
         * -f <file> == alternate rc file
         * -n        == skip rc file entirely
         * XXX
         */
        default:
          print_usage(argv[i][1]);
          break;
      };
    }
    else
    {
      print_usage(argv[i][1]);
    }
  }
  
  /* 
   * read configuration file
   */
  shrc_init();
  shrc_read(/* XXX */ NULL /* XXX */);

  /*
   * initialize process control stuff
   */
  if((rc = shproc_init()) != SH_SUCCESS)
  {
    dbg(DBG_FATAL, 
        "%s: unable to initialize process control gorp.  (rc=%d)\n", 
        argv[0], rc);
    return rc;
  }
  
  /* 
   * initialize widgets 
   */
  if((rc = shwin_init()) != SH_SUCCESS) 
  {
    dbg(DBG_FATAL, "%s: unable to initialize gtk components.  (rc=%d)\n", 
        argv[0], rc);
    return rc;
  }

  gtk_initialized = 1;
  
  gtk_timeout_add(5000,  /* every 5 seconds */
                  idle_check,
                  NULL);

  gtk_timeout_add(10000, /* every 10 seconds */
                  shproc_restart_check,
                  NULL);
  
  /*
   * go
   */
  gtk_main(); 

  return 0;
}

