/* RETIRE   Remove entries from User-Startup                         ***RGR***
                                                                      3-apr-95
   Author:  Ralf Gruner, An der Sense 5a, D-02779 Großschönau, Germany.

   MANX Aztec C 5.2:
   cc retire
   ln retire -lc
*/

//#define DEBUG

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define EOL 10

char *filename1="S:User-Startup", *filename2="RAM:User-Startup.temp";
FILE *infile=0, *outfile=0;
char *help="\nRETIRE removes entries from User-Startup.\nUsage: retire <Application 1> [<Application 2> ... [<Application n>]...]\n\n";
char *version="$VER: RETIRE 1.0 (03.04.95)";
char *application;
int i,j,k,l;
char line[256], *lineptr;
int c=0;

long ftell();

/*************/
void CloseAndExit(char *Msg)
{
	if (Msg!=0L)	puts(Msg);
	if (infile)	fclose(infile);
	if (outfile)	fclose(outfile);
	remove(filename2);
	if (Msg!=0L)	exit(10);
	exit(0);
}

/*************/
void ReadTextLine(void)
{
	for(k=0; k<256; k++)
		line[k]='\0';	// clear line buffer

	k=0;
	while((c=fgetc(infile))!=EOF)	// read line
	{
		line[k++]=c;

		#ifdef DEBUG
			printf("%c",c);
		#endif

		if(c==EOL || c==EOF || k>=256) break;
	}
}

/*************/
main(int argc, char *argv[])
{
	long beginpos;
	int ok=0;

	if (*argv[1] == '?')
	{
		printf("\nRETIRE v%s   Author: Ralf Gruner, Großschönau, Germany.\n",version+13);
		printf(help);
		exit(0);
	}

	if (argc < 2)
	{
		printf("\nMissing application name.\n");
		printf(help);
		CloseAndExit("");
	}
	else
	{
		for(i=1; i<=argc-1; i++)
		{
			#ifdef DEBUG
				printf("\nPass %d: %s\n",i,argv[i]);
			#endif

			if(!(infile=fopen(filename1,"r")))
			{
				printf("%s does not exist.\n",filename1);
				CloseAndExit(0L);
			}

			if(!(outfile=fopen(filename2,"w")))
			{
				printf("\nCould not open %s.\n",filename2);
				CloseAndExit("");
			}

			while((c=fgetc(infile))!=EOF)	// copy s:user-startup into ram-disk
			{
				if(fputc(c, outfile)==EOF)
					CloseAndExit("\nError while writing.\n");
			}

			fclose(infile);
			fclose(outfile);

			if(!(infile=fopen(filename2,"r")))
			{
				printf("\nCould not open %s.\n",filename2);
				CloseAndExit("");
			}

			if(!(outfile=fopen(filename1,"w")))
			{
				printf("\nCould not open %s for writing.\n",filename1);
				CloseAndExit("");
			}

			application=argv[i];	// application name
			c=0;
			ok=0;

			while(c!=EOF)
			{
				beginpos=ftell(infile);
				ReadTextLine();
				while(ok==1 && line[0]==EOL && c!=EOF)
				{
					beginpos=ftell(infile);
					ReadTextLine();	// remove empty lines after removed entry
				}
				ok=0;
				if(strnicmp(&line[0],";BEGIN ",7)==0)
				{
					lineptr=&line[7];
					while(*lineptr==' ')
						lineptr++;	// skip spaces
					if(strnicmp(lineptr,application,strlen(application))==0)
					{
						lineptr+=strlen(application);
						while(*lineptr==' ')
							lineptr++;	// skip spaces
						if(*lineptr==EOL)
						{
							ok=1;
							while(strnicmp(&line[0],";END ",5)!=0 && c!=EOF)
							{
								ReadTextLine();
								if(c==EOF)
								{
									ok=0;
									break;
								}
							}
							if(ok==1)
							{
								// found ;END
								lineptr=&line[5];
								while(*lineptr==' ')
									lineptr++;	// skip spaces
								if(strnicmp(lineptr,application,strlen(application))==0)
								{
									// found name
									lineptr+=strlen(application);
									while(*lineptr==' ')
										lineptr++;	// skip spaces
									if(*lineptr==EOL)
									{
										ok=1;
									}
									else
									{
										ok=0;
									}
								}
								else
									ok=0;
							}
							if(ok==0)
							{
								printf("\nError in structure of S:User-Startup:\n;BEGIN %s  does not match  %s\n",application,&line[0]);
								fseek(infile,beginpos,0);
								ReadTextLine();	// if END not valid, read BEGIN again
							}
							if(ok==1)
							{
								#ifdef DEBUG
									printf("(*** removed.***)\n");
								#endif
								printf("%s retired.\n",application);
								continue;
							}
						}
					}
				}

				l=0;
				while(l<k)	// write line
				{
					c=line[l++];
					if(fputc(c, outfile)==EOF)
						CloseAndExit("\nError while writing.\n");
				}
			}

			fclose(infile);
			fclose(outfile);
		}
	}
CloseAndExit(0L);
}

