/* csg.c
 *
 * Simple test program to exercise renderer.
 */

#include <stdio.h>

#include "basetype.h"
#include "cartesian.h"
#include "print.h"
#include "error.h"
#include "vector.h"
#include "matrix.h"
#include "point.h"
#include "quadric.h"
#include "colour.h"
#include "surface.h"
#include "bitmap.h"
#include "pattern.h"
#include "indexlist.h"
#include "ray.h"
#include "solid.h"
#include "bbox.h"
#include "list.h"
#include "hitlist.h"
#include "hitdata.h"
#include "intersect.h"
#include "shapes.h"
#include "light.h"
#include "world.h"
#include "solidtrans.h"
#include "patterns.h"
#include "cast.h"
#include "parse.h"

void
main(argc, argv)
int	argc;
char	*argv[];
{
	int		arg = 1;
	world_t		*world;
	char		*file_name,
			*prog_name;
	FILE		*file;
	/* The following should be put into structures of some sort,
	 * then we need a function WorldMerge(w1, w2, bw) which merges
	 * the two structures shadowed by the boolean world strcuture.
	 */
	int		start_x, start_y, stop_x, stop_y, x_res, y_res;
	char		*out_file_name;
	bool_t		verbose;

	bool_t		b_start_x = FALSE,
			b_start_y = FALSE,
			b_stop_x = FALSE,
			b_stop_y = FALSE,
			b_x_res = FALSE,
			b_y_res = FALSE,
			b_verbose = FALSE,
			b_out_file_name = FALSE;

	prog_name = argv[0];
	

	while (arg < (argc - 1))
	{
		if (strcmp(argv[arg], "-xres") == 0)
		{
			x_res = atoi(argv[arg+1]);
			b_x_res = TRUE;
			arg += 2;
		}
		else
		if (strcmp(argv[arg], "-yres") == 0)
		{
			y_res = atoi(argv[arg+1]);
			b_y_res = TRUE;
			arg += 2;
		}
		else
		if (strcmp(argv[arg], "-startx") == 0)
		{
			start_x = atoi(argv[arg+1]);
			b_start_x = TRUE;
			arg += 2;
		}
		else
		if (strcmp(argv[arg], "-starty") == 0)
		{
			start_y = atoi(argv[arg+1]);
			b_start_y = TRUE;
			arg += 2;
		}
		else
		if (strcmp(argv[arg], "-stopx") == 0)
		{
			stop_x = atoi(argv[arg+1]);
			b_stop_x = TRUE;
			arg += 2;
		}
		else
		if (strcmp(argv[arg], "-stopy") == 0)
		{
			stop_y = atoi(argv[arg+1]);
			b_stop_y = TRUE;
			arg += 2;
		}
		else
		if (strcmp(argv[arg], "-to") == 0)
		{
			out_file_name = argv[arg+1];
			b_out_file_name = TRUE;
			arg += 2;
		}
		else
		if (strcmp(argv[arg], "-quiet") == 0)
		{
			verbose = FALSE;
			b_verbose = TRUE;
			arg += 1;
		}
		else
			Error("%s: illegal flag or wrong number of arguments", prog_name);
	}

	/* Should be pointing at file */

	if (arg >= argc)
		Error("%s: No input file specified", prog_name);

	if (strcmp(argv[arg], "-") == 0)
	{
		file_name = "<standard input>";
		file = stdin;
	}
	else
	{
		file_name = argv[arg];
		if ((file = fopen(argv[arg], "r")) == NULL)
			Error("%s: cannot open file %s", prog_name, argv[arg]);
	}

	world = Parse(file_name, file);

	/* Set up the world according to the arguments */

	if (b_x_res)
	{
		world->pixels_x = x_res;
		world->stop_x = x_res - 1;
	}
	if (b_y_res)
	{
		world->pixels_y = y_res;
		world->stop_y = y_res - 1;
	}
	if (b_start_x)
		world->start_x = start_x;
	if (b_start_y)
		world->start_y = start_y;
	if (b_stop_x)
		world->stop_x = stop_x;
	if (b_stop_y)
		world->stop_y = stop_y;
	if (b_out_file_name)
		world->file_name = out_file_name;
	if (b_verbose)
		world->report_progress = verbose;

	RayTrace(world);
}
