#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <gtk/gtk.h>

#define NUM_NODES 2

static GtkWidget *win = NULL;
static GtkWidget *list = NULL;

static int updatefreq = 2000;    

#define IDX_ID 0
#define IDX_HOSTNAME 1
#define IDX_DESC 2
#define IDX_ARCH 3
#define IDX_MHZ 4
#define IDX_BOGO 5
#define IDX_L1 6
#define IDX_L5 7
#define IDX_L15 8
#define IDX_UPTIME 9
#define IDX_IDLE 10
#define IDX_OS 11
#define IDX_OSREL 12
#define IDX_NUM 13

static gchar *titles[] = { "ID", 
			   "Hostname",
			   "Description",
			   "Arch",
			   "MHz",
			   "BogoMIPS",
			   "L 1 min",
			   "L 5 min",
			   "L 15 min",
			   "Uptime",
			   "Idle",
			   "OS",
			   "OS-Rel",
			   NULL };

static gchar *nodes[NUM_NODES][IDX_NUM];

static gchar *defnode[] = { "----",
			    "-------",
			    "-------",
			    "-------",
			    "-------",
			    "-------",
			    "-------",
			    "-------",
			    "-------",
			    "-------",
			    "-------",
			    "-------",
			    "-------",
			    NULL
};

static void init_nodes(void)
{
  int i, j;
  
  for(i = 0; i < NUM_NODES; i++)
    for(j = 0; j < IDX_NUM; j++) 
    nodes[i][j] = strdup(defnode[j]);
}

static int get_node_info(gchar **node)
{
  char fn[255];
  int fd, rc, i;
  char rbuf[8192], *q, *p;

  strcpy(fn, "/usr/nodes/status/");
  strcat(fn, *node);
  strcat(fn, ".stat");

  fd = open("fn", O_RDONLY);
  if(fd == -1)
    return 1;
  rc = read(fd, rbuf, 8192);
  if(rc ==  -1) {
    close(fd);
    return 1;
  }
  p = rbuf;
  for(i = 0; i < IDX_NUM; i++) {
    q = strchr(p, '\n');
    if(!q)
      break;
    *q = 0;
    free(node[i]);
    node[i] = strdup(p);
    p = q + 1;
  }
  return 0;
}

static void update(void)
{
  int i;

  gtk_clist_freeze(GTK_CLIST(list));
  gtk_clist_unselect_all(GTK_CLIST(list));
  gtk_clist_clear(GTK_CLIST(list));
  
  for(i = 0; i < NUM_NODES; i++) {
    get_node_info(nodes[i]);
    gtk_clist_append(GTK_CLIST(list), nodes[i]);
  }
  gtk_clist_columns_autosize(GTK_CLIST(list));
  gtk_clist_thaw(GTK_CLIST(list));
}

static void destroy (void)
{
  gtk_exit (0);
}

gint myidle(gpointer *data);

void add_idle(gpointer *p)
{
#if 0
  gtk_idle_add_priority(G_PRIORITY_LOW, myidle, p);
#else
  gtk_timeout_add(updatefreq, myidle, p);
#endif

  return;
}


gint myidle(gpointer *data)
{
    fprintf(stderr, ".");
    update();
  
  add_idle(NULL);

  return 0;
}



int main(int argc, char **argv)
{

  gtk_init(&argc, &argv);
  init_nodes();
  
  win =  gtk_window_new (GTK_WINDOW_TOPLEVEL); 
  gtk_signal_connect (GTK_OBJECT (win), "destroy",
		      GTK_SIGNAL_FUNC (destroy), NULL);
  gtk_window_set_title(GTK_WINDOW(win), "Quant-X/Samsung Node statistics");

  gtk_window_set_default_size(GTK_WINDOW(win), 1000, 760);
  list = gtk_clist_new_with_titles (IDX_NUM,  titles);
  gtk_container_add (GTK_CONTAINER (win), list); 
  gtk_clist_set_shadow_type(GTK_CLIST(list), GTK_SHADOW_IN);
  gtk_clist_set_row_height(GTK_CLIST(list), 22);
 gtk_widget_show(list);
  gtk_widget_show (win); 

  update();
  add_idle(NULL);

  gtk_main(); 
  
  return 0;
}



