import java.net.*;
import java.io.*;
import java.util.*;

public class WebCollector {

  static int imageNum = 0;
  static String imageURL[] = new String[500];

  /* $B%$%a!<%8$N%U%!%$%k$X$N=q$-=P$7(B */
  public static void writeOutImage(FileOutputStream dest, DataInputStream in) throws IOException {
    int limit = 0;
    byte[] buffer;
    int bytes_read;

    buffer = new byte[1024];
    while(limit < 500000) {
      bytes_read = in.read(buffer);
      if (bytes_read == -1)
	break;
      dest.write(buffer, 0, bytes_read);   //$B%U%!%$%k=PNO(B
      limit++;
    }
  }


  /* $B%F%-%9%H$N%U%!%$%k$X$N=q$-=P$7(B */
  public static void writeOutText(FileOutputStream dest, DataInputStream in) throws IOException {
    int limit = 0;

    PrintStream pdest = new PrintStream(dest);   //FileOutputStream$B$K(BPrintStream$B$r%i%C%W$9$k(B
    while(limit < 5000) {
      String line = in.readLine();
      if (line == null)
	break;
      pdest.println(line);   //$B%U%!%$%k=PNO(B
      limit++;
    }
  }


  /* html$B%U%!%$%k$N=hM}(B $B%$%a!<%8%U%!%$%k$N(BURL$B$rCj=P(B */
  public static void procText(FileInputStream from, String urlFull) throws IOException {

    int limit = 0;
    String urlFullPath;   //URL$B$N%U%k%Q%9(B
    int startImg = 0;

    DataInputStream dataFrom = new DataInputStream(from);   //FileInputStream$B$K(BDataInputStream$B$r%i%C%W$9$k(B

    while(limit < 5000) {

      String line = dataFrom.readLine();
      if (line == null)
	break;

      if (((startImg = line.indexOf("<img")) != -1) ||
	  ((startImg = line.indexOf("<IMG")) != -1)) {

	String newURLNameTmp = line.substring(line.indexOf("\"", startImg) + 1);   //$BEv3:(Bhtml$B%U%!%$%k$K5-=R$5$l$F$$$k(B
	String newURLName = newURLNameTmp.substring(0, newURLNameTmp.indexOf("\""));   //$B?7$7$$(BURL$B$J$I<hF@(B

	String urlPath = urlFull.substring(0, urlFull.lastIndexOf("/"));   //URL$B$N%Q%9$r<hF@(B

	if (newURLName.startsWith("http://")) {
	  urlFullPath = newURLName;
	} else if (newURLName.startsWith("/")) {
	  urlFullPath = urlPath + newURLName;
	} else {
	  urlFullPath = urlPath + "/" + newURLName;
	}

	imageURL[imageNum++] = urlFullPath;
      }
    }
  }


  /* $BEv3:(BURL$B$+$i%$%a!<%8%U%!%$%k$r;}$C$FMh$k=hM}(B */
  public static void getImage(URLConnection uc) throws IOException {

    FileOutputStream imageDest = null;
    FileInputStream imageFrom = null;

    try {
      String imageUrlFull = uc.getURL().toExternalForm();   //URL$B$N%U%k%M!<%`(B
      int imageSlashPlace = imageUrlFull.lastIndexOf("/");
      String imageUrlFile = imageUrlFull.substring(++imageSlashPlace);   //$B%U%!%$%kL>(B

      File imageIOFile = new File(imageUrlFile);   //$B;XDj$N%G%#%l%/%H%j2<$K=PNOMQ%U%!%$%k$r@8@.(B

      System.out.println(imageUrlFull);   //URL$BI=<((B
      DataInputStream imageIn = new DataInputStream(uc.getInputStream());   //InputStream$B$r(BDataInputStream$B$K$9$k(B
      imageDest = new FileOutputStream(imageIOFile);   //$B=q$-=P$7%U%!%$%k(B

      writeOutImage(imageDest, imageIn);   //$B%$%a!<%8%U%!%$%k$H$7$F07$&(B

    } finally {
      if (imageDest != null)   //$B%U%!%$%k%/%m!<%:(B
	imageDest.close();
      if (imageFrom != null)   //$B%U%!%$%k%/%m!<%:(B
	imageFrom.close();
    }

  }


  /* $BEv3:(BURL$B$+$iD:E@%U%!%$%k!J%$%a!<%8(BURL$B$N5-=R$5$l$?%U%!%$%k!K$N=hM}(B */
  public static void getImageURL(URLConnection uc) throws IOException {

    FileOutputStream dest = null;
    FileInputStream from = null;

    try {
      String urlFull = uc.getURL().toExternalForm();   //URL$B$N%U%k%M!<%`(B
      int slashPlace = urlFull.lastIndexOf("/");
      String urlFile = urlFull.substring(++slashPlace);   //$B%U%!%$%kL>(B

      File ioFile = new File(urlFile);   //$B=PNOMQ%U%!%$%k$r@8@.(B

      System.out.println(urlFull);   //URL$BI=<((B
      DataInputStream in = new DataInputStream(uc.getInputStream());   //InputStream$B$r(BDataInputStream$B$K$9$k(B
      dest = new FileOutputStream(ioFile);   //$B=q$-=P$7%U%!%$%k(B

      writeOutText(dest, in);   //$B%F%-%9%H%U%!%$%k$H$7$F07$&(B
      from = new FileInputStream(ioFile);   //$BFI$_9~$_%U%!%$%k(B
      procText(from, urlFull);

    } finally {
      if (dest != null)   //$B%U%!%$%k%/%m!<%:(B
	dest.close();
      if (from != null)   //$B%U%!%$%k%/%m!<%:(B
	from.close();
    }
  }


  /* URL$B$rJd40(B ($BNc!'(Bwww.jaist.ac.jp $B"*(B http://www.jaist.ac.jp/index.html) */
  public static String urlCompletion(String urlOriginal) {

    String afterFirstCheck;
    boolean dirsw;

    if (urlOriginal.indexOf("/") == -1) {   //$BEv3:(BURL$B$K(B"/"$B$O4^$^$l$F$$$k$+(B
      dirsw = true;
    } else {
      dirsw = false;
    }

    if (!urlOriginal.startsWith("http://")) {   //$B:G=i$KIU$$$F$J$1$l$PIU$1$k(B
      afterFirstCheck = "http://" + urlOriginal;
    } else {
      afterFirstCheck = urlOriginal;
    }

    if (urlOriginal.endsWith("/"))   //index.html$B<+F0Jd40(B
      return(afterFirstCheck + "index.html");

    String tmpLast = afterFirstCheck.substring(afterFirstCheck.lastIndexOf("/"));
    if (dirsw || (tmpLast.indexOf(".") == -1) || (tmpLast.startsWith("~"))) {
      return(afterFirstCheck + "/index.html");
    } else {
      return(afterFirstCheck);
    }
  }
  

  /* $B%3%^%s%I%i%$%s$N0z?t$,$"$C$?>l9g(B */
  public static void doPseudoCL(String arg, int sw) throws MalformedURLException, IOException {

    URL url = new URL(urlCompletion(arg));
    URLConnection connection = url.openConnection();

    if (sw == 0){
      getImageURL(connection);
    } else {
      getImage(connection);
    }
  }


  /* main */
  public static void main(String[] args) throws MalformedURLException, IOException {

    for (int i = 0; i < 500; i++)   //$BK,$l$?(BURL$B5-21G[Ns$N=i4|2=!#$@$5$$$N$G$J$s$H$+$9$k!#(B
      imageURL[i] = "0";

    if (args.length != 0) {   //$B%3%^%s%I%i%$%s$N0z?t$,$"$C$?>l9g(B
      doPseudoCL(args[0], 0);
    } else {
      System.out.println("Please input a URL as a parameter!");
      System.exit(0);
    }

    for (int j = 0; j < imageNum; j++) {
      doPseudoCL(imageURL[j], 1);
    }
  }
}


/* $BNc30=hM}(B */
class DefFileException extends IOException {
  public DefFileException(String msg) {
    super(msg);
  }
}

