/*
 * Copyright (c) 2008 Telappliant Ltd.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */


#include "config.h"

#define _GNU_SOURCE
#define __USE_GNU

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

static void
rtrim( char* str, char* trim )
{
	int i;
	for( i = strlen(str) - 1;
  	     i >= 0 && strchr(trim,str[i]) != NULL;
	     i-- )
	{
		str[i] = '\0';
	}
}

struct sescle_config*
config_new( void )
{
	struct sescle_config* cfg;

	cfg = malloc(sizeof(struct sescle_config));
	cfg = memset(cfg, 0, sizeof(struct sescle_config));

	return cfg;
}

void
config_delete( struct sescle_config *cfg )
{
	if( cfg->save_handler != NULL )
		free(cfg->save_handler);

	if( cfg->save_path != NULL )
		free(cfg->save_path);

	free( cfg );
}

void
config_fill( struct sescle_config* cfg, char* php_exe, char* php_ini )
{
	assert( cfg != NULL );

	char* l_php = "php";
	char* l_cmd = NULL;
	FILE* l_handle = NULL;

	if( php_exe != NULL )
	{
		l_php = php_exe;
	}

	if( php_ini != NULL )
	{
		asprintf(&l_cmd, "%s -c %s -i | grep -e '^session.'", l_php, php_ini);
	} else
	{
		asprintf(&l_cmd, "%s -i | grep -e '^session.'", l_php);
	}

	l_handle = popen(l_cmd, "r");
	assert( l_handle != NULL );

	int l_read = 0;
	while( 1 )
	{
		char*	t_option = NULL;
		char*	t_value = NULL;
		char*	t_default = NULL;

		l_read = fscanf(l_handle, "%as => %a[^=] => %a[^\n]\n", &t_option, &t_value, &t_default);
		if( l_read == 3 )
		{
			if( strcmp(t_option, "session.gc_maxlifetime") == 0 )
			{
				cfg->gc_maxlifetime = atoi(t_value);
			} else if( strcmp(t_option, "session.save_handler") == 0 )
			{
				cfg->save_handler = strdup(t_value);
				rtrim(cfg->save_handler, " ");
			} else if( strcmp(t_option, "session.save_path") == 0 )
			{
				cfg->save_path = strdup(t_value);
				rtrim(cfg->save_path, " ");
			}
		}

		if( t_option != NULL )
			free(t_option);

		if( t_value != NULL )
			free(t_value);

		if( t_default != NULL )
			free(t_default);

		if( l_read != 3 )
		{
			break;
		}
	}

	pclose(l_handle);
	free(l_cmd);

	// Special case for null values
	if( strcmp(cfg->save_path, "no value") == 0 )
	{
		free(cfg->save_path);
		cfg->save_path = NULL;
	}
}

