import java.lang.String;
import java.util.StringTokenizer;
import java.util.Hashtable;
import java.io.*;

/* A  Java program to extract a directory entry from a faculty database,
   based upon first and last names
   Program assumes the directory is located in 
      /home/walker/pubic_html/cgi-bin/math-cs-faculty-98-alt
*/

public class facDirJavaAlt {
    static String directoryName = "/home/walker/public_html/cgi-bin/math-cs-faculty-98-alt";
    static int maxNames = 20;
    static String directoryArray [][] = new String [maxNames][6];
    /* directoryArray[i] will give the ith array entry, as an array
          directoryArray[i][0] will be the first name
          directoryArray[i][1] will be the last name
          directoryArray[i][2] will be the person's title
          directoryArray[i][3] will be the e-mail address
          directoryArray[i][4] will be the telephone numberr
          directoryArray[i][5] will be the office number
    */

    public static void printWebPageHeader () 
           throws Exception {
        /* prints an html page header */
      System.out.println("Content-type: text/html");
      System.out.println();
      System.out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
      System.out.println("<html>");
      System.out.println("<head>");
      System.out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">");
      System.out.println("<title>");
      System.out.println("Directory Search");
      System.out.println("</title>");
      System.out.println("</head>");
      System.out.println("<body>");
    }

    public static void printWebPageFooter () 
           throws Exception {
        /* prints an html page footer */
      System.out.println("</body>");
      System.out.println("</html>");
    }

  public static void main(String[] args) 
         throws Exception {

      /********************** html/browser header ************************/ 
      int dirSize = 0;
      printWebPageHeader ();

      /*******************************************************************
       * load directory array of names and telephone numbers from file
       */

      /* open directory file */
      FileReader freader = new FileReader(directoryName);

      /* convert input stream to an DataInputStream */
      BufferedReader in = new BufferedReader(freader);

      try 
          {
              /* first two lines of data file contain header information */
              in.readLine();
              in.readLine();

              /* read successive lines and put into directoryArray */
              while (in.ready())
                  {

                      String line = in.readLine();
                      /* split line into fields by scanning for commas */
                      int fieldIndex = 0;
                      int firstPos   = 0;
                      int secondPos  = line.indexOf(",");

                      /* scan to locate first name, last name, title, 
                                        e-mail, phone, office */
                     while (secondPos >= 0)
                          {
                              directoryArray[dirSize][fieldIndex]
                                  = line.substring(firstPos, secondPos);
                              firstPos = secondPos + 1;
                              secondPos = line.indexOf(",", firstPos);
                              fieldIndex++;
                          }                 
                     directoryArray[dirSize][fieldIndex]
                                  = line.substring(firstPos);
                      dirSize++;
                  }
          }
      catch (Exception e)
          {
              System.out.println ("<br>File input error<br>");
          }

      in.close();

      /*******************************************************************
       * process the form data, putting values into a Hashtable 
       *    args[0] contains the browser information as composite string 
       *    divide the query strings into name=value pieces
       */
      StringTokenizer nameValue = new StringTokenizer (args[0], "&");

      /* put name=value pieces into hash table for easy retrieval */
      Hashtable<String, String> queryElements=new Hashtable<String, String>();
      while (nameValue.hasMoreTokens()) {
          String pair = nameValue.nextToken();
          int pos = pair.indexOf("=");
          queryElements.put(pair.substring(0, pos), pair.substring(pos+1));
      }


      /*******************************************************************
       * search directoryArray for desired name
       */
      System.out.println("<center>");
      System.out.println ("<h1>Search Results for Math/CS Faculty</h2>");
      System.out.println("</center>");

      String firstname = queryElements.get("firstname");
      String lastname  =  queryElements.get("lastname");
      System.out.println ("Search for " + firstname + " " + lastname);

      for (int i = 0; i < dirSize; i++)
          {if (firstname.equalsIgnoreCase(directoryArray[i][0])
               && lastname.equalsIgnoreCase(directoryArray[i][1]))
              {
                  System.out.println("<p>Name:  " + firstname + " " + lastname);
                  System.out.println("<ul>");
                  System.out.println("<li>Title: " + directoryArray[i][2]);
                  System.out.println("<li>E-mail:  " + directoryArray[i][3]);
                  System.out.println("<li>Telephone:  " + directoryArray[i][4]);
                  System.out.println("<li>Office:  " + directoryArray[i][5]);
                  System.out.println("</ul>");
              }
          }
  

      /********************** html/browser footer ************************/ 
      printWebPageFooter();

  } // main(String[])
} // facDirJavaAlt

