//---------------------------------------------------------------------------
//
// MODULE NAME:     main.cpp
//
// COPYRIGHT:       Copyright (c) 2013, GladeSoft, Inc.
//                  All Rights Reserved.
//
//                  NOTICE: THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF
//                  GLADESOFT, INC., AND SHALL NOT BE REPRODUCED, COPIED
//                  IN WHOLE OR IN PART ADAPTED, MODIFIED, OR
//                  DISSEMINATED WITHOUT THE EXPRESS WRITTEN CONSENT
//                  OF GLADESOFT, INC. ANY REDISTRIBUTION OR
//                  PUBLICATION TO UNAUTHORIZED PERSONS IS STRICTLY
//                  PROHIBITED BY U.S. AND INTERNATIONAL COPYRIGHT LAW.
//
// DESCRIPTION:     Another example program using the Seminole embedded 
//                  web server.
//
// AUTHOR:          M. Grosberg
//
// CREATION DATE:   Jun  7, 2006
//
//---------------------------------------------------------------------------
//
// HISTORY OF CHANGE  
//
// -----+--------+------------------------------------------+------+---------
// VERS | DATE   | DESCRIPTION OF CHANGE                    | INIT | PR#
// =====+========+==========================================+======+=========
//  001 |06/07/06| File creation.                           | MYG  |
// -----+--------+------------------------------------------+------+---------
// -----+--------+------------------------------------------+------+---------
//---------------------------------------------------------------------------

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "seminole.h"
#include "sem_template.h"

#include "sitedata.h"

//**********************************************************************

class DemoHandler : public HttpdFileHandler
{
protected:
  void SendFile(RequestState &state);
    
public:
  DemoHandler(HttpdFileSystem *p_filesys,
              const char      *p_root,
              const char      *p_prefix);
};

static const char  gServername[] = "localhost";
static const int   gServerport   = 8080;
static Httpd      *gpWebServer;

//**********************************************************************

//---------------------------------------------------------------------------
//
// NAME:           DemoHandler::DemoHandler
//
// DESCRIPTION:    Construct the request handler.
//
// AUTHOR:         M. Grosberg
// CREATED:        Jun  7, 2006
// LAST MODIFIED:  N/A           BY:  N/A
//
//--------------------------------------------------------------------------
DemoHandler::DemoHandler
 (
   HttpdFileSystem *p_filesys,
   const char      *p_root,
   const char      *p_prefix
  )
  : HttpdFileHandler(p_filesys, p_root, p_prefix)
{}

//---------------------------------------------------------------------------
//
// NAME:           DemoHandler::SendFile
//
// DESCRIPTION:    Handle template file processing if a file to be sent
//                 needs template expansion.
//
// REMARKS:        This is the most common way of hooking the template
//                 engine and file handler together. This allows normal
//                 processing for any file except for templates which
//                 share a special MIME type.
//
// AUTHOR:         M. Grosberg
// CREATED:        Jun  7, 2006
// LAST MODIFIED:  N/A           BY:  N/A
//
//--------------------------------------------------------------------------
void DemoHandler::SendFile(RequestState &state)
{
  if (strcmp(state.mFileInfo.MimeType(), "x-server-internal/template") == 0)
  {
    HttpdCgiParameter *p_params;

    p_params = HttpdCgiParameter::ParseUriString(state.mpRequest->Query());
    HttpdCgiListSymbols symbols(p_params);
    
    (void )HttpdFSTemplateShell::Execute(state, &symbols);
    HttpdCgiParameter::FreeList(p_params);
  }
  else
    state.mpRequest->Respond(HTTPD_RESP_SRV_ERROR);
}

//***********************************************************************

//---------------------------------------------------------------------------
//
// NAME:           failure
//
// DESCRIPTION:    Handle a fatal error.
//
// AUTHOR:         S. Conner
// CREATED:        Mar  6, 2004
// LAST MODIFIED:  N/A           BY:  N/A
//
//--------------------------------------------------------------------------
static void failure(const char *p_file, int line, const char *p_msg)
{
  assert(p_file != NULL);
  assert(line   >  0);
  assert(p_msg  != NULL);
  
  fprintf(stderr, "%s:%d Call to %s failed\n", p_file, line, p_msg);
  exit(EXIT_FAILURE);
}

//---------------------------------------------------------------------------
//
// NAME:           Initialize_The_Web_Server
//
// DESCRIPTION:    Start and configure Seminole.
//
// AUTHOR:         M. Grosberg
// CREATED:        Jun  7, 2006
// LAST MODIFIED:  N/A           BY:  N/A
//
//--------------------------------------------------------------------------
static void Initialize_The_Web_Server()
{
  //-------------------------------------------------------
  // Initialize the environment.
  //-------------------------------------------------------
  
  if (Httpd::Init() != 0)
    failure(__FILE__, __LINE__, "Httpd::Init()");
  
  //--------------------------------------------------------
  // Create the webserver instance.  This just initializes
  // the object --- it doesn't start it up.
  //--------------------------------------------------------
    
  gpWebServer = new Httpd(gServername, gServerport);
  if (gpWebServer == NULL)
    failure(__FILE__, __LINE__, "Httpd::Httpd()");

  static HttpdMemoryDataSource builtin(SemWeb, sizeof(SemWeb));
  static HttpdRomFileSystem    romfs;
  if (romfs.Mount(&builtin) != 0)
    failure(__FILE__, __LINE__, "HttpdRomFileSystem::Mount()");
    
  static DemoHandler hand(&romfs, "/", "/");
  gpWebServer->Install(&hand);

  printf("Server started on host %s port %d\n", gServername, gServerport);
}

//---------------------------------------------------------------------------
//
// NAME:           main
//
// DESCRIPTION:    Entry point for the demo.
//
// AUTHOR:         S. Conner
// CREATED:        Mar  6, 2004
// LAST MODIFIED:  N/A           BY:  N/A
//
//--------------------------------------------------------------------------
int main(int argc, const char *const *argv)
{
  char buf[4];

  Initialize_The_Web_Server();
  gpWebServer->Start();
  
  printf("Press <ENTER> to stop.\n");
  fgets(buf, sizeof(buf), stdin);
  
#if HTTPD_INC_SHUTDOWN
  gpWebServer->Stop(HARD);
#endif

  return (EXIT_SUCCESS);
}
