# Copyright (C) 2003, 2004 Gregoire Lejeune <gregoire.lejeune@free.fr>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

require 'asp/compile'
require 'asp/common'
require 'asp/globalasa'
require 'asp/asperror'
require 'asp/server'
require 'asp/application'
require 'asp/session'
require 'asp/request'
require 'asp/response'

module Asp
  class AspFile
    def initialize( xURI )
      @_URI = xURI
    end
    
    def status
      return 200 
    end
    
    def filename
      ## TODO !!!
      return( @_URI )
    end
  end
  
  class AspServer
    def initiliase
    end
    
    def log_error( xMessage, *lvArgs )
lvArgs.each{ |v|
  $stderr.print v,"\n"
}
	  $stderr.print xMessage, "\n"
    end
  end
  
  class AspRequest
    def initialize( _asp )
	  @_asp = _asp
	  @_hHeader = Hash::new
      @_status = 200
    end
    
    def lookup_file( path )
      return( Asp::AspFile.new( path ) )
    end
    
	def uri
	  ENV['REQUEST_URI']
	end

    def server
      return( Asp::AspServer.new() )
    end
    
    def escape_html( xData )
      return xData.to_s.gsub(/&/, "&amp;").gsub(/\"/, "&quot;").gsub(/>/, "&gt;").gsub(/</, "&lt;")
    end
    
    def status=( iState )
      @_status = iState
    end
    
    def status
      @_status
    end

	def filename
	  return( @_asp.file )
	  #return( ENV["REQUEST_URI"] )
	end

	def headers_in
	  @_hHeader['Cookie'] = ENV['HTTP_COOKIE']
	  @_hHeader['Content-Length'] = -1
	  return( @_hHeader )
	end

	def request_method
	  ENV["REQUEST_METHOD"]
	end
  end
  
  class AspHandler
    def initialize
    end
    
    def load_code( code, filename = nil )
      code.untaint
      if filename != nil
        filename.untaint
        eval(code, binding(), filename).untaint
      else
        eval(code, binding()).untaint
      end
    end
  end
  
  def Asp::serve( _asp )
    @compiler = AspCompiler.new
    @compiler.put_cmd = 'print'
    @_asp = _asp
    
    oAspRequest = Asp::AspRequest.new( @_asp )
    oAspHandler = Asp::AspHandler.new()

    code = Asp::compile
    
    ## Initialise Ruby/ASP objects
    __oASPError = Asp::Error::new( )
    __oASPError.source = @compiler.init_code
    __oASPError.file = @_asp.file
    
    $Server = Asp::Server::new( oAspRequest, __oASPError, oAspHandler )
    $Application = Asp::Application::new( oAspRequest, __oASPError, oAspHandler )
    $Session = Asp::Session::new( oAspRequest, __oASPError, oAspHandler )
    $Request = Asp::Request::new( oAspRequest, __oASPError, oAspHandler )
    $Response = Asp::Response::new( oAspRequest, __oASPError, oAspHandler )
    # ...
    
    oGAsa = Asp::GlobalASA::new( Asp::Common::globalASA, oAspRequest, oAspHandler ) 
    if oGAsa.getAsaCode == false 
      @_asp.debug( oAspRequest, code, @_asp.file, 0, "Can't load global.asa file" )
    end
    
    _asp.asp = oAspHandler
	_asp.request = oAspRequest
    _asp.body = code
  end

  def Asp::compile
    return @compiler.compile( @_asp.src )
  end

  def Asp::compiler
    @compiler
  end

end
