import java.io.*;
import java.util.StringTokenizer;

public class UnWtTrav
{
   static BufferedReader console = new BufferedReader
                                  (new InputStreamReader(System.in));

/**
 * Return one line from System.in
 */
   static String readLine()
   {  String rtn = "";

      try
      {  rtn = console.readLine();  }
      catch (Exception e)
      {  System.out.println(e);  System.exit(-1);  }
      return rtn;
   }

   public static void main ( String[] args )
   {
      String   lineIn = "";
      CGraph   map = new CGraph();
      PrintWriter logFile = null;
      BufferedReader inp = null;

      try
      {  File f  = new File("UnWtLogFile.txt");
         logFile = new PrintWriter(new FileWriter(f));
      }
      catch (IOException e)
      {  System.err.println ("Log file open failed: " + e);
         System.exit(-1);
      }

   // Debug option:  program supports command-line argument of file name
      if (args.length > 0)
      {
         lineIn = args[0];
         System.out.println ("Reading data from file " + lineIn);
      }

      inp = openInput (lineIn);
      logFile.println ("Data read from " + lineIn);

      map.fillGraph(inp);

      System.out.println ("\nThe data file has " + map.nCities() + " towns, and "
           + map.nRoads()  + " roads connecting them.");

      if (args.length > 1)
      {
         lineIn = args[1];
         System.out.println ("Starting from " + lineIn);
      }

      while (!map.validCity(lineIn))  // break on known city
      {  System.out.print ("Please enter the starting city:  ");
         lineIn = readLine();
         if (! map.validCity(lineIn))
            System.out.print ("Unrecognized city.  ");
         if (MyInpLib.eof())
            System.exit(-1);
      } // end while unknown starting point

      logFile.println ( "\nThe data file has " + map.nCities()
          + " towns, and " + map.nRoads()  + " roads connecting them.\n"
          + "Starting point:  " + lineIn);
      map.dump(logFile);  map.dump(System.out);

      System.out.println();

   // Recursive depth 1st, followed by a listing of the edges traversed in order
      System.out.println ("Recursive depth-first");
      map.depthRecur(lineIn); map.nodeList(); map.edgeList();
      logFile.println ( "\nRecursive depth-first");
      map.nodeList(logFile);map.edgeList(logFile);
      map.pathList();                // Interactive paths to the root;

   // Iterative depth 1st, followed by a listing of the edges traversed in order
      System.out.println ("Iterative depth-first");
      map.depth1st(lineIn); map.nodeList(); map.edgeList();
      logFile.println ( "\nIterative depth-first");
      map.nodeList(logFile);map.edgeList(logFile);
      map.pathList();                // Interactive paths to the root;

   // Breadth First, followed by a listing of the edges traversed in order
      System.out.println ("Breadth-first");
      map.breadth1st(lineIn); map.nodeList(); map.edgeList();
      logFile.println ( "\nBreadth-first");
      map.nodeList(logFile);map.edgeList(logFile);
      map.pathList();                // Interactive paths to the root;

      logFile.close();
   } // end main

   static BufferedReader openInput ( String lineIn )
   {
      BufferedReader inp = null;

      if ( lineIn == null || lineIn.length() == 0 )
      {
         System.out.print ("input file:  ");
         lineIn = readLine();
      }

      while ( inp == null )
      {
         try
         {
            File f = new File (lineIn);
            FileReader  fis = new FileReader(f);
            inp = new BufferedReader (fis);
         }
         catch (IOException e)
         {  System.out.println ("Exception: " + e);  }
         finally
         {  if (inp == null)
            {
               System.out.print ("Yo! Open failed for " + lineIn
                    + "\nPlease enter a valid file name:  ");
               System.out.flush();
               lineIn = readLine();
               if ( lineIn.length() == 0 )
               {  System.out.println ("NULL name; aborting!");
                  System.exit(0);
               }
            }
         } // end try / catch / finally

      } // end while

      return inp;
   } // end openInput

} // end class UnWtTrav
