/*
 * |P|I|X|O| Image cataloging using html and thumb nails
 * Released under GPL http://www.gnu.org/copyleft/gpl.html
 * 
 * kishan@hackorama.com www.hackorama.com     2001 MAY
 *
 * Class "Options"  
 * Take care of command line options 
 * and the options file  .pixo file in future
 *  
 */
#include "options.h"


Options::Options()
{
	preProcess();
}

Options::Options(int _argc, char **_argv, int _max_args)
{
	argc = _argc;
	argv = _argv;
	max_args  = _max_args;
	preProcess();
	process();
	postProcess();
}

Options::~Options()
{

}

void 
Options::preProcess()
{
	/* the deafult options */
	def_tw = 0;
	def_th = 80;
	def_mw = 600;
	def_mh = 0;
	def_size = 5;
	def_align = 1;

	helped = false ;
	no_pages = false;
	no_images = false;
	tw = -1;
	th = -1;
	mw = -1;
	mh = -1;
	size = -1;
	align = -1;
	dir = NULL;
	new_args =  new char*[ max_args+1 ];
	for( int i = 0 ; i < max_args ; i++ )
		new_args[i] = NULL ;
}


void 
Options::process()
{
        int arg_count = 0;
        /* argv[0] is the programname dont parse it */
        for( int i = 1 ; i < argc ; i++ ){
                if( strcmp( argv[i], "?" ) == 0 ){ /* ? is help not regular */
			printUsage();
			printHelp();
		}else if(  argv[i][0] != '-' ) { /* all other regular argument */
                        if( arg_count < max_args ){
                                new_args[ arg_count] = argv[i] ;
                                arg_count++;
                        }else{ /* ignore extra arguments */
                                cout << endl << "Ignoring extra argument: " << argv[i] << endl ; 
                        }
                }else if( strcmp( argv[i], "-help" )   == 0 || 
			  strcmp( argv[i], "--help" )  == 0 || 
			  strcmp( argv[i], "-h" )      == 0 ){
			printUsage();
			printHelp();
                }else if( strcmp( argv[i], "-noimages" )  == 0 ){
                        no_images = true;
                }else if( strcmp( argv[i], "-nopages" )  == 0 ){
                        no_pages = true;
                }else if( strcmp( argv[i], "-indexsize" )  == 0 ){
                        if( i+1 < argc && argv[i+1][0] != '-' )
                                sscanf( argv[++i] , "%d", &size );
                }else if( strcmp( argv[i], "-thumb" )  == 0  ){
                        if( i+1 < argc && argv[i+1][0] != '-' )
                                sscanf( argv[++i] , "%d", &tw );
                        if( i+1 < argc && argv[i+1][0] != '-' )
                                sscanf( argv[++i] , "%d", &th );
                }else if( strcmp( argv[i], "-main" )  == 0 ){
                        if( i+1 < argc && argv[i+1][0] != '-' )
                                sscanf( argv[++i] , "%d", &mw );
                        if( i+1 < argc && argv[i+1][0] != '-' )
                                sscanf( argv[++i] , "%d", &mh );
                }else { /* unknown options print usage  */
                        cout << endl << "Unknown argument: " <<  argv[i] << endl ;
			printUsage();
                }
        }

}

void 
Options::postProcess()
{
	if( tw < 0 ) 	tw 	= def_tw;
	if( th < 0 )	th 	= def_th;
	if( mw < 0 ) 	mw 	= def_mw;
	if( mh < 0 ) 	mh 	= def_mh;
	if( size < 0 )	size 	= def_size;
	if(align < 0 ) 	align 	= def_align;

	dir = new_args[0] ;
}

void 
Options::cleanUp()
{
	delete[] new_args;
}

void 
Options::printUsage()
{
	static bool once = true ;
	if( once ){
		once = false;
		cout << endl ;
		cout << "Usage is: " << endl ;
		cout << "pixo [-help] [-noimages] [-nopages] [-thumb width height] " << endl ;
		cout << "     [-main width height] [-align aligntype] <directory>" << endl ;
		cout << endl ;

	}
}

void 
Options::printHelp()
{
	helped = true ;

	cout << endl ;
	cout << "-help 		Pints this help" << endl ;
	cout << "-noimages 	Do not process the image files" << endl ;
	cout << " 		only generate the HTML files" << endl ;
	cout << "-nopages 	Do not generate the HTML pages" << endl ;
	cout << " 		only process the image files" << endl ;
	cout << "-noframes 	Do not use frames, use a single HTML file" << endl ;
	cout << "-indexsize n 	The number of images in an index page" << endl ;
	cout << " 		[default is " << def_size << "]" << endl ;
	cout << "-thumb w h 	Specify the width (w) and height (h)" << endl ;
	cout << " 		to be used for the thumb nail images" << endl ;
	cout << " 		if either value is 0 only the other will" << endl ;
	cout << "		be used for scaling [default "; 
	cout << def_tw  << " " << def_th << " ]" << endl ;
	cout << "-main w h 	Specify the width (w) and height (h)" << endl ;
	cout << " 		to be used for the main display images" << endl ;
	cout << " 		if either value is 0 only the other will" << endl ;
	cout << "		be used for scaling [default ";
	cout << def_mw  << " " << def_mh << " ]" << endl ;
	cout << "-align type 	Specify the alignment type for the thumb nails" << endl ;
	cout << " 		1 - vertically on top (default)" << endl ;
	cout << " 		2 - vertically on bottom " << endl ;
	cout << " 		3 - horizontally on left side" << endl ;
	cout << " 		4 - horizontally on right  side" << endl ;
	cout << "<directory>  	the directory where the images are" << endl ;
	cout << " 		if none specified pixo will search the " << endl ;
	cout << " 		current directory \".\" for images" << endl ;
	cout << endl ;

}


syntax highlighted by Code2HTML, v. 0.9