/*

Copyright (c) 2002-2005, Yauheni Akhotnikau
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

- The name of the author may not be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.

*/

#include <iostream>
#include <fstream>
#include <cerrno>
#include <cstring>

#include <args_4/h/args_iostream.hpp>

#include <auto_ptr_3/h/obj_ptr.hpp>

#include <oess_1/defs/log/h/err.hpp>

#include <oess_1/defs/h/ex.hpp>

#include <oess_1/db/site/h/localhost.hpp>
#include <oess_1/db/cln/h/db.hpp>

#include <oess_1/defs/util/h/logo.hpp>

void
show_usage()
{
	std::cout << OESS_1__LOGO << std::endl
		<< "Enumerate entities in slice\n"
			"\n"
			"oess_slice_create [options] [@reponse-file]\n\n"
			"options:\n"
			"\t-db <name-of-db>  database name (mandatory)\n"
			"\t-slice <slice_name>  slice name (mandatory)"
			<< std::endl;
}

void
parse_resp_file(
	const char * resp_file_name,
	args_4::cmd_t & arg_db,
	args_4::cmd_t & arg_slice ) {

	args_4::cmd_parser_t args_parser;
	args_parser.add_cmds( &arg_db, &arg_slice, 0 );

	args_4::words_from_file_t words_container(
		resp_file_name );

	args_4::iostream_error_stream_t args_error_stream;
	int error = args_parser.parse( words_container,
		&args_error_stream );
	if( args_4::c_ok != error ) {
		if( args_4::c_io_error == error ) {
			OESS_THROW_LOGIC( error,
				"I/O error with response file: "
				<< resp_file_name << ", error: "
				<< strerror(
					words_container.query_last_io_result() ) )
		}
		else
			OESS_THROW_LOGIC( error,
				"unable to parse response file: "
				<< resp_file_name )
	}
}

void
check_args(
	args_4::cmd_str_value_t & arg_db,
	args_4::cmd_str_value_t & arg_slice )
{
	if( !arg_db.is_defined() )
		OESS_THROW_LOGIC( args_4::c_mandatory_cmd_missed,
			"cmd-line argument missed: "
			<< arg_db.query_name() )
	if( !arg_slice.is_defined() )
		OESS_THROW_LOGIC( args_4::c_mandatory_cmd_missed,
			"cmd-line argument missed: "
			<< arg_slice.query_name() )
}

void
parse_args(
	int argc, char ** argv,
	args_4::cmd_str_value_t & arg_db,
	args_4::cmd_str_value_t & arg_slice ) {

	args_4::cmd_response_file_t arg_resp_file;
	args_4::cmd_parser_t args_parser;
	args_parser.add_cmds( &arg_db, &arg_slice,
		&arg_resp_file, 0 );

    args_4::words_from_argv_t words_container(
		argc, argv );
	args_4::iostream_error_stream_t args_error_stream;

	int error = args_parser.parse( words_container,
		&args_error_stream );
	if( args_4::c_ok == error ) {
		// Если нужно, разбираем response-файлы.
		if( arg_resp_file.is_defined() ) {
			for( size_t i = 0, i_max =
				arg_resp_file.query_value_count();
				i != i_max; ++i )
				parse_resp_file( arg_resp_file.query_value( i ),
					arg_db, arg_slice );
		}
	}
	else
		OESS_THROW_LOGIC( error,
			"unable to parse cmd-line arguments" )

	check_args( arg_db, arg_slice );
}

void
enumerate_slice(
	oess_1::db::cln::db_t & db,
	const std::string & slice_name )
{
	oess_1::ent_id_t id;
	do {
		id = db.ent_find_next( slice_name, id );
		if( id )
			std::cout << id << std::endl;
	} while( id );
}

void
do_work( int argc, char ** argv )
{
	if( 1 == argc )
		show_usage();
	else {
		args_4::cmd_str_value_t arg_db( "-db", false );
		args_4::cmd_str_value_t arg_slice( "-slice", false );
		parse_args( argc, argv, arg_db, arg_slice );

		auto_ptr_3::obj_ptr_t< oess_1::db::site::localhost_t >
			localhost_ptr( oess_1::db::site::create_std_localhost() );
		localhost_ptr->db_describe( "db", arg_db.query_value(),
			16, true, false );

		auto_ptr_3::obj_ptr_t< oess_1::db::cln::db_t >
			db_ptr( oess_1::db::cln::create_std_db(
				oess_1::db::site::create_std_localhost_connector(
					*localhost_ptr ) ) );
		db_ptr->attach( "db" );

		enumerate_slice( *db_ptr, arg_slice.query_value() );

		db_ptr->detach();
	}
}

int
main( int argc, char ** argv ) {
	try {
		do_work( argc, argv );

		return 0;
	}
	catch( oess_1::ex_t & x ) {
		std::cerr << "*** " << x.query_err_code() << " ***"
			<< std::endl;
	}
	catch( ... ) {
		OESS_1_LOG_ERR( "Oops! Unknown exception!" << std::endl )
	}
	
	return 1;
}
