/*****************************************************************************
 * TeXClean:	Delete files generated by TeX, LaTeX, and BibTeX.
 *		Does not affect ".tex" files.
 *
 * Author:	Daniel J. Barrett.  Originally a UNIX shell script.
 *
 * Updates:
 *
 *	1.00	Original version.
 *	1.10	Now deletes texput.log and mfput.log.
 *		Added version string.
 *	1.11	Support for AmigaDOS "?" character to print a Usage
 *		message.
 *		Put dots in front of the suffix names in the Usage message.
 *
 * Someday...
 *
 *	Add wildcard support.
 *	But do it so the program is portable!
 *
 ****************************************************************************/

char *version = "$VER: TeXClean 1.11 23/11/92";

#include <stdio.h>
#include <string.h>
#include <exec/types.h>


#define	SUFFIX_DELIMITER	'.'
#define	EXISTS(filename)	!access(filename, 0)
#define	EQUAL(str1, str2)	!strcmp(str1, str2)


/* NEVER NEVER NEVER put "tex" in this ending list.
 * Always end with NULL. */

char *endList[] = { "aux", "bbl", "blg", "dvi", "idx", "lof", "log", "lot", 
		    "toc", NULL};

/* Other junk files to be deleted if they exist. */

char *dieList[] = { "ShowDVI.log", "texput.log", "mfput.log", NULL};

	
char *RemoveSuffix(char *filename);
BOOL SanityCheck(char *endList[]);
long DeleteMe(char *filename);
void TeXClean(char *filename, char *endings[]);
void Usage(char *prog, char *endings[], char *crap[]);

	
main(int argc, char *argv[])
{
	char **s;

	if (!SanityCheck(endList))
	{
		fprintf(stderr, "INTERNAL ERROR IN ENDLIST!!!!!!\n");
		exit(20);
	}

	if ((argc <= 1) || (EQUAL(argv[1], "?")))
		Usage(argv[0], endList, dieList);
	else
	{
		while (*(++argv))
		{
			TeXClean(RemoveSuffix(*argv), endList);
		}

		s = dieList;
		while (*s)
			(void) DeleteMe(*(s++));
	}

	exit(0);
}

	
/* Make sure "tex" is not in the ending list. */
BOOL SanityCheck(char *endList[])
{
	while (*endList)
	{
		if (EQUAL(*endList, "tex"))
			return(FALSE);
		endList++;
	}
	return(TRUE);
}


/* Print a usage message. */
void Usage(char *prog, char *endings[], char *crap[])
{
	fprintf(stderr, "%s removes TeX files with these endings:\n", prog);
	while (*endings)
		fprintf(stderr, " .%s", *(endings++));

	fprintf(stderr, "\nThe following files are also deleted if they"
			" exist:\n");
	while (*crap)
		fprintf(stderr, "\t%s\n", *(crap++));

	fprintf(stderr, "\nFiles ending with \".tex\" CANNOT be removed.\n");
	fprintf(stderr, "Usage: %s texfile1 texfile2 ...\n", prog);
}


/* Remove the stuff after and including the last SUFFIX_DELIMITER. */
char *RemoveSuffix(char *filename)
{
	char *here;

	here = rindex(filename, SUFFIX_DELIMITER);
	if (here)
		*here = '\0';
	return(filename);
}


/* Delete all files with the given prefix and a suffix in endings. */

void TeXClean(char *prefix, char *endings[])
{
	char filename[BUFSIZ];

	while (*endings)
	{
		(void) sprintf(filename, "%s.%s", prefix, *(endings++));
		(void) DeleteMe(filename);
	}
}


long DeleteMe(char *filename)
{
	long err;

	if (EXISTS(filename))
    	{

#ifdef DEBUG
		fprintf(stderr, "Pretend to delete \"%s\"\n", filename);
		if (0)	/* Never execute. */
#else
		if (!DeleteFile(filename))
#endif
		{
			err = IoErr();
			fprintf(stderr, "ERROR: can't delete %s\n",
				filename);
			fprintf(stderr,
				"Type \"fault %ld\" for more info.\n",
		    		err);
		}
		else
			printf("%s -- deleted\n", filename);
	}
	return(err);
}
