/*
 * map.c
 * (C) Christian Magnusson 2000, Email: d1mag@dtek.chalmers.se
 * This program is copyright, and covered by the Gnu Public License.
 * The Q bot.
 */

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

#include "settings.h"
#include "bottypes.h"
#include "globals.h"
#include "dl.h"

void m_map(char *a);

MODULE_PUBLIC int _init_map(void *a, void *b)
{
  dl_command_entry_t *dl = a;
  if(!a) return 0;

  snprintf(dl->help, MAX_COMMAND_HELP_LEN,
	   "/msg %s MAP\nShow a map over the servers and show how they are connected to eachother.\n", BOT_NAME);
  snprintf(dl->summary, MAX_COMMAND_SUMMARY_LEN,
	   "Show a map over the servers");
  dl->level = 950;
#if 0
  dl->func = m_map;
#else
  dl->func = (void (*)(char*)) DL_sym(dl->val, "m_map");
#endif
  return 1;
}

MODULE_PUBLIC int _remove_map(char *a, char *b)
{
  remove_command("map");
  return 1;
}

/*
  This must be rewritten... it's rather buggy and won't print a correct
  tree structure. I just haven't had time to fix it yet.
*/
void get_server_depth(int my_main_server, int depth, int last_main_server)
{
  char tmp[256];
  int my_main_server_yxx = servers[my_main_server].yxx[0]*64+servers[my_main_server].yxx[1];
  int i, j, pos;
  int nr = 0;
  int tot = 0;
  for(i=0; i<MAX_SERVERS; i++) {
    if(servers[i].connected && (servers[i].parent_server[0]*64+servers[i].parent_server[1]==my_main_server_yxx)){
      /* found the main-server in the list */
      tot++;
    }
  }
  if(tot==0) return;

  for(i=0; i<MAX_SERVERS; i++) {
    if(servers[i].connected && (servers[i].parent_server[0]*64+servers[i].parent_server[1]==my_main_server_yxx)){
      nr++;
      pos = 0;

      if(nr == tot) {
	if(depth == 0) {
	  pos += sprintf(&tmp[pos], "`-");
	} else {
	  if(!last_main_server) {
	    pos += sprintf(&tmp[pos], "|");
	    for(j=0; j<depth/2-1; j++) pos += sprintf(&tmp[pos], "  ");
	    pos += sprintf(&tmp[pos], " `-");
	  } else {
	    for(j=0; j<depth/2; j++) pos += sprintf(&tmp[pos], "  ");
	    pos += sprintf(&tmp[pos], "`-");
	  }
	}
	pos += sprintf(&tmp[pos], servers[i].name);
	say_sender(tmp);
	if(depth == 0)
	  get_server_depth(i, depth+2, (nr == tot));
	else
	  get_server_depth(i, depth+2, last_main_server);
      } else {
	if(depth == 0) {
	  pos += sprintf(&tmp[pos], "|-");
	} else {
	  if(!last_main_server) {
	    pos += sprintf(&tmp[pos], "|");
	    for(j=0; j<depth/2; j++) pos += sprintf(&tmp[pos], " ");
	    pos += sprintf(&tmp[pos], "|-");
	  } else {
	    for(j=0; j<depth/2 + 1; j++) pos += sprintf(&tmp[pos], " ");
	    pos += sprintf(&tmp[pos], "|-");
	  }
	}
	pos += sprintf(&tmp[pos], servers[i].name);
	say_sender(tmp);
	if(depth == 0)
	  get_server_depth(i, depth+2, (nr == tot));
	else
	  get_server_depth(i, depth+2, last_main_server);
      }
    }
  }
}

MODULE_PUBLIC void m_map(char *a)
{
  int i;
  struct dbaseuser *db;
  db = sender_au->dbuser;
  if(db) {
    log("From: %s map\n", db->nick);
  } else
    return;
  if(!is_oper()) {
    say_sender("You are not an Oper.");
    log("To: %s Is not an Oper\n", db->nick);
    return;
  }

  for(i=0; i<MAX_SERVERS; i++) {
    if(servers[i].connected && (servers[i].parent_server==0)) {
      /* found the main-server in the list */
      break;
    }
  }
  say_sender(servers[i].name);
  get_server_depth(i, 0, 0);
  return;
}
