
if ENV['BUILDWISE_HOME']
  if Dir.exist?(ENV['BUILDWISE_HOME'])
    BUILDWISE_HOME = File.expand_path(ENV['BUILDWISE_HOME'])
  else
    begin 
      FileUtils.mkdir_p(ENV['BUILDWISE_HOME'])
      BUILDWISE_HOME = File.expand_path(ENV['BUILDWISE_HOME'])
    rescue => e

    end
  end
end




unless defined?(BUILDWISE_HOME)  
  if defined?(RACK_ENV) && RACK_ENV == "integration"
    BUILDWISE_HOME = "/opt/buildwise-integration/.buildwise"
  end  
end


unless defined?(BUILDWISE_HOME)  
  if defined?(RACK_ENV) && RACK_ENV == "testwisely"
    BUILDWISE_HOME = "/opt/buildwise-testwisely-working/.buildwise"
  end  
end

unless defined?(BUILDWISE_HOME)
  if ENV['USERPROFILE'] && File.exist?(ENV['USERPROFILE'])
    BUILDWISE_HOME = File.expand_path(File.join(ENV['USERPROFILE'], ".buildwise"))
  else
    BUILDWISE_HOME = File.expand_path("~/.buildwise")
  end
end



if !defined?(BUILDWISE_HOME)

  if ENV['BUILDWISE_HOME']
    if Dir.exist?(ENV['BUILDWISE_HOME'])
      BUILDWISE_HOME = File.expand_path(ENV['BUILDWISE_HOME'])
    else
      begin 
        FileUtils.mkdir_p(ENV['BUILDWISE_HOME'])
        BUILDWISE_HOME = File.expand_path(ENV['BUILDWISE_HOME'])
      rescue => e

      end
    end
  end


  unless defined?(BUILDWISE_HOME)  
    if RACK_ENV == "integration"
      BUILDWISE_HOME = "/opt/buildwise-integration/.buildwise"
    end  
  end

  unless defined?(BUILDWISE_HOME)
    if ENV['USERPROFILE'] && File.exist?(ENV['USERPROFILE'])
      BUILDWISE_HOME = File.expand_path(File.join(ENV['USERPROFILE'], ".buildwise"))
    else
      BUILDWISE_HOME = File.expand_path("~/.buildwise")
    end
  end

  FileUtils.mkdir(BUILDWISE_HOME) unless Dir.exist?(BUILDWISE_HOME)
  config_dir = File.join(BUILDWISE_HOME, "config")
  FileUtils.mkdir(config_dir) unless File.exist?(config_dir)
end

CONFIG_FILE = "#{BUILDWISE_HOME}/config.yml" unless defined?(CONFIG_FILE)

CONFIG_SERVER_DIGEST_FILE = File.expand_path File.join(File.dirname(__FILE__), "..", "..", "config", "server.digest")
begin

  if File.exist?(CONFIG_SERVER_DIGEST_FILE)
    digest_str = File.read(CONFIG_SERVER_DIGEST_FILE)
    if digest_str.strip.size <= 10 &&  digest_str.strip.size >= 8 # look likes a digest
      SERVER_DIGEST = digest_str.strip
      puts("[Info] BuildWise Server is starting (#{SERVER_DIGEST})...")      
    end    
  end
rescue => e
  puts "Error: #{e}"

end


if !defined?(SERVER_DIGEST) &&  ENV['SERVER_DIGEST']
  SERVER_DIGEST = ENV['SERVER_DIGEST'].strip
end

if !defined?(SERVER_DIGEST)
  SERVER_DIGEST = "abcde12345" # default one, if you want to customize, put it in config/server.digest file
end


MAX_DISPLAY_LOG_KB=850





require File.join(File.dirname(__FILE__), "version.rb")