/*
 * 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"
#include "cleaner.h"
#include "debug.h"

#include <unistd.h>
#include <stdio.h>
#include <libgen.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <signal.h>
#include <assert.h>

static int g_shutdown = 0;

void
sig_shutdown( int signal )
{
	g_shutdown = 1;
}

void
show_usage( char* path )
{
	char*	l_exe;

	l_exe = basename(path);

	printf(" info: PHP SESsion CLEaner v%.2f\n", SESCLE_VERSION);
	printf("       Copyright (c) 2008 Telappliant Ltd.\n");
	printf("\n");
	printf("usage: %s [-p path] [-i path] [-v] [-h]\n", l_exe);
	printf("\t-p\tPHP executable\n");
	printf("\t-i\tPHP ini file\n");
	printf("\t-v\tVerbose output\n");
	printf("\t-h\tShow this help\n");
}

static int
run( struct sescle_config* cfg )
{
	struct sescle_cleaner* cleaner;
	int ret_val = 0;

	cleaner = cleaner_new( cfg );
	assert( cleaner != NULL );

	printf("Raah!");

	while( ! g_shutdown )	
	{
		ret_val = cleaner_run( cleaner, 1 );
		if( ret_val == -1 )
		{
			break;
		}
	}

	cleaner_delete( cleaner );

	return ret_val;
}

int
main( int argc, char **argv )
{
	int c;
	struct sescle_config*	cfg;
	char	php_exe_tmp[PATH_MAX+1];
	char	php_ini_tmp[PATH_MAX+1];
	char*	php_exe = NULL;
	char*	php_ini = NULL;
	int	ret_val = 0;

	cfg = config_new();

	while( 1 )
	{
		c = getopt(argc, argv, "hvp:i:");
		if( c == -1 )
		{
			break;
		}

		switch( c )
		{
		case 'h':
			show_usage(argv[0]);
			return 1;

		case 'v':
			cfg->verbose = 1;
			break;

		case 'p':
			php_exe = realpath(optarg, php_exe_tmp);
			if( php_exe == NULL )
			{
				perror("invalid PHP executable path");
				return 1;
			}
			break;

		case 'i':
			php_ini = realpath(optarg, php_ini_tmp);
			if( php_ini == NULL )
			{
				perror("invalid PHP .ini path");
				return 1;
			}
			break;

		default:
			return 1;
		}
	}

	config_fill( cfg, php_exe, php_ini );

	if( strcmp(cfg->save_handler,"files") != 0 )
	{
		fprintf(stderr, "error: session save handler '%s' not supported\n", cfg->save_handler);
		return 1;
	}

	if( cfg->save_path == NULL )
	{
		fprintf(stderr, "error: no session.save_path has been set!\n");
		return 1;
	}

	if( cfg->verbose )
	{
		printf("Settings from %s:\n", php_ini);
		printf("\tsession.save_handler %s\n", cfg->save_handler);
		printf("\tsession.save_path = \"%s\"\n", cfg->save_path);
		printf("\tsession.gc_maxlifetime = %ld\n", cfg->gc_maxlifetime);
		printf("\n");
	}

	chdir( cfg->save_path );

	//signal(SIGHUP, sig_shutdown);
	ret_val = run( cfg );

	config_delete( cfg );

	return ret_val;
}
