/* rmap - vector based global map generating program
 * Copyright (C) 2000 Reza Naima <reza@reza.net>
 *
 * http://www.reza.net/rmap/
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include "rmap.h" 
#include "functions.h"

int main (int argc, char **argv) {
	// default values 
	struct options o;
	short transparent = 0;
	struct imageLayout scene = {500,500,0}; //and some guesses at a random initializer
	// misc variables
	char *string;
	char *string2;
	struct stat buf;
	int i;
	//the various structs for the data file

	// the default behaviour
	bzero(&o, sizeof(o));
	o.zoom = 1.;
	o.gridlines = 1;
	o.desired_categories =0xFFFFFFFF; //default is all
	o.desired_continents =0xFFFFFFFF; //default is all

	// Process command line options
	if (argc == 1) {
		printf("Try `rmap -h' for more information.\n");
		exit(-1);
	}
	while(1) {
		int c, option_index;
     		static struct option long_options[] =
    			 {
			       {"zoom", 1, 0, 0},
			       {"xrot", 1, 0, 0},
			       {"yrot", 1, 0, 0},
			       {"zrot", 1, 0, 0},
			       {"datafile", 1, 0, 0},
			       {"outfile", 1, 0, 0},
			       {"colorfile", 1, 0, 0},
			       {"continent", 1, 0, 0},
			       {"category", 1, 0, 0},
			       {"height", 1, 0, 0},
			       {"width", 1, 0, 0},
			       {"transparent", 0, 0, 0},
			       {"cities", 0, 0, 0},
			       {"nogridlines", 0, 0, 0},
			       {0, 0, 0, 0}
			};
		c = getopt_long (argc, argv, "hv", long_options, &option_index);
		if (c == -1)
			break;

		switch (c) {
			case 0:
				if (!strcmp(long_options[option_index].name, "transparent")) 
					scene.transparent = 1;
				if (!strcmp(long_options[option_index].name, "zoom")) 
                                        o.zoom =  strtod(optarg, NULL);
				if (!strcmp(long_options[option_index].name, "xrot")) 
                                        o.xrot = strtod(optarg, NULL); //THE OPPOSITE OF THE RIGHT HAND RULE
				if (!strcmp(long_options[option_index].name, "yrot")) 
                                        o.yrot = strtod(optarg, NULL);
				if (!strcmp(long_options[option_index].name, "zrot")) 
                                        o.zrot = strtod(optarg, NULL);
				if (!strcmp(long_options[option_index].name, "height")) 
					scene.height = atoi(optarg);
				if (!strcmp(long_options[option_index].name, "width")) 
					scene.width  = atoi(optarg);
				if (!strcmp(long_options[option_index].name, "cities")) 
					o.cities = 1;
				if (!strcmp(long_options[option_index].name, "nogridlines")) 
					o.gridlines = 0;
				if (!strcmp(long_options[option_index].name, "datafile")) 
					o.mapfile = (char*) strdup(optarg);
				if (!strcmp(long_options[option_index].name, "colorfile")) 
					o.colorfile = (char*) strdup(optarg);
				if (!strcmp(long_options[option_index].name, "outfile")) 
					o.outfile = (char*) strdup(optarg);
				if (!strcmp(long_options[option_index].name, "continent")) {
					if (!strcmp(optarg,"all")) {
						o.desired_continents = 0xFFFFFFFF;
						break;
					}
					string = (char*) strtok(optarg, ",");
					if (string == NULL)  {
						fprintf(stderr, "Invalid option to --continent, skipping\n");
						break;
					}
					i = atoi(string);
					if (i < 1 || i > 5) {
						fprintf(stderr,"Continents must be in the range of 1-5\n");
						break;
					}
					o.desired_continents |= (0x00000001 << (i-1));
					while ((string = (char*) strtok(NULL,",")) != NULL) {
						i = atoi(string);
						if (i < 1 || i > 5) {
							fprintf(stderr,"Continents must be in the range of 1-5\n");
							break;
						}
						o.desired_continents |= (0x00000001 << (i-1));
					}
				}
				if (!strcmp(long_options[option_index].name, "category")) {
					if (!strcmp(optarg,"all")) {
						o.desired_categories = 0xFFFFFFFF;
						break;
					}
					string = (char*) strtok(optarg, ",");
					if (string == NULL)  {
						fprintf(stderr, "Invalid option to --categories, skipping\n");
						break;
					}
					i = atoi(string);
					if (i < 1 || i > 4) {
						fprintf(stderr,"Catagories must be in the range of 1-4\n");
						break;
					}
					o.desired_categories |= (0x00000001 << (i-1));
					while ((string = (char*) strtok(NULL,",")) != NULL) {
						i = atoi(string);
						if (i < 1 || i > 4) {
							fprintf(stderr,"Catagories must be in the range of 1-4\n");
							break;
						}
						o.desired_categories |= (0x00000001 << (i-1));
					}

				}
				break;
			case 'v':
				printf("%s version %s\n",PACKAGE,VERSION);
				break;
			case 'h':
				printf("Usage: rmap [--zoom=VALUE] [--xrot=VALUE] [--yrot=VALUE] [--zrot=VALUE]\n");
				printf("            [--datafile=FILENAME] [--continent=LIST] [--category=LIST]\n");
				printf("            [--outfile=FILENAME] [--height=PIXELS] [--width=PIXELS]\n");
				printf("            [--colorfile=FILENAME] [--nogridlines] [--cities] [-h] [-v]\n");
				printf("\n");	
				printf("\t--zoom=<value>             Zoom Value, 1=whole earth\n");
				printf("\t--xrot=<value>             Rotates the earth (LATITUDE)\n");
				printf("\t--yrot=<value>             Rotates the earth (LONGITUDE)\n");
				printf("\t--zrot=<value>             Rotates the earth\n");
				printf("\t--datafile=<path_to_file>  The datafile containing the vector data\n");
				printf("\t--outfile=<path_to_file>   The filename to the image to be generated\n");
				printf("\t--colorfile=<path_to_file> The filename of the color mapping file\n");
				printf("\t--continent=<list>         `all' or comma seperated list of numbers ...\n");
				printf("\t\t1=Africa, 2=Asia, 3=Europe, 4=N. America, 5=S. America\n");
				printf("\t--category=<list>          `all' or comma seperated list of numbers ...\n");
				printf("\t\t1=US State Boundaries, 2=Rivers, 3=International Boundaries,\n\t\t4=Coasts, Islands, Lakes\n");
				printf("\t--height=<value>           The height of the generated image\n");
				printf("\t--width=<value>            The width of the generated image\n");
				printf("\t--nogridlines              Do not display grid lines\n");
				printf("\t--cities                   Display some major cities\n");
				printf("\t-h                         This screen\n");	
				printf("\t-v                         Version information\n");	
				exit(-1);
				break;
			default :
				printf("Try `rmap -h' for more information.\n");
				exit(-1);
		}
	}

	// set some default values 
	if (o.mapfile == NULL)
		o.mapfile = (char*) strdup(MAPFILE); //default
	if (o.outfile == NULL)
		o.outfile = (char*) strdup(OUTFILE); //default

	// if ~/.rmaprc exists, use that file instead (for a colormap file)
	if (o.colorfile == NULL) {
		string = getenv("HOME");
		if (string != NULL) {
			string2 = (char*) malloc(strlen(string) + 10);
			if (string2 == NULL) {
				fprintf(stderr,"Malloc error?");
				exit(-3);
			}
			strcpy(string2, string);
			strcat(string2, "/.rmaprc");
			if (!stat(string2, &buf)) 
				o.colorfile = string2;
			else {
				o.colorfile = (char*) strdup(COLORFILE); //default
				free(string2);

			}
		}
	}
	// generate the image
	generate(&scene, &o);
	return 0;
}	


syntax highlighted by Code2HTML, v. 0.9.1