/*
 * EveryBuddy 
 *
 * Copyright (C) 1999, Torrey Searle <tsearle@uci.edu>
 *
 * This program 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 of the License, or
 * (at your option) any later version.
 *
 * This program 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 program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*
 * main.c
 * yipee
 */
#ifndef USE_APPLET
#include <gtk/gtk.h>
#else
#include <applet-widget.h>
#include "pixmaps/ebicon.xpm"
#endif

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#if defined( _WIN32 )
#include <direct.h>
#define mkdir( x, y )	_mkdir( x )
#else
#include <unistd.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include "status.h"
#include "service.h"
#include "globals.h"
#include "account.h"
#include "dialog.h"
#include "util.h"
#include "libproxy/libproxy.h"
#include "console_session.h"

void eb_show_contact_list() { }

#ifdef USE_APPLET
	GtkWidget * applet;
	GtkWidget * applet_pixmap;

static void hide_window(GtkWidget * widget, GdkEvent * event, gpointer d)
{
	static int hidden = 0;
	printf("event\n");
	if(event->button.type == GDK_2BUTTON_PRESS && event->button.button == 1 && statuswindow)
	{
		if(hidden)
		{
			gtk_widget_show(statuswindow);
			hidden = 0;
		}
		else
		{
			gtk_widget_hide(statuswindow);
			hidden = 1;
		}
	}
}
#endif

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

    int c = 0;
	int sock;
	int len;
	struct sockaddr_un local;
	char message[4096] = "";
	char contact[256] = "";
    gchar buff[1024];
#ifdef USE_APPLET
	GdkPixmap * icon_pm;
	GdkPixmap * icon_bm;
#endif

	
    geometry[0] = 0;
#ifndef USE_APPLET
   	gtk_init(&argc, &argv);
#else

	#define PACKAGE "everybuddy"
	applet_widget_init(PACKAGE, VERSION, argc, argv, NULL, 0, NULL);
#endif

#ifdef HAVE_GETOPT_H
    while (1) 
	{
		static struct option long_options[] = 
		{
	    	{"version", no_argument, NULL, 'v'},
		{"help", no_argument, NULL, 'h'},
	    	{"geometry", required_argument, NULL, 'G'},
	    	{"activate-goad-server", required_argument, NULL, 'a'},
	    	{"contact", required_argument, NULL, 'c'},
	    	{"message", required_argument, NULL, 'm'},
	    	{0, 0, 0, 0}
		};
    
		int option_index = 0;

		/* Detect the end of the options. */

		if (c == -1)
		{
			break;
		}

		c = getopt_long(argc, argv, "vhGa:Dcm", long_options, &option_index);

		switch (c) 
		{
		    case 'v':           /* version information */
		
			printf("EveryBuddy v%s\n", VERSION); 
			printf("Copyright (C) 1999 Torrey Searle\n");
			printf("EveryBuddy comes with NO WARRANTY, to the extent permitted by law.\n");
			printf("You may redistribute copies of EveryBuddy under the terms of the\n");
			printf("GNU General Public License.  For more information about these\n");
			printf("matters, see the file named COPYING.\n");
			printf("\nEveryBuddy is brought to you by: Torrey Searle, \n");
			printf("Jared Peterson, Ben Rigas, Jeff Miller, and Troy Morrison.\n");
			printf("\nFor more information on EveryBuddy, visit the website at\n");
			printf("         http://www.everybuddy.com/\n");
			exit(0);
		    
		    case 'h':           /* help information */
		    case '?':
			printf("Usage: everybuddy [OPTION]...\n");
			printf("An integrated instant messenger program.\n");
			printf("Supports AOL Instant Messenger, ICQ, and Yahoo! Messenger.\n\n");
			printf("  -g, --geometry             Set position of main event window\n");
			printf("      --help                 display this help and exit\n");
			printf("      --version              output version information and exit\n");
			printf("\nReport bugs to tsearle@uci.edu or\n");
			printf("http://www.akropolys.com/cgi-bin/eb-bugs\n");
			exit(0);

		    case 'G':
			strcpy(geometry, optarg);
			break;


		    case ':':
			printf("Try 'everybuddy --help' for more information.\n");
			exit(0);

			case 'a':
			printf("hi\n");
			break;

			case 'c':
			strcpy(contact, optarg);
			break;

			case 'm':
			strcpy(message, optarg);
			break;


		}
    } 

#endif

	if(strlen(message) > 0 && strlen(contact) > 0)
	{
		struct sockaddr_un remote;
		int sock;
		short length;
		int ret;

		sock = socket(AF_UNIX, SOCK_STREAM, 0);
		strcpy(remote.sun_path, getenv("HOME"));
		strcat(remote.sun_path, "/.everybuddy/eb_socket");
		remote.sun_family = AF_UNIX;
		len = strlen(remote.sun_path) + sizeof(remote.sun_family);
		if(connect(sock, (struct sockaddr*)&remote, len) == -1 )
		{
			perror("connect");
			exit(1);
		}
		length = strlen(contact)+1;
		write(sock, &length, sizeof(short));
		write(sock, contact, length);
		length = strlen(message)+1;
		write(sock, &length, sizeof(short));
		write(sock, message, length);
		read(sock, &ret, sizeof(int));
		close(sock);
		exit(ret);
	}
	g_snprintf(buff, 1024, "%s/.everybuddy",getenv("HOME"));
	services_init();
		
	mkdir(buff, 0700);
	g_snprintf(buff, 1024, "%s/.everybuddy/logs",getenv("HOME"));
	mkdir(buff, 0700);
	g_snprintf(buff, 1024, "%s/.everybuddy/files",getenv("HOME"));
	mkdir(buff, 0700);

	sock = socket(AF_UNIX, SOCK_STREAM, 0);
	strcpy(local.sun_path, getenv("HOME"));
	strcat(local.sun_path, "/.everybuddy/eb_socket");
	unlink(local.sun_path);
	local.sun_family = AF_UNIX;
	len = strlen(local.sun_path) + sizeof(local.sun_family);
	if(bind(sock, (struct sockaddr *)&local, len) == -1)
	{
		perror("bind");
		exit(1);
	}
	if(listen(sock, 5) == -1)
	{
		perror("listen");
		exit(1);
	}
	gdk_input_add(sock, GDK_INPUT_READ, console_session_init, NULL);

	//proxy_set_proxy( PROXY_NONE, "", 0 );
	//proxy_set_proxy(PROXY_SOCKS5,"24.0.201.225",1080);

	system("update.pl");
	system("contact-update.pl");
	load_contacts();
	eb_read_prefs();
    if (!load_accounts())
    	eb_new_user();
    else
	{
        eb_show_contact_list();
		eb_status_window();
	}

#ifndef USE_APPLET
    gtk_main();
#else
	applet = applet_widget_new(PACKAGE);
	gtk_widget_set_events( applet, gtk_widget_get_events(applet) |
			               GDK_BUTTON_PRESS_MASK | GDK_EXPOSURE_MASK );
	gtk_signal_connect(GTK_OBJECT(applet),"button_press_event", GTK_SIGNAL_FUNC(hide_window), NULL);
	gtk_widget_realize(applet);
	icon_pm = gdk_pixmap_create_from_xpm_d(applet->window, &icon_bm,
			                          NULL, (gchar **)ebicon);
	applet_pixmap = gtk_pixmap_new(icon_pm, icon_bm);
	applet_widget_add(APPLET_WIDGET(applet), applet_pixmap);
	gtk_widget_show(applet_pixmap);
	gtk_widget_show(applet);
	
	applet_widget_gtk_main();
#endif
		
	return 0;

}
