/*
    gutenfetch - a small utility to list and fetch books available through
	project gutenberg

    Copyright (C) 2001, 2002, 2003, 2004 Russell Francis 

    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

Last updated on $Date: 2004/03/21 21:40:15 $ by $Author: johntabularasa $.
*/
#include "stddefs.h"
#include "libgutenfetch_init.h"
#include "libgutenfetch_filter.h"
#include "libgutenfetch_error.h"
#include "libgutenfetch_servers.h"
#include "libgutenfetch_listing.h"
#include "libgutenfetch_utility.h"
#include "libgutenfetch_detail.h"
#include "libgutenfetch_cache.h"
#include <curl/curl.h>
#ifdef HAVE_STDIO_H
	#include <stdio.h>
#endif
#ifdef HAVE_STDLIB_H
	#include <stdlib.h>
#endif 

/**
 * Initialize the gutenfetch library.
 *
 * @return GUTENFETCH_OK on success, something else on failure.
 */
gutenfetch_error_t
gutenfetch_init(void)
{
	char *tempdir;
#if (HAVE_CURL_GLOBAL_INIT == 1)
	long curl_flags; 
#endif

	/**
	 * This library should never be run as root!
	 */
	if ( (getuid() == 0) || (geteuid() == 0)) {
		fprintf(stderr, "Never ever run this application as root!\n");
		exit (-1);
	}
	
#ifdef ENABLE_NLS
	/* Enable Il8N */
	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);
#endif /* ENABLE_NLS */	

	/* This will create and initialize our temporary working directory. */
	tempdir = gutenfetch_util_get_temp_dir();
	FREE_NULL(tempdir);
	
	gutenfetch_error_init();
	gutenfetch_filter_init();
	gutenfetch_servers_init();
	gutenfetch_listing_init();
	gutenfetch_detail_init();
	gutenfetch_cache_init(FALSE); // default is to not use the cache.

#if (HAVE_CURL_GLOBAL_INIT == 1)	
	/* This function was defined in curl 7.8 so it may not be
	 * present on older libraries, don't call if it is not 
	 * present.
	 */
#	if defined(WIN32)
	curl_flags = CURL_GLOBAL_WIN32;
#	else
	curl_flags = CURL_GLOBAL_NOTHING;
#	endif /* WIN32 */	
	if (curl_global_init(curl_flags) != CURLE_OK)
		return GUTENFETCH_CURL_GLOBAL_INIT_FAILED;
#endif /* HAVE_CURL_GLOBAL_INIT */

	return GUTENFETCH_OK;	
}


/**
 * Release all resources used by the gutenfetch library.
 *
 */
void
gutenfetch_shutdown(void)
{
#if	(HAVE_CURL_GLOBAL_INIT == 1)
	curl_global_cleanup();
#endif /* HAVE_CURL_GLOBAL_INIT */

	gutenfetch_cache_shutdown();
	gutenfetch_detail_shutdown();
	gutenfetch_listing_shutdown();
	gutenfetch_servers_shutdown();
	gutenfetch_filter_shutdown();
	gutenfetch_error_shutdown();

	gutenfetch_util_free_temp_dir();
}

/**
 * gutenfetch_init_curl_handle
 *
 * Initialize a curl handle used for making network
 * connections.
 *
 * @return A valid CURL* handle or NULL.
 */
CURL *
gutenfetch_init_curl_handle(void)
{
	CURL *handle;
	if ( (handle = curl_easy_init()) == NULL) {
		return NULL;
	}	
	if (curl_easy_setopt(handle, CURLOPT_HEADER, FALSE) != CURLE_OK) {
		curl_easy_cleanup(handle);
		return NULL;
	}	
	if (curl_easy_setopt(handle, CURLOPT_USERAGENT, PACKAGE_STRING) != 
		CURLE_OK)
	{
		curl_easy_cleanup(handle);
		return NULL;
	}
	return handle;
}

/**
 * gutenfetch_shutdown_curl_handle
 *
 * Release resources used by the network connection.
 *
 * @param handle The network handle to free.
 */
void
gutenfetch_shutdown_curl_handle(CURL *handle)
{
	curl_easy_cleanup(handle);
}

/**
 * Return the version of the library.
 *
 * @return a statically allocated string which states the version
 *   of libgutenfetch we are.
 */
char *
gutenfetch_version(void)
{
	return PACKAGE_VERSION;
}

/**
 * Return whether the library is thread-safe or not.
 *
 * @return TRUE if the library is thread-safe, FALSE
 *		if it is not.
 */
int
gutenfetch_is_threadsafe(void)
{
#ifdef HAVE_PTHREAD
	return TRUE;
#else
	return FALSE;
#endif	
}


syntax highlighted by Code2HTML, v. 0.9.1