import java.io.*;
import java.util.*;
import java.net.*;

public class Clone
{
    static OutputStream outputStream;
    static InputStream inputStream;
    static Socket socket;
    static Socket outsock;
    static int desired;

    public static void main(String[] args) throws Exception {
        if(args.length<4)
        {
          System.out.println("usage: clone <localport> <remotehost> <remoteport>");
          System.out.println("example: clone 4000 localhost 5010 0");
          System.exit(1);
        }

        System.out.println("Dangerous Clone v4.4.3 (Esperanto)");
        System.out.println("Copyleft Postmodern Rutabega 2002");
        System.out.println("\"Um...what time is it?\"");

        int local=Integer.parseInt(args[0]);
        String host=args[1];
        int port=Integer.parseInt(args[2]);
        desired=Integer.parseInt(args[3]);

        ServerSocket server=new ServerSocket(local);
        while(true)
        {
          socket = server.accept();
System.out.println("Accepted connection");
          inputStream = new BufferedInputStream(socket.getInputStream());

          try
          {
            Socket outsock=new Socket(host, port);
            outputStream=outsock.getOutputStream();
          }
          catch(Exception e)
          {
            System.out.println("Could not connect to destination.");
            socket.close();
            continue;
          }
          pump(inputStream, new PrintWriter(outputStream));
          try { inputStream.close(); outputStream.close(); }
          catch(Exception e) {}
        }
    }

  static public void pump(InputStream inputStream, PrintWriter writer)
  {
            byte[] readBuffer = new byte[16];
            int[] min = new int[16];
            int[] max = new int[16];

            byte[] sync = new byte[1];

            for(int x=0;x<min.length;x++)
            {
              min[x]=0;
              max[x]=0;
            }

            try
            {
                while (true)
                {
                    while(sync[0]!=-1)
                    {
//                      System.out.println("waiting for sync: "+sync[0]);
                      inputStream.read(sync);
                    }

//                    System.out.println("got sync");

                    int numRead = 0;
                    int numBytes = 0;
                    while(numRead!=16)
                    {
                      int left=16-numRead;
                      numBytes = inputStream.read(sync);
                      if(numBytes==1)
                      {
                        readBuffer[numRead]=sync[0];
                        if(sync[0]==-1)
                          System.out.println("ARGH! Sync");
                        else
                          numRead = numRead+numBytes;
// System.out.println("read: "+numBytes+" need: "+left+"("+sync[0]+")");
                      }
                    }

                    for(int x=0;x<16;x++)
                    {
                      int val=(int)readBuffer[x];
                      if(val<0) val=128+(128+val);
                      if(x==desired)
                        System.out.println(x+"="+val);
                      writer.println("[R="+x+",D="+val+",V=0,A=0,X=1,Y=0,Z=0]");
                      writer.flush();
                      if(min[x]>val) min[x]=val;
                      if(max[x]<val) max[x]=val;
                      System.out.println("min["+x+"]="+min[x]);
                      System.out.println("max["+x+"]="+max[x]);
                    }

                    inputStream.read(sync);
                }
            }
            catch (SocketException e)
            {
              System.out.println("Connection broken");
              System.exit(1);
            }
            catch (Exception e) {e.printStackTrace();}
         }
    }

