import java.io.*;
import java.util.*;
import java.net.*;
//_____________________________________________________________
public class makefull
{ 
//_____________________________________________________________
public static final void main(String[] args) throws Exception
{
   makefull bnk = new makefull();
   bnk.start(args);
}
//_____________________________________________________________
makefull() throws Exception 
{
}
//___________________________________________________________________________
void start(String[] args)
{
   File file2 = new File("full.txt");
   if (file2.exists()) 
   {
      System.out.println("already found");
      return;
   }
   StringBuffer bufout = new StringBuffer();
   String name2 = "http://www.textfiles.com/etext/FICTION/";
   if (args.length > 0) name2 = args[0];
   if (!name2.substring(name2.length()-1).equals("/"))
   {
      name2 = name2 + "/";
   }
   String file = getUrl(name2);
   saveFile("url.txt",file,true);
   String alllinks = links(file,name2);
   String[] look = alllinks.split("\r\n");  
   for (int i = 0; i < look.length; i++)
   {
      System.out.println(look[i]);
      file = getUrl(look[i]);
      saveFile("full.txt",file,true); 
   }
}
//___________________________________________________________________________
String links(String file, String name)
{
   file = file.toLowerCase();
   StringBuffer buf = new StringBuffer();
   int poslast = name.lastIndexOf("/");
   String parent = name.substring(0,poslast+1);
   if (parent.length() <= 8) parent = name + "/";
   int pos = 0;
   for (int i = 0; i < 1000; i++)
   {
      pos = file.indexOf("<a",pos+1);
      if (pos < 0) break;
      int pos2 = file.indexOf("href",pos+1);
      int pos3 = file.indexOf("\"",pos+1);
      int pos4 = file.indexOf("'",pos+1);
      int pos5 = 0;
      int pos6 = 0;
      if ((pos2 > pos) & (pos2 < pos + 20))
      {
         String lk = "\"";
         if ((pos3 > pos2+1) & (pos3 < pos4))
         {
            pos6 = pos3;
            pos5 = file.indexOf("\"",pos3 + 1);
         }
         else
         {
             if (pos4 > pos2+1)
             {
                pos6 = pos4;
                pos5 = file.indexOf("'",pos4 + 1);
             }
         }
         if (pos5 > pos)
         {
            String htm = file.substring(pos6+1,pos5);
            if (htm.indexOf("\r") < 0)
            {
               if (htm.indexOf("\n") < 0)
               {
                  if (htm.length() > 4)
                  {
                     if (!htm.substring(0,4).equals("http"))
                     {
                        htm = parent + htm;
                     }
                  }
                  else htm = parent + htm;
                  buf.append(htm + "\r\n");
               }
            }
         }
      }
   }
   return(buf.toString());
}
//___________________________________________________________________________
String getUrl(String name)
{
   StringBuffer buf = new StringBuffer();
  try
  {
   String line;
   URL url = new URL(name);
   InputStream is = url.openStream();
   BufferedReader br = new BufferedReader(new InputStreamReader(is));
   while ((line = br.readLine()) != null) 
   {
      buf.append(line + "\r\n");
   }
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
   return(buf.toString());
}
//___________________________________________________________________________
void saveFile(String file, String str, boolean append) 
{
  try
  {
   BufferedWriter out = new BufferedWriter(new FileWriter(file, append));
   out.write(str);
   out.close();
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
}
}//__________________________________________________________________________
