/* pr.c

	simple print file utility by: Bob Leivian

	pr [-chn] file [file2...]

	This program will print a list of files in a background cli

	This program will accept Un*x style wildcards, not Amigados
	o * matches any substring
	o ? matches any single character

	This program has options to...
	o print line numbers (-n)
	o print a header before each file (-h)
	o confirm all wild card matches individulally (-c)
		<CR> means yes, I want that file printed
		! means yes for this one and all remaining
		anything else means no don't print the file
	o options may be grouped: i.e. -chn, or separated: i.e. -h -n -c


*/

#include <stdio.h>
#include <ctype.h>

int control = 0;
int confirm = 0;


/* Amiga lib execute a cli command */
long Execute();
char *scdir();
char *gets();

main(argc, argv)
int argc;
char *argv[];
{
	int i;
	char *p;
	FILE *out;
	char buf[2048];		/* command buffer for print */
	char name[30];		/* name of file that we are using this pass */
	char answer[10];
	char append[4];
	char c;
	int accepted;
	long result;
	int once = 0;

	printf("pr copyright(c) 1986 by Robert H. Leivian\n");
	if (argc < 2) {
		printf("usage: pr -chnpwl:<control string> file1 file2...\n");
		printf("use pr -? for more help\n");
		exit(27);
	}

	/* set up to run the simple print in another cli */
	strcpy(buf, "RUN print ");
	strcpy(append, " -x");

	while (argv[1] != NULL) {

		/* process options if any */
		while (*argv[1] == '-') {
			p = (char *) &*argv[1];

			p++;		/* point to the option chars */
			do {
				switch (*p) {
				case '^':		/* just pass these options to print */
				case 'n':
				case 'h':
				case 'w':
				case 'p':
				case 'l':
					append[2] = *p;
					strcat(buf, append);
					break;

				case ':':		/* print the control char string first */
					strcat(buf, " -:^j");/* include a char for the line eater*/
					strcat(buf, p); /* get the control string goto next opt */
					goto next;

				case 'c':		/* confirm any templates */
					confirm++;
					break;

				case '?':
printf("'pr' prints multiple files in background with serval options\n\n");
				goto help;

				default:
printf("'%c' is an invalid option, ", *p);
help:
printf("The vaild options are...\n");
printf("n - line numbers, h -headers, ^ - print control chars as ^X\n");
printf("l - letter quailty, w - wide (132 char mode), p - proportional\n");
printf("c - confirm wild cards, :<string> - print string first\n");
				exit(0);
				}
				p++;
			} while (*p);
	next:
			argc--;
			argv++;
		}

		strcat(buf, " ");

		/* must be a file name */
		strcpy(name, argv[1]);

		if (index(name, '*') || index(name, '?')) {

			/* a template was given, expand it */
			i = 0;
#ifdef DEBUG
printf("expanding template: %s\n", name);
#endif
			while (((p = scdir(name)) != NULL) && ++i < 100) {

				/* if confirm ask him for each file */
				if (confirm) {
again:
					printf("confirm '%s'? ", p);
					gets(answer);
					c = answer[0];
					switch (c) {
					case '!':
						/* if he answers ! then accept all remaining */
						confirm = 0;
						accepted = 1;
						break;

					case '\0':
					case 'y':
					case 'Y':
						/* if yes then include this name */
						accepted = 1;
						break;

					case 'N':
					case 'n':
						accepted = 0;
						break;

					default:
						printf("valid answers are Yy<ret> - yes do print\n");
						printf("  Nn - no don't print this one\n");
						printf("  ! - yes print this one and all remaining\n");
						goto again;
					}
				} else
					/* if not confirming then match all  items */
					accepted = 1;

				/* go ahead and print this one */
				if (accepted) {
					strcat(buf, p);
					strcat(buf, " ");
					printf("spooling: '%s'\n", p);
				}
			}
		} else {
			printf("spooling: %s\n", name);
			strcat(buf, name);
			strcat(buf, " ");
		}

		argv++;
	}
#ifdef DEBUG
printf("command...\n%s\n", buf);
#endif

	/* now just run the print in another CLI */
	result = Execute(buf, 0L, 0L);
	return result;
}
