#ifndef lint
static char *sccsid = "@(#)munge.c	1.1 (UKC) 25/11/87";
#endif  lint
/*
 *	munge: convert width tables for ditroff font tables
 *	from first argument size to size at 10 point such that
 *	troff munging them back to the original size gives the correct widths
 *
 *	Martin Guy, UKC, last hacked November 1987
 */

#include <stdio.h>
#include <ctype.h>	/* for isdigit() */

char *getlogin();
char *ctime();
long time();		/* no see */
double atof();

main(argc,argv)
char **argv;
{
	register int from;
	char buf[256];
	char *filename = NULL;		/* name of input file, if supplied */
	long clock;			/* for ident message */

	switch (argc) {
	case 2:
		break;
	
	case 3:
		/* 2nd argument is the file name to read */
		if (freopen(argv[2], "r", stdin) == NULL) {
			/* Couldn't open that file, chum. */
			fputs("munge: ", stderr);
			perror(argv[2]);
			exit(1);
		}
		filename = argv[2];
		break;

	default:
		fputs("Usage: munge realsize [inputfile]\n", stderr);
		exit(1);
	}
	from = atoi(argv[1]);

	printf("# Munged version ");
	if (filename != NULL) printf("of \"%s\" ", filename);
	clock = time(0L);	/* get time of day */
	printf("by %s on %s", getlogin(), ctime(&clock));
	/* ctime appends a newline */

	/* Copy up to and including the "charset" line */
	do {
		if (fgets(buf, sizeof(buf), stdin) == NULL) {
			fputs("munge: Couldn't find \"charset\" line.\n", stderr);
			exit(1);
		}
		fputs(buf, stdout);
	} while (strcmp(buf, "charset\n"));

	while (fgets(buf, sizeof(buf), stdin) != NULL) {
		register char *cp;
		register i;
		cp = buf;
		while (putchar(*cp++) != '\t');
		switch(*cp) {
		case '"':
			break;
		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			i = 0;
			do {
				i = i*10 + *cp++ - '0';
			} while (isdigit(*cp));
			printf("%d", (10*i+from-6)/from);
			break;
		}
		while (*cp!='\0') putchar(*cp++);
	}
}
