/*

  File: client.cc
  Author: Jason Villarreal

  This is the cgi script that will be run from the web.  It communicates
  with the server program on "isky" that actually turns the stepper motor.

*/

#include "comm.h"
#include <iostream.h>
#include <string.h>

int main(int argc, char* argv[])
{

  // Hard code the values to connect to into the code

  char* host, *port;
  host = new char[13];
  strcpy(host, "isky.ucr.edu");
  port = new char[5];
  strcpy(port, "5000");
  int fd;

  // The proper format for running this program is with one command line
  //  parameter.  If it is not run this way, we must output an Error
  //  HTML page.

  if (argc != 2)
  {

    // There were either too many or too few paramters

    cout<<"Content-Type: text/html"<<endl<<endl;
    cout<<"<HTML>"<<endl;
    cout<<"<HEAD><TITLE>Error!</TITLE></HEAD>"<<endl;
    cout<<"<BODY>"<<endl;
    cout<<"<H1>Error!</H1>"<<endl;
    cout<<"<P> Too many (or too few) parameters. "<<endl;
    cout<<"Please go back and try again."<<endl;
    cout<<"</BODY>"<<endl;
    cout<<"</HTML>"<<endl;

    return 0;
    
  }
  else
  {

    // We got our one parameter, but was it a valid value?

    if(strcmp(argv[1], "a") == 0)
    {

      // Tell the server to move camera 1 to the right

      // First, make a connection with isky

      fd = connectTCP(host, port);

      if(fd == -1)
      {

	    // We were unable to connect with isky!
	    
	    cout<<"Content-Type: text/html"<<endl<<endl;
	    cout<<"<HTML>"<<endl;
       cout<<"<HEAD><TITLE>Error!</TITLE></HEAD>"<<endl;
       cout<<"<BODY>"<<endl;
       cout<<"<H1>Error!</H1>"<<endl;
	    cout<<"<P>Server is busy.."<<endl;
	    cout<<"Please try again a little later."<<endl;
       cout<<"</BODY>"<<endl;
       cout<<"</HTML>"<<endl;

	    return 0;

      }

      //We have a connection so I'll send the value "a" over the wire

      write(fd, "a", 1);

      //We're done talking to isky

      close(fd);
      
    }
    else if(strcmp(argv[1], "b") == 0)
    {

      //Send command to move camera 1 to the left.

      //First, open up a dialogue with isky

      fd = connectTCP(host, port);

      if(fd == -1)
      {

	    //We can't talk with isky!

	    cout<<"Content-Type: text/html"<<endl<<endl;
	    cout<<"<HTML>"<<endl;
       cout<<"<HEAD><TITLE>Error!</TITLE></HEAD>"<<endl;
       cout<<"<BODY>"<<endl;
       cout<<"<H1>Error!</H1>"<<endl;
	    cout<<"<P>Server is busy."<<endl;
	    cout<<"Please try again a little later."<<endl;
            cout<<"</BODY>"<<endl;
            cout<<"</HTML>"<<endl;

	    return 0;

      }

      //We have a connection

      write(fd, "b", 1);

      //We're done talking to isky

      close(fd);

    }
    
	else if(strcmp(argv[1], "c") == 0)
	{
	
      //First, open up a dialogue with isky

      fd = connectTCP(host, port);

      if(fd == -1)
      {

	    //We can't talk with isky!

	    cout<<"Content-Type: text/html"<<endl<<endl;
	    cout<<"<HTML>"<<endl;
        cout<<"<HEAD><TITLE>Error!</TITLE></HEAD>"<<endl;
        cout<<"<BODY>"<<endl;
        cout<<"<H1>Error!</H1>"<<endl;
	    cout<<"<P>Server is busy.."<<endl;
	    cout<<"Please try again a little later."<<endl;
        cout<<"</BODY>"<<endl;
        cout<<"</HTML>"<<endl;

	    return 0;

      }

      //We have a connection

      write(fd, "c", 1);

      //We're done talking to isky

      close(fd);

	}

	else if(strcmp(argv[1], "d") == 0)
	{
	
      //First, open up a dialogue with isky

      fd = connectTCP(host, port);

      if(fd == -1)
      {

	    //We can't talk with isky!

	    cout<<"Content-Type: text/html"<<endl<<endl;
	    cout<<"<HTML>"<<endl;
       cout<<"<HEAD><TITLE>Error!</TITLE></HEAD>"<<endl;
       cout<<"<BODY>"<<endl;
       cout<<"<H1>Error!</H1>"<<endl;
	    cout<<"<P>Server is busy."<<endl;
	    cout<<"Please try again a little later."<<endl;
       cout<<"</BODY>"<<endl;
       cout<<"</HTML>"<<endl;

	    return 0;

      }

      //We have a connection

      write(fd, "d", 1);

      //We're done talking to isky

      close(fd);

	}

	else
    {

      // Someone somehow called this program with something other
      // than one of the valid values so we must inform them of this fact.

      cout<<"Content-Type: text/html"<<endl<<endl;
      cout<<"<HTML>"<<endl;
      cout<<"<HEAD><TITLE>Error!</TITLE></HEAD>"<<endl;
      cout<<"<BODY>"<<endl;
      cout<<"<H1>Error!</H1>"<<endl;
      cout<<"<P>This cgi only takes \"a\" or \"b\" or \"c\" or \"d\" as parameters."<<endl;
      cout<<"Please go back and try again."<<endl;
      cout<<"</BODY>"<<endl;
      cout<<"</HTML>"<<endl;

      return 0;

    }
    
  }

  // Now that we've successfully moved the camera, we will output the
  // correct page.

  if (strcmp(argv[1], "a") == 0 || strcmp(argv[1], "b") == 0)
  {
      

  cout<<"Content-Type: text/html"<<endl<<endl;
  cout<<"<HTML>"<<endl;
  cout<<"<HEAD><TITLE>The Fabulous B256 Cam!</TITLE>"<<endl;
  cout<<"<META HTTP-EQUIV=\"Refresh\" CONTENT=\"10;
URL=http://dalton.ucr.edu/lab_cam/B256.html\">"<<endl;
  cout<<"</HEAD>"<<endl;
  cout<<"<BODY BGCOLOR=\"#00FFFF\">"<<endl;
  
  cout<<"<FONT COLOR=\"#0000FF\">"<<endl;
  cout<<"<H1 ALIGN=CENTER>The Fabulous B256 Cam!</H1>"<<endl;
  cout<<"</FONT>"<<endl;
  cout<<"<CENTER>"<<endl;
  cout<<"<IMG SRC=\"../cgi-bin/send_image.pl?image=img2.jpg\" ALIGN=CENTER BORDER=5>"<<endl;
  cout<<"</CENTER>"<<endl;
  cout<<"<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=\"100%\" NOSAVE ALIGN=CENTER>"<<endl;
  cout<<"<TR NOSAVE>"<<endl;
  cout<<"<TD NOSAVE ALIGN=CENTER>"<<endl;
  
  cout<<"<FORM method=\"post\" action=\"../cgi-bin/client.cgi?b\">"<<endl;
  cout<<"<input type=\"Submit\" value=\"<<\">"<<endl;
  cout<<"</form>"<<endl;

  cout<<"</TD>"<<endl;
  cout<<"<TD NOSAVE ALIGN=CENTER>"<<endl;
  cout<<"<B>Camera Controls</B>"<<endl;
  cout<<"</TD>"<<endl;

  cout<<"<TD NOSAVE ALIGN=CENTER>"<<endl;
  cout<<"<FORM method=\"post\" action=\"../cgi-bin/client.cgi?a\">"<<endl;
  cout<<"<input type=\"submit\" value=\">>\">"<<endl;
  cout<<"</form>"<<endl;
  cout<<"</TD>"<<endl;
  cout<<"</TABLE>"<<endl;
  cout<<"<H1>Note:</H1>"<<endl;
  cout<<"<P>The camera has been moved.  Please wait"<<endl;
  cout<<" for approximately 10 seconds for the new picture to appear"<<endl;
  cout<<"</BODY>"<<endl;
  cout<<"</HTML>"<<endl;
  
  
  }
  else
  {
      
  
  cout<<"Content-Type: text/html"<<endl<<endl;
  cout<<"<HTML>"<<endl;  
  cout<<"<HEAD><TITLE>The Fabulous B265 Cam!</TITLE>"<<endl;
  cout<<"<META HTTP-EQUIV=\"Refresh\" CONTENT=\"10;
URL=http://dalton.ucr.edu/lab_cam/B265.html\">"<<endl;
  cout<<"</HEAD>"<<endl;
  cout<<"<BODY BGCOLOR=\"00FFFF\">"<<endl;
  cout<<"<FONT COLOR=\"#0000FF\">"<<endl;
  cout<<"<H1 ALIGN=CENTER>The Fabulous B265 Cam!</H1>"<<endl;
  cout<<"</FONT>"<<endl;
  cout<<"<CENTER>"<<endl;
  cout<<"<IMG SRC=\"../cgi-bin/send_image.pl?image=img1.jpg\" ALIGN=CENTER BORDER=5>"<<endl;
  cout<<"</CENTER>"<<endl;
  cout<<"<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=\"100%\" NOSAVE ALIGN=CENTER>"<<endl;
  cout<<"<TR NOSAVE>"<<endl;
  cout<<"<TD NOSAVE ALIGN=CENTER>"<<endl;

  cout<<"<FORM method=\"post\" action=\"../cgi-bin/client.cgi?d\">"<<endl;
  cout<<"<input type=\"Submit\" value=\"<<\">"<<endl;
  cout<<"</form>"<<endl;

  cout<<"</TD>"<<endl;

  cout<<"<TD NOSAVE ALIGN=CENTER>"<<endl;
  cout<<"<B>Camera Controls</B>"<<endl;
  cout<<"</TD>"<<endl;

  cout<<"<TD NOSAVE ALIGN=CENTER>"<<endl;
  cout<<"<FORM method=\"post\" action=\"../cgi-bin/client.cgi?c\">"<<endl;
  cout<<"<input type=\"submit\" value=\">>\">"<<endl;
  cout<<"</form>"<<endl;

  cout<<"</TD>"<<endl;
  cout<<"</TABLE>"<<endl;
  
  
  cout<<"<H1>Note:</H1>"<<endl;
  cout<<"<P>The camera has been moved.  Please wait"<<endl;
  cout<<" for approximately 10 seconds for the new picture to appear"<<endl;
  cout<<"</BODY>"<<endl;
  cout<<"</HTML>"<<endl;
  
  }
  
  
  return 0;

}
