#
# gpcupsconfig.rb
# Encapsulates external configuration data and constants.
#
# Copyright (c) 2003-2014 GoPrint Systems, Inc.  All rights reserved.
#

# This assumes that GS4Home is set and ends properly with a '/'
require GS4Home + 'lib/gpconfig'

# The name of the config file is a set constant.  This can't change.
GoPrintConfigFilename = "goprint.cfg"
LogFileDirectory = "logs"
LoggingFileName = "cups.log"

DefaultAgentBackendMonitorPort		= 6332
DefaultAgentBackendTimeout		= 3
DefaultAgentListenerMonitorPort		= 6333
DefaultAgentListenerTimeout		= 3

DefaultAgentBackendInfoDelimiter	= "|"
DefaultAgentListenerInfoDelimiter	= "|"
DefaultAgentListenerDummyPrinterName	= "//"

# The names of the properties to read from the config file.
AgentBackendMonitorPortProperty		= "agent.cups.backend.port"
AgentBackendTimeoutProperty		= "agent.cups.backend.timeout"
AgentBackendInfoDelimiterProperty	= "agent.cups.backend.delimiter"
AgentListenerMonitorPortProperty	= "agent.cups.listener.port"
AgentListenerTimeoutProperty		= "agent.cups.listener.timeout"
AgentListenerDummyPrinterNameProperty	= "agent.cups.listener.dummyprinter"
AgentListenerInfoDelimiterProperty	= "agent.cups.listener.delimiter"
LoggingEnabledProperty			= "agent.cups.logging"

class GoPrintCUPSConfiguration
	def initialize
		# Load the properties
		properties = loadProperties(GS4Home + GoPrintConfigFilename)

		@agentBackendMonitorPort = properties[AgentBackendMonitorPortProperty]
		@agentBackendMonitorPort = DefaultAgentBackendMonitorPort if @agentBackendMonitorPort.nil?
		@agentBackendTimeout = properties[AgentBackendTimeoutProperty]
		@agentBackendTimeout = @agentBackendTimeout.to_i unless @agentBackendTimeout.nil?
		@agentBackendTimeout = DefaultAgentBackendTimeout if @agentBackendTimeout.nil?
		@agentBackendInfoDelimiter = properties[AgentBackendInfoDelimiterProperty]
		@agentBackendInfoDelimiter = DefaultAgentBackendInfoDelimiter if @agentBackendInfoDelimiter.nil?
		@agentListenerMonitorPort = properties[AgentListenerMonitorPortProperty]
		@agentListenerMonitorPort = DefaultAgentListenerMonitorPort if @agentListenerMonitorPort.nil?
		@agentListenerTimeout = properties[AgentListenerTimeoutProperty]
		@agentListenerTimeout = @agentListenerTimeout.to_i unless @agentListenerTimeout.nil?
		@agentListenerTimeout = DefaultAgentListenerTimeout if @agentListenerTimeout.nil?
		@agentListenerDummyPrinterName = properties[AgentListenerDummyPrinterNameProperty]
		@agentListenerDummyPrinterName = DefaultAgentListenerDummyPrinterName if @agentListenerDummyPrinterName.nil?
		@agentListenerInfoDelimiter = properties[AgentListenerInfoDelimiterProperty]
		@agentListenerInfoDelimiter = DefaultAgentListenerInfoDelimiter if @agentListenerInfoDelimiter.nil?

		@loggingEnabled = properties[LoggingEnabledProperty]
		@loggingEnabled = false if @loggingEnabled.nil?
		@logFileName = GS4Home + LogFileDirectory + "/" + LoggingFileName

		# Determine the location of the CUPS Spool Directory		
		@cupsSpoolDirectory = String.new(ENV['CUPS_REQUESTROOT']) unless ENV['CUPS_REQUESTROOT'] == nil
		if(@cupsSpoolDirectory != nil && @cupsSpoolDirectory[@cupsSpoolDirectory.length] != "/")
			@cupsSpoolDirectory << "/"
		end

		# Determine the name of this backend and the run path
		@runPath = ""
		@commandName = $0
		while @commandName.include? "/"
			@runPath << @commandName[0, @commandName.index("/")+1]
			@commandName = @commandName[@commandName.index("/")+1, @commandName.size]
		end
	end

	attr_reader :agentBackendMonitorPort, :agentBackendTimeout, :agentBackendInfoDelimiter, 
	:agentListenerMonitorPort, :agentListenerMonitorPort, :agentListenerTimeout,
	:agentListenerDummyPrinterName, :agentListenerInfoDelimiter,
	:loggingEnabled, :logFileName, :cupsSpoolDirectory,
	:runPath, :commandName
end

# Create the global configuration object
Configuration = GoPrintCUPSConfiguration.new
