/******************************************************************
 * options.c
 *
 * $Id: options.c,v 1.10 2000/12/19 02:49:01 alderman Exp $
 *
 * Get commandline arguments and set options accordingly.
 *
 ******************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#include "options.h"

int block_size = ONE_MEGA;
int num_streams = 10;
int bit_rate = ONE_MEGA * 1.5;
int num_blocks = 100;
static int calc_num_blocks = 1;

void printUsage() {
	printf("Command line options: [ -b block size ] [ -s # streams ] [ -r bit rate ] [ -n number blocks ]");
	printf(" block size is in Kb, bit rate is mbps; number blocks defaults to 100Mb/block size\n");
}

void parseArgs(int argc, char *argv[]) {
	extern char *optarg;
	extern int optind;
	int ch;

	extern int block_size;
	extern int num_streams;
	extern int bit_rate;
        extern int num_blocks;

	while((ch = getopt(argc, argv, "b:s:r:n:")) != -1) {
		switch(ch) {
		case 'b':
			block_size = atoi(optarg)*1024;
			break;
		case 's':
			num_streams = atoi(optarg);
			break;
		case 'r':
			bit_rate = (int)(atof(optarg)*1024.0*1024.0);
			break;
                case 'n':
                        num_blocks = atoi(optarg);
                        calc_num_blocks = 0;
                        break;
		default:
			perror("Unknown command line argument");
			printUsage();
			exit(1);
		}
	}
	argc -= optind;
	argv += optind;
}

int validateParams(void) {
	extern int block_size;
	extern int num_streams;
	extern int bit_rate;
        extern int num_blocks;

        if(calc_num_blocks) {
                num_blocks = 100 * ONE_MEGA / block_size;
        }

	if(block_size < 1 || block_size > MAX_BUF_SIZE) {
		fprintf(stderr, "Validation failed: block_size: %d\n", block_size);
		return VALIDATION_ERROR;
	}
	if(num_streams < 1 || num_streams > MAXIMUM_REASONABLE_THREADS) {
		fprintf(stderr, "Validation failed: num_streams: %d\n", num_streams);
		return VALIDATION_ERROR;
	}
	if(bit_rate < 1) {
		fprintf(stderr, "Validation failed: bit_rate: %d\n", bit_rate);
		return VALIDATION_ERROR;
	}
        if(num_blocks < 1 || num_blocks > 400) {
                fprintf(stderr, "Validation failed: num_blocks %d\n", num_blocks);
                return VALIDATION_ERROR;
        }
	printf("Options: block_size: %d, num_streams: %d, bit_rate: %d, num_blocks: %d\n",
		block_size, num_streams, bit_rate, num_blocks);
	return VALIDATION_SUCCESS;
}


			
