#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wrapper.h"

int parse(int *argcp, const char *argv[], int *noutmp, const char **display,
	const char **childprogram)
{
	int i, j, k, argc=*argcp;
	const char *option[]=	/* List of options that take a parameter */
	{
		"-T", "-b", "-bd", "-bg", "-bw", "-cc", "-cr", 
		"-fb", "-fg", "-fi", "-fn", "-geometry", "-lf", "-me", "-ms",
		"-n", "-name", "-nb", "-sl", "-title", "-tm", "-tn", "-w", 
		"-xrm", NULL
	};

	*display=getenv("DISPLAY");
	*childprogram=NULL;
	for (i=1,k=1; i<argc; i++)
	{
		if (!strcmp(argv[i], "-e")) goto skip;
		if (!strcmp(argv[i], "-ut")) *noutmp=1;
		if (!strcmp(argv[i], "+ut")) *noutmp=0;
		if (!strcmp(argv[i], "-display"))
		{
			if (i+1<argc) 
			{
				argv[k++]=argv[i++];
				*display=argv[i];
			}
			goto next;
		}
		if (!strcmp(argv[i], "-program"))
		{
			if (i+1<argc) 
			{
				*childprogram=argv[++i];
				continue;
			}
			fprintf(stderr, 
				"Argument required for -program\n");
			return 1;
		}
		for (j=0; option[j]; j++)
		{
			if (!strcmp(argv[i], option[j]))
			{
				argv[k++]=argv[i++];
				break;
			}
		}
	next:
		if (i<argc) argv[k++]=argv[i];
	}
skip:
	while (i<argc) argv[k++]=argv[i++];
	if (!*display) *display="";
	*argcp=k;
	return 0;
}

