/*********************************************************************
 * Author: B. Alex Bridges                                           *
 * Login ID: brid0129                                                *
 * Class: CPSC-221, Winter 1999                                      *
 * Project: Programming Assignment 2                                 *
 * Description: This program generates a cross reference for a Java  *
 *              program file.  It uses a closed hash table with      *
 *              double hashing.                                      *
 * Contents: main method which drives program.                       *
 *********************************************************************/

/* IMPORTS */
import java.io.*;


class Prog2
{
  /* CONSTANTS */
  final static boolean b_debug = false; // CONTROLS EXTRA DEBUG OUTPUT
  // => MENU OPTION - CHOICES
  final static String c_start_choice = "1.  Start Generation of Cross Reference";
  final static String c_exit_choice  = "0.  None of the Above - Exit Program";
  final static String c_choices      = "Enter a Number - [1,0]? ";
  // => MENU OPTION - COMMANDS
  final static int c_start_cmnd =  1;
  final static int c_exit_cmnd  =  0;

  /*************************************************************************
   * Method: main                                                          *
   * Purpose: Drives program.                                              *
   * Input: --COMMAND-LINE ARGUMENTS--                                     *
   *        1. 'JAVA_PROG' = The filename of the Java program.             *
   * Output: --EXIT VALUES--                                               *
   *         => COMPLETED SUCCESSFULLY = +1                                *
   *         => FATAL ERROR/EXCEPTION  = -1                                *
   *************************************************************************/
  public static void main(String[] args)
  {
    /* LOCAL VARIABLES */
    BufferedReader br_std_in; // TEXT INPUT STREAM
    int int_menu_option;      // USER INPUT = MENU OPTION
    boolean b_valid;          // VALIDITY OF USER INPUT
    String str_given = "";    // USER INPUT = STRING

    try
    {
      /* STANDARD TEXT INPUT STREAM SETUP */
      br_std_in = new BufferedReader( new InputStreamReader(System.in) );

      /* HEADER */
      System.out.println("*******************************\n"+
                         "* BAB's Cross Referencer v1.0 *\n"+
                         "*******************************\n");

      /* CHECKING FOR REQUIRED ARGUMENT */
      if(args.length != 1)
      {
        System.out.println(" FATAL ERROR: The following command-line argument must be included:\n"+
                           "  => 'JAVA_PROG' = The filename of the Java program.");
        System.exit(-1);
      } // if
      if(b_debug)
      {
        System.out.println("The following command-line arguments were provided:\n"+
                           " => 1. 'JAVA_PROG' = " + args[0] + "\n");
      } // if

      /* MENU OPTIONS */
      do
      {
        int_menu_option = -1; // DEFAULT VALUE = NONE
        b_valid = false;      // DEFAULT VALUE = INVALID

        do
        {
          // => DISPLAYING MENU OPTIONS
          System.out.print("Please choose one of the following commands:\n"+
                           c_start_choice+"\n"+
                           "\n"+
                           c_exit_choice+"\n"+
                           "\n"+
                           c_choices);
          // => READING OF USER INPUT
          System.out.flush();
          int_menu_option = Integer.parseInt( br_std_in.readLine() );
          System.out.println("");

          // => CHECKING OF USER INPUT
          if( (int_menu_option < c_exit_cmnd) || (int_menu_option >  c_start_cmnd) )
            System.out.println(" ERROR: Invalid menu option. Please try again.");
          else
            b_valid = true;
        }
        while (b_valid == false);

        // => RESULTING ACTIONS BASED ON USER INPUT
        switch(int_menu_option)
        {
          // => START A NEW GAME
          case 1:
            System.out.println(" Starting generation of cross reference . . . \n");
            Xref.begin(args[0]);
            break;
        } // switch
      } // do
      while(int_menu_option != 0);

      // RESULTING ACTIONS BASED ON USER INPUT => EXIT PROGRAM
      System.out.println(" Program exiting . . . ");
      System.exit(1);
    } // try
    /* EXCEPTION HANDLING */
    catch(IOException exception)
    {
      System.out.println(" FATAL EXCEPTION: Standard input problem.");
      System.exit(-1);
    } // catch
    catch(NumberFormatException exception)
    {
      System.out.println(" EXCEPTION: Invalid menu option.");
    } // catch
  } // method main
} // class Prog2

