//testme

import cs1Lib.io.*;
import java.io.*;
import java.net.*;

public class Sluggy
{
  public static void main(String args[])
  {
    int year, month, day, repeat;
    TextReader in = new TextReader(System.in);

    System.out.println("Sluggy Leecher <c>2001 Albert O'Connor");
    System.out.println("");
  
    System.out.println("Step1.  Enter the day you wish to start at:");
    System.out.print("Enter the year(yyyy): ");
    year = in.readInt();
    System.out.println("");
    System.out.print("Enter the month(mm): ");
    month = in.readInt();
    System.out.println("");
    System.out.print("Enter the day(dd): ");
    day = in.readInt();
    System.out.println("");

    System.out.print("Step2. Enter the number of day you want to leech images for:");
    repeat = in.readInt();

 
    InputStream istream = null;
    SluggyDate sd = new SluggyDate(year,month,day);
    for(int q=0; q<repeat; q++)
    {
      if(!sd.isSunday())
      {
        try
        {
	  System.out.println(sd.toString());
          URL url = new URL("http://pics.sluggy.com/comics/"+sd.toString()+"a.gif");
          istream = url.openStream();
        }
        catch(MalformedURLException MURLE)
        {
          System.out.println("EXCEPTION REPORT: crapy URL you have there");
        }
        catch(IOException IOE)
        {
          System.out.println("EXCEPTION REPORT: Unable to openStream()");
        }
    
        try
        {
          FileOutputStream fout = new FileOutputStream(sd.toString()+".gif");
          byte[] b = new byte[256];
          int bread = 0;
          while(bread != -1)
          {
            bread = istream.read(b);
            if(bread == -1)
	      break;
            fout.write(b,0,bread);
       /* System.out.println("Data DUMP:");
        for(int i=0; i<256; i++)
        {
          System.out.print(b[i]+" ");
        }*/
          }
        }
        catch(IOException IOE)
        {
           System.out.println("EXCEPTION REPORT: to do other IO");
        }
      }
      else //it's Sunday!
      {
        for(int x=1; x<7; x++)
        {
          try
          {
	    System.out.println("http://pics.sluggy.com/comics/"+sd.toString()+"b"+x+".jpg");
            URL url = new URL("http://pics.sluggy.com/comics/"+sd.toString()+"b"+x+".jpg");
            istream = url.openStream();
          }
          catch(MalformedURLException MURLE)
          {
            System.out.println("EXCEPTION REPORT: crapy URL you have there");
          }
          catch(IOException IOE)
          {
            System.out.println("EXCEPTION REPORT: Unable to openStream()");
          }
    
          try
          {
            FileOutputStream fout = new FileOutputStream(sd.toString()+"_"+x+".jpg");
            byte[] b = new byte[256];
            int bread = 0;
            while(bread != -1)
            {
              bread = istream.read(b);
              if(bread == -1)
	        break;
              fout.write(b,0,bread);
            }
          }
          catch(IOException IOE)
          {
            System.out.println("EXCEPTION REPORT: to do other IO");
          }
        }//end for loop
      }
      sd.Tomorrow();
    }
  }
}
