/* copyright 	Maher Awamy <muhri@muhri.net>		*
 *		Charlie Schmidt <ishamael@themes.org>	*
 *							*
 *  released under the GPL				*/

#include "skipstone.h"
#include <sys/param.h>


GSList *window_count = NULL; /* open windows count, starts at 1 logically */
GSList *prefixes = NULL;
GList *combo_urls = NULL;
GNode *bookmarks = NULL;
ConfigData config;
gchar *app_path = NULL;
gint skipstone_fullscreen_mode = 0;
G_CONST_RETURN gchar *home = NULL;
extern void register_ch();

static void skipstone_read_configs(void);
static void skipstone_init_mozembed(void);
static gboolean skipstone_get_single_embed_object(void);



static gboolean skipstone_get_single_embed_object(void)
{
#ifndef WebKit   
     	GtkMozEmbedSingle *single = NULL;
     	D_ENTER;
     	single = gtk_moz_embed_single_get();
     	if (!single) D_RETURN_(FALSE);
     	g_signal_connect(G_OBJECT(single), "new_window_orphan",
			   G_CALLBACK(new_window_orphan_cb),
			   NULL);
#endif   
     	D_RETURN_(TRUE);
}
     	
static void skipstone_init_mozembed(void)
{
#ifndef WebKit
	gchar *profile;
     	D_ENTER;
     	profile = g_strconcat(home,"/.skipstone/mozilla",NULL);
     	gtk_moz_embed_set_profile_path(profile,"skipstone");
     	g_free(profile);
     	gtk_moz_embed_push_startup();
        mozilla_init_chrome ();
        register_ch();
#endif   
     	D_RETURN;
}

static void skipstone_read_configs(void)
{
     	D_ENTER;
     	read_urls();
	read_skipstone_config();
	read_bookmarks();
	read_history();
	read_prefixes();
	D_RETURN;
}

gint
main (gint argc,gchar *argv[]) 
{
	gchar *dir;
	gint crashed=0;
     	D_ENTER;
     	D("Starting SkipStone\n");
#ifndef WebKit   
	if (!getenv("MOZILLA_FIVE_HOME")) { /* avoid mozilla's components crashing */
		g_print(_("MOZILLA_FIVE_HOME is not set !, bailing out .. set it like this\nexport MOZILLA_FIVE_HOME=/path/to/mozilla \nthen retry.\n"));
		return 0;
	}
#endif
     	app_path = argv[0];
     	home = g_get_home_dir();
	dir = g_strconcat(home,"/.skipstone",NULL);
     	/* this is fine, as it will silently error if it already exists*/
     	mkdir(dir,0755);
     	g_free(dir);     	
     	check_running(argc,argv); /* do we have another process - check if its alive */
	set_running();   /* if we are back here means we are starting a new one */
	
	
     	signal(SIGUSR1, handle_usr1_signal); /* connect the signal to launch new windows if running */
	signal(SIGSEGV, handle_segv_signal);/*connect to signal if a SIGSEGV crash happens*/

	skipstone_read_configs();
     
#ifdef ENABLE_NLS
    gtk_set_locale();
#   ifdef PACKAGE
#       ifdef LOCALEDIR
            bindtextdomain(PACKAGE, LOCALEDIR);
#       endif   /* LOCALEDIR */
        textdomain(PACKAGE);
#   else
        textdomain("skipstone");
#   endif   /* PACKAGE */
#endif  /* ENABLE_NLS */

#if 0
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);

	gtk_set_locale();
#endif

	gtk_init(&argc,&argv);
	if((crashed = check_crash_file()) !=0) {
	     g_print(_("Recovering from crash\n"));
	     argc = 0;
	}

     	skipstone_init_mozembed();

     	if (argc > 1) { /* there is an argument, pass it as a loaded url */
	     gchar pathname[MAXPATHLEN];
	     gint i;
		
	     for (i = 1; i < argc; ++i) {		    

		if (!strcmp(argv[i], "--help"))
		    {
		       printf(_("Usage: skipstone -f http://www.someurl.com http://www.someurl2.com, ... \n"));
		       printf(_("use --fullscreen or -f for fullscreen mode at startup\n"));
		       printf(_("--help, this text\n"));
		       exit(0);
		    }

		if (!strcmp(argv[i], "--fullscreen") || !strcmp(argv[i], "-f")) 
			    {
			       skipstone_fullscreen_mode  = 1;
			       i++;

			    }

		     
		     if(realpath(argv[i], pathname) != NULL)
		       make_window(pathname);
		     else 
		       make_window(argv[i]);

		

	     }
	   

	   
 
	
	} else {
	     if (!crashed)
		     make_window(NULL);
	}
	
#ifndef WebKit
     	if (!skipstone_get_single_embed_object())
       		g_error(_("Failed to get single embed object, please check your Embed!"));
#endif
	D("Entering main loop\n");     
	gtk_main();
     	D("Left main loop\n");
	D_RETURN_(0);
}



