/*
 * read \_bbs\dirs.txt and parameter (batch file) 
 * for each line in there.
 */

#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>



void
help(void) {

	fprintf(stderr,
			  "makemenu: build core of menu based on dirs.txt\n");
	fprintf(stderr,
		"usage: makemenu <starting area #> <dirs.txt path> -xxx> <out file>\n");
	fprintf(stderr, "-xxx == -n for normal, -a for ansi, -g for graphics.\n");
	exit(1);
}

void _Cdecl
main(int argc, char *argv[]) {
	char buf[200];
	char output[200];
	char batchfile[80];
	char *p;
	int count;
	FILE *dirsfile;
	int i;
	int do_normal = 0;
	int do_ansi = 0;
	int do_graphic = 0;
	
	fprintf(stderr,
		  "Makemenu.  Walnut Creek CDROM, copyright 1993.\n");

	if (argc < 4)
		help();

	if (0 == (count = atoi(argv[1])))
		help();

 	if (NULL == (dirsfile = fopen(argv[2], "r"))) {
		fprintf(stderr, "failed opening directory listing '%s'\n   %s\n",
				  argv[2]);
		help();
	}

	if (0 == strcmp(strlwr(argv[3]), "-n"))
		++do_normal;
	else if (0 == strcmp(strlwr(argv[3]), "-a"))
		++do_ansi;
	else if (0 == strcmp(strlwr(argv[3]), "-g"))
		++do_graphic;
	else
		help();

	
	while (NULL != fgets(buf, 199, dirsfile)) {

		/* wack off trailing spaces */
		i = strlen(buf) - 1;
		while (i >= 0 && isspace(buf[i])) {
			buf[i] = '\0';
			--i;
		}
		
		p = strchr(buf, ' ');
		if (!p) {
			fprintf(stderr, "something wrong with line `%s'\n", buf);
			help();
		}
		while (isspace(*p))
				 ++p;

		sprintf(output, "%3d %-32s", count++, p);
		
		if (NULL == fgets(buf, 199, dirsfile)) {
			if (do_ansi)
				printf("[44m[1;37m³");
			if (do_graphic)
				printf("³");
			printf("%s %3s %32s", output, " ", " ");
			if (do_ansi)
				printf("³[1;32m[0;34m");
			if (do_graphic)
				printf("³");
			printf("\n");
			exit(0);
		}
		
		/* wack off trailing spaces */
		i = strlen(buf) - 1;
		while (i >= 0 && isspace(buf[i])) {
			buf[i] = '\0';
			--i;
		}
		
		p = strchr(buf, ' ');
		if (!p) {
			fprintf(stderr, "something wrong with line `%s'\n", buf);
			help();
		}
		while (isspace(*p))
				 ++p;
		
		if (do_ansi)
			printf("[44m[1;37m³");
		if (do_graphic)
				printf("³");
		printf("%s %3d %-32s", output, count++, p);
		if (do_ansi)
			printf("³[1;32m[0;34m");
		if (do_graphic)
				printf("³");
		printf("\n");
	}
}
