/*
 * BadWM - minimalistic window manager for the X Window System
 * Copyright (C) Robert Annessi <robert@annessi.at>
 *
 * 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.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <include/BadWM.h>
#include <include/config.h>

char **set_args(char *args) {
	char **ret;
	char *buf, *tmp;
	int argc=1;

	ret = malloc(sizeof(*ret) * (argc + 1));
	ret[argc - 1] = strdup(config.global->term);
	if(args == NULL)
	{
		ret[argc] = NULL;
		return ret;
	}

	#ifdef DEBUG
		bad_debug(4, "Terminal arguments to split: %s\n", args);
	#endif
	buf = strdup(args);
	tmp = strtok(buf, " ");

	while(1)
	{
		if (!tmp)
			break;
		argc++;
		ret = realloc(ret, sizeof(*ret) * (argc + 1));
		ret[argc - 1] = strdup(tmp);
		#ifdef DEBUG
			bad_debug(5, "New argument found for terminal: %s\n", tmp);
		#endif
		tmp = strtok(NULL, " ");
	}

	ret[argc] = NULL;
	return ret;
}

void initstr(void)
{
        config.global = (struct ConfigGlobals *) calloc(1, sizeof(struct ConfigGlobals));
        config.keys = (struct ConfigKeys *) calloc(1, sizeof(struct ConfigKeys));
        config.theme = (struct ConfigTheme *) calloc(1, sizeof(struct ConfigTheme));

}

void setconf(void) {
	/* setting defaults */
	if (!config.global->debug) config.global->debug = 0;
	config.global->term = strdup("xterm");
	config.global->term_args = NULL;
	config.global->max_vdesks = 10;
        config.global->snap_distance = 10;
        config.global->flip_resizelower = 0;
	config.global->solidwindows = 0;
	config.keys->next = strdup("Tab");
	config.keys->term = strdup("Return");
	config.keys->topleft = strdup("t");
	config.keys->topright = strdup("y");
	config.keys->bottomleft = strdup("g");
	config.keys->bottomright = strdup("h");
	config.keys->lower = strdup("Down");
	config.keys->max = strdup("x");
	config.keys->maxhoriz = strdup("c");
	config.keys->maxvert = strdup("v");
	config.keys->fix = strdup("f");
	config.keys->kill = strdup("k");
	config.keys->reload = strdup("r");
	config.keys->vddown = strdup("Left");
	config.keys->vdup = strdup("Right");
        config.theme->bgcolor = strdup("gray50");
        config.theme->fgcolor = strdup("goldenrod");
        config.theme->fxcolor = strdup("blue");
	#ifdef DEBUG
		bad_debug(1,"setting default global term = %s\n", config.global->term);
		bad_debug(1,"setting default global term_args = %s\n", config.global->term_args);
		bad_debug(1,"setting default global max_vdesks = %d\n", config.global->max_vdesks);
		bad_debug(1,"setting default global snap_distance = %d\n", config.global->snap_distance);
		bad_debug(1,"setting default global flip_resizelower = %d\n", config.global->flip_resizelower);
		bad_debug(1,"setting default global solidwindows = %d\n", config.global->solidwindows);
		bad_debug(1,"setting default key next = %s\n", config.keys->next);
		bad_debug(1,"setting default key term = %s\n", config.keys->term);
		bad_debug(1,"setting default key topleft = %s\n", config.keys->topleft);
		bad_debug(1,"setting default key topright = %s\n", config.keys->topright);
		bad_debug(1,"setting default key bottomleft = %s\n", config.keys->bottomleft);
		bad_debug(1,"setting default key bottomright = %s\n", config.keys->bottomright);
		bad_debug(1,"setting default key lower = %s\n", config.keys->lower);
		bad_debug(1,"setting default key max = %s\n", config.keys->max);
		bad_debug(1,"setting default key maxhoriz = %s\n", config.keys->maxhoriz);
		bad_debug(1,"setting default key maxvert = %s\n", config.keys->maxvert);
		bad_debug(1,"setting default key fix = %s\n", config.keys->fix);
		bad_debug(1,"setting default key kill = %s\n", config.keys->kill);
		bad_debug(1,"setting default key reload = %s\n", config.keys->reload);
		bad_debug(1,"setting default key vddown = %s\n", config.keys->vddown);
		bad_debug(1,"setting default key vdup = %s\n", config.keys->vdup);
		bad_debug(1,"setting default theme bgcolor = %s\n", config.theme->bgcolor);
		bad_debug(1,"setting default theme fgcolor = %s\n", config.theme->fgcolor);
		bad_debug(1,"setting default theme fxcolor = %s\n", config.theme->fxcolor);
	#endif

	getconf();

	bad_debug(5,"using global debug = %d\n", config.global->debug);
	#ifdef DEBUG
		bad_debug(5,"using global max_vdesks = %d\n", config.global->max_vdesks);
		bad_debug(5,"using global term = %s\n", config.global->term);
		bad_debug(5,"using global snap_distance = %d\n", config.global->snap_distance);
		bad_debug(5,"using global flip_resizelower = %d\n", config.global->flip_resizelower);
		bad_debug(5,"using key next = %s\n", config.keys->next);
		bad_debug(5,"using key term = %s\n", config.keys->term);
		bad_debug(5,"using key topleft = %s\n", config.keys->topleft);
		bad_debug(5,"using key topright = %s\n", config.keys->topright);
		bad_debug(5,"using key bottomleft = %s\n", config.keys->bottomleft);
		bad_debug(5,"using key bottomright = %s\n", config.keys->bottomright);
		bad_debug(5,"using key lower = %s\n", config.keys->topright);
		bad_debug(5,"using key max = %s\n", config.keys->max);
		bad_debug(5,"using key maxhoriz = %s\n", config.keys->maxhoriz);
		bad_debug(5,"using key maxvert = %s\n", config.keys->maxvert);
		bad_debug(5,"using key fix = %s\n", config.keys->fix);
		bad_debug(5,"using key kill = %s\n", config.keys->kill);
		bad_debug(5,"using key reload = %s\n", config.keys->reload);
		bad_debug(5,"using theme bgcolor = %s\n", config.theme->bgcolor);
		bad_debug(5,"using theme fgcolor = %s\n", config.theme->fgcolor);
		bad_debug(5,"using theme fxcolor = %s\n", config.theme->fxcolor);
	#endif
}

void getconf(void) {
        FILE *fp;
        char buf[1024];
        char *cp, *val;
	char config_file[1024];
        enum {INVALID=0, GLOBAL, KEYS, THEME} section = 0;

        *buf=0;
	snprintf(config_file, 1024, "%s/.BadWMrc", getenv("HOME"));
	#ifdef DEBUG
		bad_debug(1,"%s\n", config_file);
	#endif

	fp = fopen(config_file, "r");
        if(fp != NULL)
        {
                while(fgets(buf, sizeof(buf)-1, fp))
                {
                        cp = strchr(buf, '\n');
                        if(cp)
                                *cp= '\0';
                        if(!*buf)
                                continue;
                        if(*buf == '#')
                                continue;

                        if(*buf == '[')
                        {
                                if(!strcmp(buf, "[global]"))
                                        section = GLOBAL;
                                 else if (!strcmp(buf, "[keys]"))
                                        section = KEYS;
				 else if (!strcmp(buf, "[theme]"))
				 	section = THEME;
                                continue;
                        }

                        if (section == GLOBAL)
                        {
                                cp = strchr(buf, '=');
                                if (cp)
                                {
                                        *cp++ = '\0';
                                        while(*cp == ' ')
                                                cp++;

                                        val =  cp;
                                        cp = strchr(buf, ' ');
                                        if(cp)
                                                *cp = '\0';

                                        if(!strcmp(buf, "term")) config.global->term = strdup(val);
                                        if(!strcmp(buf, "term_args")) config.global->term_args = strdup(val);
                                        if(!strcmp(buf, "max_vdesks")) config.global->max_vdesks = atoi(val);
                                        if(!strcmp(buf, "snap_distance")) config.global->snap_distance = atoi(val);
                                        if(!strcmp(buf, "flip_resizelower")) config.global->flip_resizelower = atoi(val);
					if(!strcmp(buf, "solidwindows")) config.global->solidwindows = atoi(val);
					#ifdef DEBUG
						bad_debug(4,"setting from config term = %s\n", config.global->term);
						bad_debug(4,"setting from config max_vdesks = %d\n", config.global->max_vdesks);
						bad_debug(4,"setting from config snap_distance = %d\n", config.global->snap_distance);
						bad_debug(4,"setting from config term_args = %s\n", config.global->term_args);
						bad_debug(4,"setting from config flip_resizelower = %d\n", config.global->flip_resizelower);
						bad_debug(4,"setting from config solidwindows = %d\n", config.global->solidwindows);
					#endif
                                }
                        }

			if (section == KEYS)
                        {
                                cp = strchr(buf, '=');
                                if (cp)
                                {
                                        *cp++ = '\0';
                                        while(*cp == ' ')
                                                cp++;

                                        val =  cp;
                                        cp = strchr(buf, ' ');
                                        if(cp)
                                                *cp = '\0';

                                        if(!strcmp(buf, "next")) config.keys->next = strdup(val);
                                        if(!strcmp(buf, "term")) config.keys->term = strdup(val);
                                        if(!strcmp(buf, "topleft")) config.keys->topleft = strdup(val);
                                        if(!strcmp(buf, "topright")) config.keys->topright = strdup(val);
                                        if(!strcmp(buf, "bottomleft")) config.keys->bottomleft = strdup(val);
                                        if(!strcmp(buf, "bottomright")) config.keys->bottomright = strdup(val);
                                        if(!strcmp(buf, "lower")) config.keys->lower = strdup(val);
                                        if(!strcmp(buf, "max")) config.keys->max = strdup(val);
                                        if(!strcmp(buf, "maxhoriz")) config.keys->maxhoriz = strdup(val);
                                        if(!strcmp(buf, "maxvert")) config.keys->maxvert = strdup(val);
                                        if(!strcmp(buf, "fix")) config.keys->fix = strdup(val);
                                        if(!strcmp(buf, "kill")) config.keys->kill = strdup(val);
                                        if(!strcmp(buf, "reload")) config.keys->reload = strdup(val);
					if(!strcmp(buf, "vdup")) config.keys->vdup = strdup(val);
					if(!strcmp(buf, "vddown")) config.keys->vddown = strdup(val);
					#ifdef DEBUG
						bad_debug(4,"setting from config term = %s\n", config.keys->term);
						bad_debug(4,"setting from config next = %s\n", config.keys->next);
						bad_debug(4,"setting from config topleft = %s\n", config.keys->topleft);
						bad_debug(4,"setting from config topright = %s\n", config.keys->topright);
						bad_debug(4,"setting from config bottomleft = %s\n", config.keys->bottomleft);
						bad_debug(4,"setting from config bottomright = %s\n", config.keys->bottomright);
						bad_debug(4,"setting from config lower = %s\n", config.keys->topright);
						bad_debug(4,"setting from config max = %s\n", config.keys->max);
						bad_debug(4,"setting from config maxhoriz = %s\n", config.keys->maxhoriz);
						bad_debug(4,"setting from config maxvert = %s\n", config.keys->maxvert);
						bad_debug(4,"setting from config fix = %s\n", config.keys->fix);
						bad_debug(4,"setting from config kill = %s\n", config.keys->kill);
						bad_debug(4,"setting from config reload = %s\n", config.keys->reload);
						bad_debug(4,"setting from config vdup = %s\n", config.keys->vdup);
						bad_debug(4,"setting from configvddown = %s\n", config.keys->vddown);
					#endif
                                }
                        }

			if (section == THEME)
                        {
                                cp = strchr(buf, '=');
                                if (cp)
                                {
                                        *cp++ = '\0';
                                        while(*cp == ' ')
                                                cp++;

                                        val =  cp;
                                        cp = strchr(buf, ' ');
                                        if(cp)
                                                *cp = '\0';

                                        if(!strcmp(buf, "bgcolor")) config.theme->bgcolor = strdup(val);
                                        if(!strcmp(buf, "fgcolor")) config.theme->fgcolor = strdup(val);
                                        if(!strcmp(buf, "fxcolor")) config.theme->fxcolor = strdup(val);
					#ifdef DEBUG
						bad_debug(4,"setting from config bgcolor = %s\n", config.theme->bgcolor);
						bad_debug(4,"setting from config fgcolor = %s\n", config.theme->fgcolor);
						bad_debug(4,"setting from config fxcolor = %s\n", config.theme->fxcolor);
					#endif
                                }
                        }
                }
        } else {
		bad_debug(0,"Config file \"%s\" not found!\n", config_file);
	}

	/* set min_vdesk */
	if ( config.global->max_vdesks == 10 ) {
		config.global->min_vdesk = 0;
	} else {
		config.global->min_vdesk = 1;
	}

}


syntax highlighted by Code2HTML, v. 0.9.1