/* @(#)tparams.c	1.42 94/05/18  */

#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
#include "params.h"

int verbose = 0;

#define GETOPTARGS "B:Q:C:k:s:H:F:D:I:P:c:d:f:i:m:w:h:x:y:pbtEv"


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\
        -H %%s  - destination hostname             local subnet \n\
        -c %%d  - broadcast channel [0-9]          0    \n\
        -F %%s  - file of destination hostnames    none \n\
        -D %%s  - name of osprey device            /dev/o1k0 \n\
        -I %%d  - select input channel             1    \n\
        -P %%d  - delay x ms per transmit          0    \n\
        -d %%s  - save bitstream in file           NULL \n\
        -f %%d  - frames to broadcast              18000\n\
        -i %%d  - frames to skip between capture   0    \n\
        -m %%d  - maximum frames to buffer         2    \n\
        -s %%d  - shrink factor                    2    \n\
        -w %%d  - width of capture window          NTSC:640,PAL:768\n\
        -h %%d  - height of capture window         NTSC:480,PAL:576\n\
        -x %%d  - x coord of display window        (random)\n\
        -y %%d  - y coord of display window        (random)\n\
        -B %%d  - h261 bitrate                     256    \n\
        -Q %%d  - h261 max quant                   10    \n\
  Legal Switches:\n\
        -p      - disable preview \n\
        -b      - enable display of partial CellB frames by receiver \n\
        -t      - disable transmission \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 i;
    int c;
    double t;
    char *fileptr = NULL;

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

    /*
     * .... load parameter with default values ....
     */

    for (i = 0; i < MAXSENDTO; i++)
	param->hostname[0][0] = 0;
    param->hostnameknt = 0;
    param->cis_type = "CellB";
    param->do_cis = CELLB;
    param->x = -1;
    param->y = -1;
    param->width = -1;
    param->height = -1;
    param->scalex = 2;
    param->scaley = 2;
    param->channel = 0;
    param->frames = 18000;
    param->preview = 1;
    param->whichinput = 1;
    param->windowname = (char *) malloc(32);
    param->image_skip = 0;
    param->max_buffer = 2;
    param->datafile = NULL;
    param->cellb_xy_addr = 0;
    param->transmit = 1;
    param->polling = 0;
    param->error_out = 0;
    param->pattern = "IIII";
    param->h261_bitrate = 256;
    param->h261_maxquant = 10;
    
    param->devname = NULL;
    strcpy(param->windowname, "Video Broadcast: ");

    /*
     * .... parse command line arguments ....
     */
    while ((c = getopt(argc, argv, GETOPTARGS)) != EOF)
	switch (c) {

	case 'C':
	    if (!strcasecmp(optarg, "CellB")) {
		param->do_cis = CELLB;
		param->cis_type = "CellB";
            }
            else if (!strcasecmp(optarg, "H261")) {
		param->do_cis = H261;
		param->cis_type = "H261";
	    }
	    else if (!strcasecmp(optarg, "Jpeg")) {
		param->do_cis = JPEG;
		param->cis_type = "Jpeg";
	    }
	    else if (!strcasecmp(optarg, "UYVY")) {
		param->do_cis = UYVY;
		param->cis_type = "UYVY";
	    }
	    else
		usage(argv[0], optarg);
	    break;

	case 'k':
	    param->pattern = optarg;
	    break;

	case 's':

	    param->scaley = param->scalex = atoi(optarg);
	    break;

	case 'b':
	    param->cellb_xy_addr = 1;
	    break;
	case 'c':
	    param->channel = atoi(optarg);
	    break;
        case 'B':
	    param->h261_bitrate = atoi(optarg);
	    break;
        case 'Q':
	    param->h261_maxquant = atoi(optarg);
	    break;  
	case 'f':
	    param->frames = atoi(optarg);
	    break;
	case 'h':
	    param->height = atoi(optarg);
	    break;
	case 'p':
	    param->preview = 0;
	    break;
	case 'v':
	    verbose = 1;
	    break;
	case 'w':
	    param->width = atoi(optarg);
	    break;
	case 'x':
	    param->x = atoi(optarg);
	    break;
	case 'y':
	    param->y = atoi(optarg);
	    break;
	case 'H':
	    strncpy(param->hostname[param->hostnameknt], optarg, 64);
	    param->hostnameknt++;
	    break;
	case 'd':
	    param->datafile = optarg;
	    break;
	case 't':
	    param->transmit = 0;
	    break;
	case 'F':
	    fileptr = optarg;
	    break;
	case 'P':
	    param->polling = atoi(optarg);
	    break;
	case 'I':
	    param->whichinput = atoi(optarg);
	    break;
	case 'i':
	    param->image_skip = atoi(optarg);
	    break;
	case 'D':
	    param->devname = optarg;
	    break;
	case 'E':
	    param->error_out = 1;
	    break;
	case 'm':
	    param->max_buffer = atoi(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;
    }

    if (fileptr) {
	FILE *fp;

	if ((fp = fopen(fileptr, "r")) == NULL) {
	    fprintf(stderr, "Error in file open '%s'\n", fileptr);
	    free(param);
	    return NULL;
	}
	else {
	    while (fscanf(fp, "%s",
			  param->hostname[param->hostnameknt]) == 1 &&
		    param->hostnameknt < MAXSENDTO)
		param->hostnameknt++;
	}
    }

    return (param);
}


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