/*
 
 Copyright (C) 2010,2011 Timo Lassmann <timolassmann@gmail.com>
 
 This file is part of aln_filter.
 
 aln_filter 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 3 of the License, or
 (at your option) any later version.
 
 aln_filter 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 aln_filter.  If not, see <http://www.gnu.org/licenses/>.
 
 */

#include "interface.h"
#include "aln_filter.h"

struct parameters* interface(struct parameters* param,int argc, char *argv[])
{
	int c;
	int help = 0;

	if (argc < 2){
		usage();
		exit(0);
	}
		
	param = malloc(sizeof(struct parameters));
	param->infiles = 0;
	param->infile = 0;
	param->outfile = 0;
	param->id_cutoff = 90.0;
	param->quiet_flag = 0;
	param->discard = 0;
	
	//param->fdr = 0.05;
	//param->mean_error = 0.0f;
	while (1){	 
		static struct option long_options[] ={
			//{"as",required_argument,0,OPT_ASSEMBLY},
			{"quiet",required_argument,0,'q'},
			{"discard",required_argument,0,OPT_discard},
			{"help",0,0,'h'},
			{0, 0, 0, 0}
		};
		
		int option_index = 0;
		c = getopt_long_only (argc, argv,"c:o:qh",long_options, &option_index);
		
		if (c == -1){
			break;
		}
		
		switch(c) {
			case 0:
				
				break;
			case OPT_discard:
				
				break;
			case 'c':
				if(optarg[0] == '-'){
					fprintf(stderr,"option -c requires an argument\n");
					exit(-1);
				}
				param->id_cutoff = atof(optarg);
				break;
			case 'o':
				if(optarg[0] == '-'){
					fprintf(stderr,"option -o requires an argument\n");
					exit(-1);
				}
				param->outfile = optarg;
				break;	
			//case 'q':
			//	param->quality_cutoff =  1.0 - pow(10.0, -1.0 *   atof(optarg) / 10.0); 
			//	break;
			case 'h':
				help = 1;
				break;
			case '?':
				exit(1);
				break;
			default:
				fprintf(stderr,"default\n\n\n\n");
				abort ();
		}
	}
	//fprintf(stderr,"Viterbi: %d\n",param->viterbi);
	
	if(help){
		usage();
		free(param);
		exit(0);
	}
	
	
	
	/*if (!isatty(0)){
		j = 1;
	}else{
		j = 0;
	}*/
	param->infile = malloc(sizeof(char*)* (argc-optind ));
	
	c = 0;
	while (optind < argc){
		param->infile[c] =  argv[optind++];
		c++;
		/*if(c ==1 && j == 1){
			param->infile[c] = 0;
			c++;
		}*/
	}
	param->infiles = c;
	
	param->id_cutoff  = param->id_cutoff  / 100.0;
	return param;
}


void free_param(struct parameters* param)
{
	free(param->infile);
	free(param);
}

void usage()
{
        fprintf(stdout, "\n%s, Copyright (C) 2010, 2011 Timo Lassmann <timolassmann@gmail.com>\n", VERSION);
	fprintf(stdout, "\n");
	fprintf(stdout, "Usage:   aln_filter  [options]  <SAM/BAM file>\n\n");
	fprintf(stdout, "Options:\n");
	fprintf(stdout, "         -c             FLT    percentage identity cutoff [90.0].\n");
	fprintf(stdout, "         -o             STR    output file name [stdout].\n");
	fprintf(stdout, "         -discard       NA     discard alignments with coordinate problems [off].\n");
	fprintf(stdout, "\n");

        fprintf(stdout, "\n");
}

