import java.util.*;

// A pure 1.1 variation of a class that appears in
// Core Web Programming from Prentice Hall Publishers.
// May be freely used or adapted.
// 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/.

/** This parses the input to get a host, port,
 *  and file, then passes these three values to
 *  the UriRetriever class to grab the URL
 *  from the Web.
 * @see UriRetriever
 */

public class UrlRetriever {

  //----------------------------------------------------
  
  public static void main(String[] args) {
    checkUsage(args);
    StringTokenizer tok = new StringTokenizer(args[0]);
    String protocol = tok.nextToken(":");
    checkProtocol(protocol);
    String host = tok.nextToken(":/");
    String uri;
    int port = 80;
    try {
      uri = tok.nextToken("");
      if (uri.charAt(0) == ':') {
        tok = new StringTokenizer(uri);
        port = Integer.parseInt(tok.nextToken(":/"));
        uri = tok.nextToken("");
      }
    } catch(NoSuchElementException nsee) {
      uri = "/";
    }
    UriRetriever uriClient
      = new UriRetriever(host, port, uri);
    uriClient.connect();
  }

  //----------------------------------------------------
  /** Warn user if they forgot the URL. */
  
  private static void checkUsage(String[] args) {
    if (args.length != 1) {
      System.out.println("Usage: UrlRetriever <URL>");
      System.exit(-1);
    }
  }

  //----------------------------------------------------
  /** Tell user that this can only handle HTTP. */
  
  private static void checkProtocol(String protocol) {
    if (!protocol.equals("http")) {
      System.out.println("Don't understand protocol "
                         + protocol);
      System.exit(-1);
    }
  }
}

