/*--------------------------------------------------------------------
 *	$Id: gmtset.c,v 1.3.4.2 2002/01/17 17:42:51 pwessel Exp $
 *
 *	Copyright (c) 1991-2002 by P. Wessel and W. H. F. Smith
 *	See COPYING file for copying and redistribution conditions.
 *
 *	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; version 2 of the License.
 *
 *	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.
 *
 *	Contact info: gmt.soest.hawaii.edu
 *--------------------------------------------------------------------*/
/*
 * gmtset will set the specified options to the argument that follows them.
 *
 * Author:	Paul Wessel
 * Date:	10-JUN-2000
 * Version:	3.4.1
 */
 
#include "gmt.h"

main (int argc, char **argv)
{
	int i, j;
	char *file = CNULL;
	
        GMT_stdout = stdout;    GMT_stdin = stdin;
	if (argc == 2 && argv[1][0] == '-' && argv[1][1] == 0) GMT_quick = TRUE;
	
	if (argc == 1 || GMT_quick) {
		fprintf (stderr, "gmtset %s - To set individual default parameters\n\n", GMT_VERSION);
		fprintf (stderr, "usage: gmtset [-G<defaultsfile>] PARAMETER1 [=] value1 PARAMETER2 [=] value2 PARAMETER3 [=] value3 ...\n");
		fprintf (stderr, "\tFor available PARAMETERS, see gmtdefaults man page\n");
		
		if (GMT_quick) exit (EXIT_FAILURE);

		fprintf (stderr, "\n\tOPTIONS:\n");
		fprintf (stderr, "\t-G sets name of specific .gmtdefaults file to modify\n");
		fprintf (stderr, "\t   [Default looks for file in current directory,  If not fould,\n");
		fprintf (stderr, "\t   it looks in the home directory, if not found it uses GMT defaults.\n");
		fprintf (stderr, "\t   The modified defaults are written to the current directory]\n");
		
		exit (EXIT_FAILURE);
	}
	
	for (i = strlen(argv[0]); i >= 0 && argv[0][i] != '/'; i--);
	GMT_program = &argv[0][i+1];	/* Name without full path */

	for (i = 1, j = 0; i < argc && j == 0; i++) {
		if (!strncmp (argv[i], "-G", 2)) {
			file = &argv[i][2];
			j = i;
		}
	}
	
	if (j) {
		for (i = j + 1; i < argc; i++, j++) argv[j] = argv[i];	/* Remove the -G string */
		argc--;
	}
		
	GMT_getdefaults (file);
	
	GMT_setdefaults (argc, argv);

	if (file)
		GMT_savedefaults (file);
	else
		GMT_savedefaults (".gmtdefaults");

	exit (EXIT_SUCCESS);
}
