/*
 *
 * netboard-network-dummy  --  network backend for netboard
 *
 * Copyright (C) 2006-2007 Josef Kufner <jk@myserver.cz>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <stdio.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#include "main.h"	/* includes loudmouth and glib */
#include "callbacks.h"

/* global vars */
gint verbose = D_NORMAL;
gint verbose_flag = FALSE, quiet_flag = FALSE, debug_flag = FALSE;
gchar *show_config_dialog = NULL, *profile = NULL, *address = NULL;

GMainContext *main_context = NULL;
GMainLoop *main_loop;

gchar start_time[32];

gchar *verbose_level_str[D__MAX + 1] = { 
	"error", "normal", "verbose", "debug", NULL
};

const char *dummy_name = "dummy";
const char *dummy_real_name = "Dummy";

/* local vars */
static gboolean test_success = FALSE;


static GIOChannel *stdin_channel;
static unsigned int stdin_channel_tag;

static GOptionEntry entries[] = {
	{"configure", 'C', 0, G_OPTION_ARG_STRING, &show_config_dialog,
	 "Show configuration dialog only", NULL},
	{"profile", 'P', 0, G_OPTION_ARG_STRING, &profile,
	 "Load configuration from Net Board 2 options system", NULL},
	{"room", 'A', 0, G_OPTION_ARG_STRING, &address,
	 "Chat room to send the message to (e.g. user@server.org)", NULL},
	{"quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet_flag,
	 "Be quiet", NULL},
	{"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose_flag,
	 "Be verbose", NULL},
	{"debug", 'd', 0, G_OPTION_ARG_NONE, &debug_flag,
	 "Be more verbose -- debuging info", NULL},
	{NULL}
};


static void send_configuration_dialog (void)
{
	MESSAGE *msg;

	/* send dialog */
	msg = message_create(0, 0);
	message_append(msg, MESSAGE_PART_DATA, MESSAGE_STRING, "Odo",
			MESSAGE_STRING, "network",
			MESSAGE_STRING, show_config_dialog,
			MESSAGE_STRING, PROG_NAME,
			MESSAGE_STRING, "=Není co nastavovat.",
			MESSAGE_END);
	message_to_file(msg, stdout, 1);
	message_destroy(msg);
}


int main(int argc, char **argv)
{
	GMainContext *main_context = NULL;
	GOptionContext *context = NULL;
	MESSAGE *msg;
	int i;

	setlocale(LC_ALL, "");
	message_init();

	fprintf(stderr, "Command line:");
	for (i = 0; argv[i]; i++) {
		fprintf(stderr, " %s", argv[i]);
	}
	fprintf(stderr, "\n");

	/* parse cmd line options */
	context = g_option_context_new("- Jabber backend for Netboard 2");
	g_option_context_add_main_entries(context, entries, NULL);
	g_option_context_parse(context, &argc, &argv, NULL);
	g_option_context_free(context);

	/* config dialog */
	if (show_config_dialog) {
		send_configuration_dialog();
		return 0;
	}

	print_msg(D_VERBOSE, "Verbose level: %s (%d)", verbose_level_str[verbose], verbose);

	/* initialize main loop */
	main_context = g_main_context_default();
	
	/* initialize stdin channel */
	stdin_channel = g_io_channel_unix_new(0);
	g_io_channel_set_encoding(stdin_channel, NULL, NULL);
	print_msg(D_DEBUG, "stdin encoding: %s", g_io_channel_get_encoding(stdin_channel));
	g_io_channel_set_flags(stdin_channel, G_IO_FLAG_NONBLOCK | g_io_channel_get_flags(stdin_channel), NULL);
	stdin_channel_tag = g_io_add_watch(stdin_channel, G_IO_IN | G_IO_HUP | G_IO_ERR, stdin_data_cb, NULL);

	/* little delay */
	sleep(1);
	
	/* send "online" */
	msg = message_create(0, 0);
	message_append(msg, MESSAGE_PART_DATA, MESSAGE_STRING, "Son", MESSAGE_STRING, dummy_name, MESSAGE_END);
	message_to_file(msg, stdout, 1);
	message_destroy(msg);

	/* send dummy presence */
	msg = message_create(0, 0);
	message_append(msg, MESSAGE_PART_DATA, MESSAGE_STRING, "Pj", MESSAGE_STRING, dummy_name, MESSAGE_END);
	message_to_file(msg, stdout, 1);
	message_destroy(msg);
	
	/* send dummy info */
	msg = message_create(0, 0);
	message_append(msg, MESSAGE_PART_DATA, MESSAGE_STRING, "Pn", MESSAGE_STRING, dummy_name, MESSAGE_STRING, dummy_real_name, MESSAGE_END);
	message_to_file(msg, stdout, 1);
	message_destroy(msg);

	/* run main loop */
	main_loop = g_main_loop_new(main_context, FALSE);
	g_main_loop_run(main_loop);

	/* say bye */
	print_msg(D_NORMAL, "Bye.");
	return (test_success ? EXIT_SUCCESS : EXIT_FAILURE);
}

/* vim:set encoding=utf8: */
