/* @(#)rparams.c	1.28 94/05/13  */

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

#include "params.h"

int verbose = 0;

#define GETOPTARGS "C:c:f:x:y:bEv"

static 
usage(char *name, char *badparam)
{
    if (badparam)
	fprintf(stderr, "ERROR: bad parameter [%s]\n", badparam);


    fprintf(stderr, "\
Usage: %s [options]\n\n\
  Legal Options:                                 Default  \n\
        -C %%s  - Compression type                 CellB  \n\
                    [ CellB | Jpeg | UYVY | H261]         \n\
        -c %%d  - receive channel [0-9]            0      \n\
        -f %%d  - frames to receive                18000  \n\
        -x %%d  - x coord of display window        (random)\n\
        -y %%d  - y coord of display window        (random)\n\
  Legal Switches:\n\
        -b      - enable display of partial CellB frames \n\
        -E      - exit on first error \n\
        -v      - print verbose messages\n\
", name);




    exit(1);
}



Parameters *
initialize_parameters(int argc, char *argv[])
{
    Parameters *param;
    int c;
    double t;

    /*
     * .... create parameter block ....
     */
    param = (Parameters *) malloc(sizeof(Parameters));
    if (param == NULL)
	return (param);

    /*
     * .... load parameter with default values ....
     */
    param->hostnameknt = 0;
    param->cis_type = "CellB";
    param->do_cis = CELLB;
    param->x = -1;
    param->y = -1;
    param->width = 320;
    param->height = 240;
    param->channel = 0;
    param->frames = 18000;
    param->preview = 1;
    param->whichinput = 0;
    param->devname = NULL;
    param->windowname = (char *) malloc(32);
    param->cellb_xy_addr = 0;
    param->error_out = 0;
    param->scalex = 1;
    param->scaley = 1;

    strcpy(param->windowname, "Video Conference: ");

    /*
     * .... parse command line arguments ....
     */

    while ((c = getopt(argc, argv, GETOPTARGS)) != EOF)
	switch (c) {
	case 'b':
	    param->cellb_xy_addr = 1;
	    break;
	case 'c':
	    param->channel = atoi(optarg);
	    break;
	case 'f':
	    param->frames = atoi(optarg);
	    break;
	case 'v':
	    verbose = 1;
	    break;
	case 'x':
	    param->x = atoi(optarg);
	    break;
	case 'y':
	    param->y = atoi(optarg);
	    break;
	case 'E':
	    param->error_out = 1;
	    break;
	case 'C':
	    if (!strcasecmp(optarg, "CellB")) {
		param->do_cis = CELLB;
		param->cis_type = "CellB";
	    }
	    else if (!strcasecmp(optarg, "Jpeg")) {
		param->do_cis = JPEG;
		param->cis_type = "Jpeg";
            }
            else if (!strcasecmp(optarg, "H261")) {
		param->do_cis = H261;
		param->cis_type = "H261";
	    }
	    else if (!strcasecmp(optarg, "UYVY")) {
		param->do_cis = UYVY;
		param->cis_type = "UYVY";
	    }
	    else
		usage(argv[0], optarg);
	    break;
	case '?':
	default:
	    usage(argv[0], NULL);
	    break;
	}

    /* expect no args left over */
    if (argv[optind]) {
	usage(argv[0], argv[optind]);
    }

    strncat(param->windowname, param->cis_type, 5);

    if (param->do_cis != CELLB && param->cellb_xy_addr) {
	fprintf(stderr, "-b option only valid for CellB\n");
	param->cellb_xy_addr = 0;
    }

    return (param);
}


void 
close_parameters(Parameters * pblock)
{
    free(pblock);
}
