/* unsea.c - Part of the unsea program.
 *   Copyright 1992 by David W. Rankin, Jr.
 * 
 * See "unsea.txt" for full copyright information applying 
 * to all text in this package. 
 *
 *
 * This file holds main(), and other main parts of unsea. */

/* include statements */

#include "unsea.h"

/* Beginning of code */

struct starting_flags stflags;

void main(int argc, char *argv[])
{
	extern struct starting_flags stflags;
	
	char newfilename[FILENAME_MAX+1];/* the place for the file to be created */

	int i, j, /* generic counters */
		
		arg_per_file; /* see its use */
	
	/* start of code */

	strcpy(newfilename,""); /* This initializes this string */
	
/* This is for use on THINK_C compilers only. */
#ifdef THINK_C
    argc=ccommand(&argv);
#endif /*THINK_C*/
	
	startupmesg();

	if (argc==1)
		{
		help();  /* tell basics, then quit */
		
		exit(EXIT_SUCCESS); }
		
	/* implied "else if (argc>=2)" */
		
	
	 for(i=1;i<argc;i++)
	 	{ /* Is argv[i] "--"? If so, quit looking for options
	 	   * completely. */ 
	 	if(!strncmp(argv[i],"--",2))
	 		{
	 		i++;

	 		break;  }
		 	/* If argv[i] doesn't have '-' at the beginning, quit looking
		 	 * as well, but make no incrementation */

	 	if(argv[i][0] != '-')
	 		break;
		 	
		if(check_startup_flags(argv[i]))
			{
			fprintf(stderr,"\
Fatal error within option flags. Unable to proceed with conversion \n");

	 		help();

	 		exit(EXIT_FAILURE);

	 		}
	 	} /* end of for() loop */
	if (stflags.name_given && ((argc-i) % 2) )
			/* If there are supposed to be an even # of arguments,
			 * but aren't, then spit it back... */
		{ fprintf(stderr, "Improper number of arguments for -n flag.\n");

	 	  exit(EXIT_FAILURE);
	 	}

	if(i==argc)
		{ /* this sees if no files were selected for conversion.
		   * If so, give help and error out. */
		help();

		exit(EXIT_FAILURE);
		}

	arg_per_file = (stflags.name_given) ? 2 : 1;

	while(i<argc)
		{
		if(stflags.name_given)
			strcpy(newfilename, argv[i+1]);

		unsea_mb(argv[i], newfilename);

		strcpy(newfilename, "");

		i += arg_per_file;

		}
		 	  	
}  /* end of main() */

void restypecopy(unsigned char block[], const int startint, char s[])

{	int i;

	for (i=0;i<4;i++)
		block[startint+i] = s[i];
	
} /* end of restypecopy() */

int check_startup_flags(char *string)
{
	extern struct starting_flags stflags;
	int i, len,bad_flag=FALSE;
	
	len = strlen(string);
	
	for(i=0; i<len; i++)
		{
		switch (string[i]) {
			
			case 'B': /* 'b'rief descriptions mode. */
			case 'b':
				stflags.there = TRUE;

				stflags.less_talk = TRUE;
					/* less talk is what happens... */
				break;
			
			
		
		/* This case is put in for future versions of unsea, but is
		 * dunsel for now. */
			case 'D': /* ignore 'D'iskDoubler SEAs */
			case 'd':
				stflags.there = TRUE;

				stflags.ignore_dd_seas = TRUE;

				break;
			
			case 'C': /*ignore 'C'ompact Pro SEAs */
			case 'c':
				stflags.there = TRUE;

				stflags.ignore_cpt_seas = TRUE;

				break;
			
			case 'S': /* Ignore 'S'tuffIt SEAs /*
			case 's':
				stflags.there = TRUE;

				stflags.ignore_sitd_seas = TRUE;

				break;
			
			case 'N': /* new 'n'ame given */
			case 'n':
				stflags.there = TRUE;

				stflags.name_given = TRUE;

				break;
			
			case 'O': /* 'O'verwrite "newfilename" */
			case 'o':
				stflags.there = TRUE;

				stflags.overwrite = TRUE;
					/* Overwrite any file in the way of the new file */
				break;
			
			case '-': /* Ignore '-', as per UNIX custom */
				break;
			
			default:  /* when a bad flag is found, let main() know*/
				bad_flag=TRUE; 
				break;
			}
		
		}
		return bad_flag;
} /* End of check_startup_flags() */
