/* This is a program to edit a list of the names and account information 
   of people who use the ocean engineering Athena Cluster */

/* Project History

   Under construction July - September 1989
   Version 1.1 Released  14 September 1989  -- A,C,L,Q
   Version 1.2 Released  18 September 1989  -- S
   Version 1.3 Initiated  6 October 1989
               Released  10 October 1989    -- D, mod A0  
   Version 1.4 Initiated 14 January 1990
               Released  14 January 1990    -- Fixed lowercase & locker names
	                                    -- Fixed option N 
					    -- Added to A for lockers
   Version 2.1 Initiated 1 February 1990    -- Added the P option, but the
               Released 1 February 1990        tabs still need work.
   Version 2.2 Initiated 22 March 1990      -- Fixing up the P option, fixing PH
                                               and working on output.
*/

/* To do:
   - File routine to sort people by machine name 
     or to print files with out SSN's (2.3)
   - Clean up some of the logical bugs in the menu drivers of 2.1 (2.2)
   - Fix the tabs in the menu of P (2.2)
   - Fix the bug that doubles up the list of peoples names in P and 
     toggle on/off the debug file and give and option for its loction. (2.2)
*/


#include <stdio.h>
#define LINELEN 80
#define CHARLEN 1
#define WORD 14

main()

{

  /* Variable Declarations */

  int n;
  int i;
  int j;
  int k;
  int distup;
  int current;
  int numentries;
  int cl;
  int lowestnum;
  int cycle;
  int justlisted;
  int justlistedP;
  int debugint1;
  int numberNFS;
  int count[4];
  int numnames[4];
  int numberHeadings;

  int Maxlen1;
  int Maxlen2;
  int Maxlen3;
  int Maxlen4;
  int columns;
  int pages;

  float number[1];

  char line[LINELEN];
  char response[LINELEN];
  char responseD[LINELEN];
  char responseD2[LINELEN];
  char responseP[LINELEN];
  char responsePH[LINELEN];
  char morestop[LINELEN];

  char firstname[LINELEN];
  char lastname[LINELEN];
  char middlename[LINELEN];
  char username[LINELEN];
  char SSN[LINELEN];

  char nameline[LINELEN*200];
  char useridline[LINELEN*200];
  char usernamearray[LINELEN*200];
  char userNFS[LINELEN*200];
  char sortline[LINELEN*200];
  char studidarray[LINELEN*200];
  char blankline[LINELEN];
  char newline[LINELEN];
  char zline[LINELEN];
  char currentlowestsortname[LINELEN];
  char currentlowestname[LINELEN];
  char currentlowestuserid[LINELEN];
  char server[LINELEN];
  char Fullname[LINELEN*200];

  char listcommand[LINELEN];
  char deletecommand[LINELEN];
  char movecommand[LINELEN*2];
  char hesiodcommand[LINELEN];
  char scribe1command[LINELEN];
  char scribe2command[LINELEN];
  char printcommand[LINELEN];

  char outputfile[LINELEN];
  char tempfile[LINELEN];
  char tempfile1[LINELEN];
  char tempfile2[LINELEN];
  char tempfile3[LINELEN];
  char inputfile[LINELEN];
  char scribefile[LINELEN];
  char PSfile1[LINELEN];
  char PSfile2[LINELEN];
  char trashfileP[LINELEN];

  char outputline[LINELEN];
  char firstoutputline[LINELEN];
  char secondoutputline[LINELEN];
  char hesoutput[LINELEN];
  char Heading[LINELEN*10];

  char NFSname[WORD*4];
  char FromDate[WORD*4];
  char ToDate[WORD*4];
  char FromTime[WORD*4];
  char ToTime[WORD*4];

  FILE *in;
  FILE *out;
  FILE *temp;
  FILE *hesout;
  FILE *heslist;
  FILE *scribe;
  FILE *trashP;

  strcpy(outputfile,"/mit/13.cluster/Routines.dir/namesbin.dir/namelist");
  strcpy(scribefile,"/mit/13.cluster/Routines.dir/namesbin.dir/NFSlist.mss");
  strcpy(PSfile1,"NFSlist.PS");
  strcpy(PSfile2,"/mit/13.cluster/Routines.dir/namesbin.dir/NFSlist.PS");

  strcpy(Heading+1*LINELEN,"@center(@p(The following user accounts/lockers))");
  strcpy(Heading+2*LINELEN,"@center(@p(Will be unavailable due to pending))");
  strcpy(Heading+3*LINELEN,"@center(@p(shutdowns of their fileservers))");
  
  numberHeadings=3;
  numberNFS=0;
  response[0]=32;

  while (response[0] != 'Q' && response[0] != 'q')

    {


      /***********************************************************************/
      /* Main Menu */
      
      system("clear");
      printf("\n            Welcome to Debase Version 2.1\n");
      printf("       =============== Main Menu ==============\n");
      printf("\n");

      printf("    Current filename used:\n%s\n\n", outputfile);

      printf("    A\t Append the current file\n");
      printf("    C\t Change the current file\n");
      printf("    E\t Edit the current file\n");
      printf("    L\t Look at the current file\n");
      printf("     \t Hit return when finished viewing\n");
      printf("    N\t Associate users with NFS machines\n");
      printf("    P\t Print a listing of affected NFS users\n");
      printf("    Q\t Quits the program\n");
      printf("    S\t Sorts the current file\n\n");
      justlisted=0;
      fgets(response,LINELEN,stdin);
      
      /* End Main Menu */
      /***********************************************************************/
      
      /*Show the file*/
      if (response[0] == 'L' || response[0] == 'l')
	{
	  strcpy(listcommand,"more ");
	  strcat(listcommand,outputfile);
	  system(listcommand);
	  fgets(morestop,LINELEN,stdin);
	  justlisted=1;
	}

      /* Print out a listing of affected users */
      /* This section was started (twice) on 1 February, 1989 */
      if (response[0] == 'P' || response[0] == 'p')
	{
	  while (responseP[0] != 'Q' && responseP[0] != 'q')
	    {
	      system("clear");
	      printf("\n                Debase Version 2.1\n");
	      printf("       ============= Print Menu ==============\n");
	      printf("\n");
	      
	      printf("    Current manuscript filename used:\n%s\n", scribefile);
	      printf("    Current postscript filename used:\n%s\n\n", PSfile2);
	      printf("       -- The file will read as follows -- \n\n");

	      printf("%s\n",Heading+1*LINELEN);
	      printf("%s\n",Heading+2*LINELEN);
	      printf("%s\n\n",Heading+3*LINELEN);

	      printf("Machine: ");
	      for(i=1;i<=numberNFS;i++)
		{printf("\t%s",NFSname+i*WORD);}
	      printf("\nFrom    :");
	      for(i=1;i<=numberNFS;i++)
		{printf("\t%s",FromDate+i*WORD);}
	      printf("\n         ");
	      for(i=1;i<=numberNFS;i++)
		{printf("\t%s",FromTime+i*WORD);}
	      printf("\nTo      :");
	      for(i=1;i<=numberNFS;i++)
		{printf("\t%s",ToDate+i*WORD);}
	      printf("\n         ");
	      for(i=1;i<=numberNFS;i++)
		{printf("\t%s",ToTime+i*WORD);}
	      printf("\n\n");
	      
	      printf("   Typical Command Pattern: N - E - C - G - P\n\n");
	      printf("   C\t Collect data for the file\n");
	      printf("   E\t Enter names and down times of NFS machines\n");
	      printf("   G\t Generate a scribe manuscript file\n");
	      printf("   H\t Change the heading lines\n");
	      printf("   N\t Specify number of NFS Machines Affected\n");
	      printf("   P\t Prints out a completed manuscript file\n");
	      printf("   Q\t Returns to the Main Menu\n\n");
	      printf("   R\t Print a listing of affected NFS users\n");
      
	      justlistedP=0;
	      fgets(responseP,LINELEN,stdin);
	      
	      /* Edits machine names and down times */
	      if (responseP[0] == 'E' || responseP[0] == 'e')
		{
		  for(i=1;i<=numberNFS;i++)
		    {
		      printf("All Entries in this section must be under 14 characters\n");
		      printf("|--|--|--|--14");
		      printf("Enter the name of an NFS machine\n");
		      gets(NFSname+i*WORD,WORD,stdin);
		      printf("Enter date the machine will go down\n");
		      printf("  List the date as 01/01/90\n");
		      gets(FromDate+i*WORD,WORD,stdin);
		      printf("Enter the time the machine will go\n");
		      printf("  down on %s\n",FromDate+i*WORD);
		      gets(FromTime+i*WORD,WORD,stdin);
		      printf("Enter date the machine will come up\n");
		      printf("  List the date as 01/01/90\n");
		      gets(ToDate+i*WORD,WORD,stdin);
		      printf("Enter the time the machine will come\n");
		      printf("  up on %s\n",ToDate+i*WORD);
		      gets(ToTime+i*WORD,WORD,stdin);
		    }
		}

	      /* Edits the headings  */
	      else if (responseP[0] == 'H' || responseP[0] == 'h')
		{
		  printf("Current number of heading lines: %d\n",numberHeadings);
		  printf("How many would you like [Recommend 3]\n?");
		  scanf("%d",&numberHeadings);
		  fgets(responsePH,LINELEN,stdin);
		  for(i=1;i<=numberHeadings;i++)
		    {
		      printf("All Entries in this section must be under 80 characters\n");
		      printf("%s\nEnter the number %d heading line\n",Heading+i*LINELEN,i);
		      printf("Do you wish to change it? [y/n]\n");
		      fgets(responsePH,LINELEN,stdin);
		      if(responsePH[0] == 'y' || responsePH[0] == 'Y' || responsePH[0] == '1')
			{
			  printf("Enter the line below\n");
			  gets(Heading+i*LINELEN,LINELEN,stdin);
			}
		    }
		}

	      /* Scribes and prints out a completed manuscript file */
	      else if (responseP[0] == 'P' || responseP[0] == 'p')
		{
		  printf("Please wait, the file is being processed...\n");
		  /* scribe the file */
		  strcpy(scribe1command,"scribe ");
		  strcpy(scribe2command," > garbage");
		  strcat(scribe1command,scribefile);
		  strcat(scribe1command,scribe2command);
		  strcat(scribe1command,"\n");
		  system(scribe1command);
		  /* print the file */
		  printf("   .... The file is now being printed ...\n");
		  strcpy(printcommand,"lpr -Poe -h ");
		  strcat(printcommand, PSfile1);
		  strcat(printcommand, "\n");
		  system(printcommand);
		  /* copy the file to the namesbin directory */
		  strcpy(movecommand,"cp ");
		  strcat(movecommand, PSfile1);
		  strcat(movecommand, " ");
		  strcat(movecommand, PSfile2);
		  strcat(movecommand, "\n");
		  system(movecommand);
		  /* delete the old PS file */
		  strcpy(deletecommand,"rm ");
		  strcat(deletecommand, PSfile1);
		  strcat(deletecommand, "\n");
		  system(deletecommand);
		}

	      /* Collect Data for the file */
	      else if (responseP[0] == 'C' || responseP[0] == 'c')
		{
		  in = fopen(outputfile,"r");
		  /* Read in the file into an array */
		  while(fgets(newline,LINELEN,in))
		    {
		      current = current+1;
		      strcpy(nameline+current*LINELEN,newline);
		      strstrip(newline);
		      strcpy(sortline+current*LINELEN,newline); 
		      fgets(useridline+current*LINELEN,LINELEN,in);
		      strextract(useridline+current*LINELEN,usernamearray+current*LINELEN,1,LINELEN);
		      strextract(useridline+current*LINELEN,studidarray+current*LINELEN,2,LINELEN);
		      fgets(blankline,LINELEN,in);
		    }      	  
		  numentries=current;
		  if (in!=stdin)
		    fclose(in);
		  /* Cycle through the file looking for the name */
		  /*printf("Entering the realm of looking for matching names\n");*/
		  distup=0;
		  for(i=1;i<=numberNFS;i++)
		    {
		      /*printf("Outer Loop, cycling through NFS servers, number: %d\n",i);*/
		      count[i]=0;
		      for(current=1;current<=numentries;current++)
			{
			  strextract(useridline+current*LINELEN,server,3,LINELEN);
			  if(strcmp(server,NFSname+WORD*i)==0)
			    {
			      count[i]=count[i]+1;
			      strcpy(Fullname+distup+count[i]*LINELEN,sortline+current*LINELEN);
			    }
			}
		      numnames[i]=count[i];
		      for(k=1;k<=i;k++)
			{
			  distup=distup+numnames[k]*LINELEN;
			}
		    }
		  
		  /* Debugging output of the names generated above */
		  strcpy(trashfileP,"debugP.msg");
		  trashP = fopen(trashfileP,"w+");
		  fprintf(trashP,"Number of NFS machines: %d\n",numberNFS);
		  distup=0;
		  for(i=1;i<=numberNFS;i++)
		    {
		      fprintf(trashP,"Machine: %s\n",NFSname+WORD*i);
		      fprintf(trashP,"======================\n");
		      for(j=1;j<=numnames[i];j++)
			{
			  fprintf(trashP,"%s\n",Fullname+distup+j*LINELEN);
			}
		      fprintf(trashP,"\n");
		      for(k=1;k<=i;k++)
			{
			  distup=distup+numnames[k]*LINELEN;
			}
		    }
		}
	      
	      if (responseP[0] == 'R' || response[0] == 'R')
		{
		  in = fopen(outputfile,"r");
		  scribe = fopen(scribefile,"w+");
		  fprintf(scribe,"@device(postscript)\n");
		  fprintf(scribe,"@style(font allpostscriptfonts, size 12, spacing -0.8 lines, topmargin, 0.5 inches, indent 0.0 inches,leftmargin 0.75 inches, rightmargin 0.5 inches)\n");
		  fprintf(scribe,"@LibraryFile(landscape)\n");
		  fprintf(scribe,"@begin(transparent, font souvenir, size 35)\n");
		  fprintf(scribe,"@blankspace(1 line)\n");
		  fprintf(scribe,"%s\n\n",Heading+1*LINELEN);
		  fprintf(scribe,"%s\n\n",Heading+2*LINELEN);
		  fprintf(scribe,"%s\n\n",Heading+3*LINELEN);
		  fprintf(scribe,"@end(transparent)\n@blankspace(2 lines)\n\n");
		  fprintf(scribe,"@begin(transparent, font souvenir, spacing -0.2 lines, size 12, columns 2, boxed, columnwidth 4.5 inches, columnmargin 0.75 inches)\n");
		  fprintf(scribe,"@tabset(0.75 inches)");
		  /* Read in the file into an array */
		  while(fgets(newline,LINELEN,in))
		    {
		      current = current+1;
		      strcpy(nameline+current*LINELEN,newline);
		      strstrip(newline);
		      strcpy(sortline+current*LINELEN,newline); 
		      fgets(useridline+current*LINELEN,LINELEN,in);
		      strextract(useridline+current*LINELEN,usernamearray+current*LINELEN,1,LINELEN);
		      strextract(useridline+current*LINELEN,studidarray+current*LINELEN,2,LINELEN);
		      fgets(blankline,LINELEN,in);
		      for(i=1;i<=numberNFS;i++)
			{
			  count[i]=0;
			  strextract(useridline+current*LINELEN,server,3,LINELEN);
			  if(strcmp(server,NFSname+WORD*i)==0)
			    {
			      fprintf(scribe,"@begin(group)\n");
			      fprintf(scribe,"* %s\n",Fullname+distup+j*LINELEN);
			      fprintf(scribe,"\t@\From %s on %s to %s on %s\n",FromDate+i*WORD,FromTime+i*WORD,ToDate+i*WORD,ToTime+i*WORD);
			      fprintf(scribe,"@end(group)\n\n");
			      fprintf(scribe,"@blankspace(0.35 lines)\n\n");

			      count[i]=count[i]+1;
			      strcpy(Fullname+distup+count[i]*LINELEN,sortline+current*LINELEN);
			    }
			}
		      
		    }      	  
	      numentries=current;
	      if (in!=stdin)
		fclose(in);
	      if (scribe!=stdin)
		fclose(scribe);
	      /* Cycle through the file looking for the name */
	      /*printf("Entering the realm of looking for matching names\n");*/
	  
	}

	      /* Generate a scribe Manuscript File */
	      else if (responseP[0] == 'G' || responseP[0] == 'g')
		{
		  scribe = fopen(scribefile,"w+");
		  fprintf(scribe,"@device(postscript)\n");
		  fprintf(scribe,"@style(font allpostscriptfonts, size 12, spacing -0.8 lines, topmargin, 0.5 inches, indent 0.0 inches,leftmargin 0.75 inches, rightmargin 0.5 inches)\n");
		  fprintf(scribe,"@LibraryFile(landscape)\n");
		  fprintf(scribe,"@begin(transparent, font souvenir, size 35)\n");
		  fprintf(scribe,"@blankspace(1 line)\n");
		  fprintf(scribe,"%s\n\n",Heading+1*LINELEN);
		  fprintf(scribe,"%s\n\n",Heading+2*LINELEN);
		  fprintf(scribe,"%s\n\n",Heading+3*LINELEN);
		  fprintf(scribe,"@end(transparent)\n@blankspace(2 lines)\n\n");
		  fprintf(scribe,"@begin(transparent, font souvenir, spacing -0.2 lines, size 12, columns 4, boxed, columnwidth 2.5 inches, columnmargin 0.5 inches)\n");
		  distup=0;
		  Maxlen1=9-(numberHeadings-3)*4;
		  Maxlen2=Maxlen1+6;
		  Maxlen3=19;
		  Maxlen4=Maxlen1+6;
		  columns=0;
		  for(i=1;i<=numberNFS;i++)
		    {
		      columns=columns+1;
		      if (columns <= 4)
			{
			  if (count[i] > Maxlen1)
			    {
			      columns=columns+((count[i]-Maxlen1)/Maxlen2)+1;
			      pages=1;
			    }
			}
		      else
			{
			  if (count[i] > Maxlen3)
			    {
			      columns=columns+((count[i]-Maxlen1)/Maxlen2)+1;
			      pages=1;
			    }
			}
		    }
		  if (columns > 4)
		    {
		      
		  for(i=1;i<=numberNFS;i++)
		    {
		      fprintf(scribe,"@begin(transparent, font souvenir, size 18)\n\n@b<\n");
		      fprintf(scribe,"%s",NFSname+i*WORD);
		      fprintf(scribe,">\n@end(transparent)\n\n");
		      fprintf(scribe,"@bar(0)\n\n");
		      fprintf(scribe,"@blankspace(1 line)\n");
		      fprintf(scribe,"These lockers and accounts will be down from:\n\n");
		      fprintf(scribe,"%s on %s\n\n",FromTime+i*WORD,FromDate+i*WORD);
		      fprintf(scribe,"To %s on %s\n\n",ToTime+i*WORD,ToDate+i*WORD);
		      fprintf(scribe,"@bar(0)\n\n");
		      fprintf(scribe,"@blankspace(1 line)\n");
		      for(k=1;k<i;k++)
			{distup=distup+numnames[k]*LINELEN;}
		      for(j=1;j<=numnames[i];j++)
			{
			  fprintf(scribe,"@begin(group)\n");
			  fprintf(scribe,"* %s\n",Fullname+distup+j*LINELEN);
			  fprintf(scribe,"@end(group)\n\n");
			  fprintf(scribe,"@blankspace(0.35 lines)\n\n");
			}
		  
		      if(i!=4)
			{fprintf(scribe,"@newcolumn()\n\n");}
		    }	  
		  fprintf(scribe,"@end(transparent)\n");
		  if (scribe!=stdout)
		    fclose(scribe);
		}
		
	      /* Enters number of NFS Servers Affected */
		  else if (responseP[0] == 'N' || responseP[0] == 'n')
		{
		  printf("Enter number of NFS servers affected\n");
		  j=1;
		  /* getflta(number[1],i,j);      */
		  /* numberNFS=number[1];         */
		  /*fgets(numchar,CHARLEN,stdin); */
		  /*stofa(numchar,numberNFS,1);   */
		  scanf("%d",&numberNFS);
		}
		}
	    }

	}


      /* Actually this section will append NFS names to the ends */
      /* of the data-base second lines */
      /* Fixed the error that required quitting after each use 
	 (unclosed file debase3.tmp), on 14 January 1990 */

      if (response[0] == 'N' || response[0] == 'n')
	{

	  /* Extract usernames into array */
	  strcpy(tempfile1,"debase1.tmp");  /* Single line ........ hesout  */
	  strcpy(tempfile2,"debase2.tmp");  /* One liners ......... heslist */
	  strcpy(tempfile3,"debase3.tmp");  /* Revised Database ... temp    */
/*	  heslist=fopen(tempfile2,"w+"); */ /* One liners */
	  temp=fopen(tempfile3,"w+");       /* Revised Database */
	  strcpy(inputfile,outputfile);    
	  in=fopen(inputfile,"r+");
	  
	  /* Read input file */
	  current = 0;
	  while (fgets(newline,LINELEN,in))
	    {
	      current = current+1;
	      strcpy(nameline+current*LINELEN,newline);
	      strstrip(newline);
	      strcpy(sortline+current*LINELEN,newline); 
	      fgets(useridline+current*LINELEN,LINELEN,in);
	      strextract(useridline+current*LINELEN,usernamearray+current*LINELEN,1,LINELEN);
	      strextract(useridline+current*LINELEN,studidarray+current*LINELEN,2,LINELEN);
	      fgets(blankline,LINELEN,in);
	    }
	  numentries=current;
	  if (in!=stdin)
	    fclose(in);
	  for (current=1; current <= numentries; ++current)
	    {
	      strcpy(hesiodcommand,"hesinfo ");
	      strcat(hesiodcommand,usernamearray+current*LINELEN);
	      strcat(hesiodcommand," filsys > debase1.tmp");
	      system(hesiodcommand);
	      
	      hesout=fopen(tempfile1,"r+");	     
	      fgets(hesoutput,LINELEN,hesout);
	      strextract(hesoutput,userNFS+current*LINELEN,3,LINELEN);
	      strcpy(outputline,nameline+current*LINELEN);

	      strcat(outputline,"\t");
	      strcat(outputline,userNFS+current*LINELEN);
	      strcat(outputline,"\n");
/*	      fprintf(heslist,"%s\n",outputline); */
	      
	      strcpy(firstoutputline,nameline+current*LINELEN);
	      strcpy(secondoutputline,usernamearray+current*LINELEN);
	      strcat(secondoutputline,"\t");
	      strcat(secondoutputline,studidarray+current*LINELEN);
	      strcat(secondoutputline,"\t");
	      strcat(secondoutputline,userNFS+current*LINELEN);
	      strcat(secondoutputline,"\n\n");
	      fprintf(temp,"%s%s",firstoutputline,secondoutputline);
	      if (hesout!=stdin)
		fclose(hesout);
	    }
/*	  if (heslist!=stdin)
	    fclose(heslist); */
	  if (temp!=stdin)
	    fclose(temp);
	  
	  /* Handle the Files */
	  /* Delete Input File */
	  strcpy(deletecommand,"rm ");
	  strcat(deletecommand,inputfile);
	  system(deletecommand);
	  /* Move Output File to Input Filename */
	  strcpy(movecommand,"mv ");
	  strcat(movecommand,tempfile3);
	  strcat(movecommand," ");
	  strcat(movecommand,inputfile);
	  system(movecommand);
	}	  
      
            



      
      
      
      
      
      /*Sort the file*/
      if (response[0] == 'S' || response[0] == 's')
	{
	  
	  /* This is a subroutine to sort out a standard Debase text file into 
	     alphabetical order by the users last name */
	  /* Routine initiated on 17 September 1989 */
	  /* Modified for lowercase letters 14 January 1990 */
	  
	  /* Open input and output files */
	  strcpy(inputfile,outputfile);
	  strcpy(tempfile,"debase.tmp");
	  out = fopen(tempfile,"w+");
	  in = fopen(inputfile,"r+");
	  
	  /* Read input file */
	  current = 0;
	  while (fgets(newline,LINELEN,in))
	    {
	      /*      printf("               Loop Entered");*/
	      current = current+1;
/*	      printf("               Reading entry %d\n", current);          */
/*	      printf("  Entry number %d (newline & nameline) is\n",current); */
/*	      printf("%s",newline);                                          */
	      strcpy(nameline+current*LINELEN,newline);
	      strstrip(newline);                        /* Strip the string  */
	      strcpy(sortline+current*LINELEN,newline); /* Make the sortline */
	      fgets(useridline+current*LINELEN,LINELEN,in);
/*	      printf("  The next line read (useridline) is\n");              */
/*	      printf("%s",useridline+current*LINELEN);                       */
	      fgets(blankline,LINELEN,in);
/*	      printf("  The blankline is:\n");                               */
/*	      printf("%s",blankline);                                        */
/*	      printf("               End of entry %d\n",current);            */
	    }
	  numentries=current;

/*	  printf(" contents of the nameline\n");                             */
/*	  printf("Location\tValue\n");                                       */
/*	  for (debugint1=1; debugint1 <= numentries; ++debugint1)
		{
		  printf("%d\t%s\n",debugint1,nameline+debugint1*LINELEN);
		}
	  printf(" contents of the sortline\n");
	  printf("Location\tValue\n");
	  for (debugint1=1; debugint1 <= numentries; ++debugint1)
		{
		  printf("%d\t%s\n",debugint1,sortline+debugint1*LINELEN);
		}
	  printf(" contents of the useridline\n");
	  printf("Location\tValue\n");
	  for (debugint1=1; debugint1 <= numentries; ++debugint1)
		{
		  printf("%d\t%s\n",debugint1,useridline+debugint1*LINELEN);
		}
*/
	  if (in!=stdin)
	    fclose(in);
	  
	  /* Start Sorting Loop */
	  strcpy(zline,"zzzzzzzzzzzzzzzzzzzzzzzzzzz");
	  for (cycle=1; cycle <= numentries; ++cycle)
	    {
	      strcpy(currentlowestsortname,zline);
	      for (current=1; current<= numentries; ++current)
		{
		  /* Search for lowest ranking name */
/*		  printf("CLSN = %s\n",currentlowestsortname);
		  printf("SL   = %s\n",sortline+current*LINELEN);            */
		  if (strcmp(currentlowestsortname,sortline+current*LINELEN) > 0)
		    {
/*		      printf("SL is less than CLSN\n");                      */
		      lowestnum=current;
		      strcpy(currentlowestsortname,sortline+lowestnum*LINELEN);
		    }
/*		  else
		    {
		      printf("SL is not less than CLSN\n");
		    } */
		}  
	      /* Print lowest ranking name */
	      strcpy(currentlowestname,nameline+lowestnum*LINELEN);
	      strcpy(currentlowestuserid,useridline+lowestnum*LINELEN);
/*	      strcat(currentlowestname,"\n");*/
/*	      strcat(currentlowestuserid,"\n");*/
	      fprintf(out,"%s",currentlowestname);
/*	      printf("The cycle is now set at %d\n",cycle);
	      printf("The following is currentlowestname\n");
	      printf("%s",currentlowestname); */
	      fprintf(out,"%s",currentlowestuserid);
/*	      printf("The following is currentlowestuserid\n");
	      printf("%s",currentlowestuserid); */
	      fprintf(out,"\n");
	      strcpy(sortline+lowestnum*LINELEN,zline);
	      /* Continue Loop */
	    }
	  /* End sort routine */
	  if (out!=stdout)
	    fclose(out);
	  
	  /* Handle the Files */
	  /* Delete Input File */
	  strcpy(deletecommand,"rm ");
	  strcat(deletecommand,inputfile);
	  system(deletecommand);
	  /* Move Output File to Input Filename */
	  strcpy(movecommand,"mv ");
	  strcat(movecommand,tempfile);
	  strcat(movecommand," ");
	  strcat(movecommand,inputfile);
	  system(movecommand);
	}
      /* End Sorting Sequence */





      
      
      /*Change the default file name*/
      else if (response[0] == 'C' || response[0] == 'c')
	{
	  printf("\n    Choose a new file name for debase operations\n");
	  printf("    The current name is \t%s\n\n",outputfile);
	  fgets(outputfile,LINELEN,stdin);
	  strstrip(outputfile);
	  printf("    Current filename used\t%s\n\n", outputfile);
       	}
      



      /*Edit the default file*/
      else if (response[0] == 'E' || response[0] == 'e')
	{
	  printf("\n    * This program section is still under development *\n\n");
	}
      






      /* Appending the default filename */
      /* Added stuff about lockers 14 January 1990 */
      else if (response[0] == 'A' || response[0] == 'a')
       	{
	  /* Open the output file */
	  out = fopen(outputfile,"a+");

	  /* Input/Output Loop */
	  printf("    Current filename used \t%s\n\n", outputfile);  
	  line[0] = 32;
	  while (line[0] != 'q') 
	    {
	      
	      /* Clear Reused Variables */
	      for (n=0; n <= LINELEN; ++n)
		{
		  strcpy(firstoutputline,NULL);
		  strcpy(secondoutputline,NULL);
		}
	      
	      /* Enter the input information */
	      printf("Enter the full name of the user -- first, middle, last\n");
	      printf("  If the user has no middle initial or name, enter an <A>\n");
	      printf("  please enter only one middle name or initial\n");
	      printf("  If you are entering a locker, use the following format:\n");
	      printf("    Name: NavalShipDesign Courseware locker\n");
	      printf("      note that locker is lowercase!\n");
	      printf("    Username: 13.ship\n");
	      printf("    Student ID number: 1\n");
	      fgets(line,LINELEN,stdin);
	      strextract(line,firstname,1,LINELEN);
	      strextract(line,middlename,2,LINELEN);
	      strextract(line,lastname,3,LINELEN);
	      
	     
	      printf("Enter the users username\n");
	      fgets(line,LINELEN,stdin);
	      strextract(line,username,1,LINELEN);
	      printf("Enter the users Student ID number -- do not separate digits\n");

/* NFS stuff */

	      strcpy(tempfile1,"debase1.tmp");
	      strcpy(hesiodcommand,"hesinfo ");
	      strcat(hesiodcommand,username);
	      strcat(hesiodcommand," filsys > debase1.tmp");
	      system(hesiodcommand);
	      
	      hesout=fopen(tempfile1,"r+");	     
	      fgets(hesoutput,LINELEN,hesout);
	      strextract(hesoutput,userNFS+current*LINELEN,3,LINELEN);
	      if (hesout!=stdin)
		fclose(hesout);
	      system("rm debase1.tmp");


	      fgets(line,LINELEN,stdin);
	      strextract(line,SSN,1,LINELEN);
	      
	      /* Concatinate the output string */
	      strcat(firstoutputline,lastname);
	      strcat(firstoutputline,"\t");
	      strcat(firstoutputline,firstname);
	      strcat(firstoutputline,"\t");
	      strcat(firstoutputline,middlename);
	      strcat(firstoutputline,"\n");
	      strcat(secondoutputline,username);
	      strcat(secondoutputline,"\t");
	      strcat(secondoutputline,SSN);
	      strcat(secondoutputline,"\t");
	      strcat(secondoutputline,userNFS+current*LINELEN);
	      strcat(secondoutputline,"\n");
	      strcat(secondoutputline,"\n");
	      	      
	      /* Print the output string */
	      fprintf(out,"%s",firstoutputline);
	      fprintf(out,"%s",secondoutputline);
	      printf("Enter <q> to quit, any other to continue\n");
	      fgets(line,LINELEN,stdin);
	    }
	  /* End of Appending Loop */
	  /* Close output */
	  
	  fclose(out);

	}
      /* End of Appending Commands */
      



      /* Quitting Commands */
      else if (response[0] == 'Q' || response[0] == 'q') 
	{
	  printf("Goodbye!!!\n\n");
	}




      else

	/* Scrap Heap */

	{
	  if (justlisted != 1) 
	    {printf("Unrecongnized Command\n");}
	      
	}
    }
	}
