/* AUTOPICS  by  Nick Ruppert  5-23-87
    will call your viewing program to display all the pics in a directory
    having the user-specified extension (such as .pic or .ham).
	Autopics will default to "uShow" and ".pic".
	Compiled with Manx Aztec 3.4 +l.  Linked with c32.lib.
*/
/*
	For questions & comments, I can be reached thru
	PSA BBS (sysop Dorothy Dean)
	414-278-5390
*/
#include <stdio.h>
main(argc, argv)
int argc;
char *argv[];
{
	static char extension[7];  /* storage for extension (* + 5 chars + EOS)*/
	static char *args[] = {"uShow", ".pic", ""};
	static char *defaults[] = {"Defaulting to \"uShow\" and \".pic\".",
				"Defaulting to \"uShow\".", "Defaulting to \".pic\"."};
	char *scdir(), *malloc(), *ptr, *files[100], *usage = 
		"USAGE: autopics [viewing_prog] [extension] [start (optional)]";
		/* ptr = pointer to temp dir filename */
	int count, count1, compare();

	switch (argc)
	{
	case (1) :	puts(usage); puts(defaults[0]); break;
	case (2) :	if (argv[1][0] == '.')
					{ args[1] = argv[1]; puts(defaults[1]); }
				else if (tolower(argv[1][0]) == 's' && argv[1][1] == '=')
					{ args[2] = &argv[1][2]; puts(defaults[0]); }
				else
					{ args[0] = argv[1]; puts(defaults[2]); }
				break;
	case (3) :  if (tolower(argv[2][0]) == 's' && argv[2][1] == '=')
					args[2] = &argv[2][2];
				else
					{ args[0] = argv[1]; args[1] = argv[2]; break; }
				if (argv[1][0] == '.')
					{ args[1] = argv[1]; puts(defaults[1]); }
				else
					{ args[0] = argv[1]; puts(defaults[2]); }
				break;
	case (4) :	for (count = 1; count < argc-1; count++)
					args[count-1] = argv[count];
				if (tolower(argv[count][0]) == 's' && argv[count][1] == '=')
					args[count-1] = &argv[count][2];
				break;
	default :	puts("Too many args."); 
				puts(usage);
				exit();
	}
	extension[0] = '*';  /* make 1st extension char '*' */
	for (count = 0; count < strlen(args[1]) && count < 5; count++)
		extension[count+1] = args[1][count];
	fputs("Reading the directory for all files", stdout);
	fputs(" with the extension \"", stdout);
	fputs(extension, stdout);
	puts("\"...");
	for (count = 0; (ptr = scdir(extension)) && count < 100; count++)
		{
		files[count] = malloc(strlen(ptr)+1);
		strcpy(files[count], ptr);
		}
	if (count)
		{
		count1 = 0; /* initialize */
		if (*args[2])
			{
			for (; compare(args[2], files[count1]) &&
				count1 < count; count1++) ;
			if (count1 == count)
				{
				fputs("\nERROR: \"", stdout);
				fputs(args[2], stdout);
				puts("\" is not in this directory");
				exit(1);
				}
			}
		puts("\nNow viewing...");
		for (; count1 < count; count1++)
			{
			puts(files[count1]); 
			if (fexecl(args[0], 0L, files[count1], 0L) != 0) 
				{
				fputs("\nERROR: I am unable to use ",stdout);
				fputs(args[0], stdout);
				fputs(" to show ", stdout);
				puts(files[count1]);
				exit(1);
				}
			}
		puts("END OF PICTURES\n");
		}
	else 
		{
		fputs("\nSorry, there are no files with the extension ", stdout);
		puts(&extension[1]);
		}
}

 /*     -     -     -     -     -     -     -     -     -     -     -     */

/* compare 2 strings regardless of case returning 0 if a match */
int compare(s1, s2)
char *s1, *s2;
{
	for (; *s1 != '\0'; s1++, s2++)
		{
		if (*s1 == '#' && *(s1+1) == '?') /* match all */
			return (0);
		if (*s1 == '?') /* wildcard */
			continue;
		if(toupper(*s1) != toupper(*s2))
			return (1);
		}
	return (*s2);
}
