#!/usr/bin/env ruby
require 'logger'

CONFIG_FILE='/etc/sysconfig/abiquo-server'

Log = Logger.new('/var/log/abiquo-etk.log')
Log.level = Logger::INFO

begin
	@settings = {}
	if File.exist? CONFIG_FILE
		File.read(CONFIG_FILE).each_line do |l|
			next if l.strip.chomp.empty?
			key,val = l.strip.chomp.split('=')
			@settings[key.strip.chomp] = val.strip.chomp rescue ''
		end

	else
		Log.error "Config file #{CONFIG_FILE} does not exist. Exit."
		exit
	end

	Log.info "Setting db user/password in localhost bpm-async context.xml"
  `sed -i 's/localhost/0\.0\.0\.0/' /opt/abiquo/tomcat/webapps/bpm-async/WEB-INF/classes/activemq.xml`
  `abicli set database-host #{@settings['abiquo_db_host']}`
  `abicli set database-user root`
  `abicli set database-password #{@settings['abiquo_db_password']}`
rescue Exception => e
	Log.error "Unhandled exception: #{e.message}"
	Log.error "Unhandled exception: #{e.backtrace}"
end


