#!/usr/bin/env ruby

require 'rubygems'
require 'abiquo-etk'

def create_schemas(user = 'root', password = '')
  log = AETK::Log.instance
  cmd = ''
  if password.strip.chomp.empty? 
    cmd = "mysql -u #{user} "
  else
    cmd = "mysql -u #{user} -p#{password} "
  end

  if `#{cmd} -e 'show databases'|grep kinton`.strip.chomp.empty?
    out = `mysql -u root < /usr/share/doc/abiquo-server/database/kinton-schema.sql`
    if $?.exitstatus == 0 
      log.info 'kinton-schema imported succesfully.'
    else
      log.error "Error importing kinton-schema: #{out}"
    end
    out = `mysql -u root < /usr/share/doc/abiquo-server/database/kinton-premium-schema.sql`
    if $?.exitstatus == 0 
      log.info 'kinton-premium-schema imported succesfully.'
    else
      log.error "Error importing kinton-premium-schema: #{out}"
    end
  else
    log.warn 'kinton schema found. Skipping schema creation.'
  end
  
end

def init_server_install
  config_file ='/etc/sysconfig/abiquo-server'
  log = AETK::Log.instance

  begin
    if File.exist? config_file 
      @settings = abiquo_server_settings
    else
      log.error "Config file #{config_file} does not exist. Exit."
      exit
    end

    log.info "Setting EventSink URL"
    `abicli set event-sink-url http://#{@settings['abiquo_server_ip']}/server/EventSink`
    
    log.info "Setting DB Properties"
    `abicli set database-host #{@settings['abiquo_db_host']}`
    `abicli set database-user root`
    `abicli set database-password #{@settings['abiquo_db_password']}`
    `abicli set mail-server 127.0.0.1`
      
    log.info "Creating database schemas..."
    create_schemas
  rescue Exception => e
    log.error "Unhandled exception: #{e.message}"
    log.error "Unhandled exception: #{e.backtrace}"
  end
end

def init_remote_services_install
  config_file ='/etc/sysconfig/abiquo-rs'

  log = AETK::Log.instance

  begin
    @settings = {}
    if File.exist? config_file
      @settings = abiquo_rs_settings
    else
      log.error "Config file #{config_file} does not exist. Exit."
      exit
    end
    repo = @settings['abiquo_nfs_repository']
    if repo =~ /localhost|127.0.0.1/
      if File.exist? '/etc/sysconfig/abiquo-server'
        log.info "NFS Repository points to localhost, fixing..."
        s = abiquo_server_settings
        repo = s['abiquo_server_ip'] + ':/opt/vm_repository'
      end
    end
    log.info "Setting nfs-repository to #{repo}"
    `abicli set nfs-repository #{repo}`
    log.info "Setting cifs-repository to #{repo}"
    `abicli set cifs-repository //your-cifs-server-ip-here/opt/vm_repository`

  rescue Exception => e
    log.error "Unhandled exception: #{e.message}"
    log.error "Unhandled exception: #{e.backtrace}"
  end
end

def init_v2v_install
  config_file ='/etc/sysconfig/abiquo-server'
  log = AETK::Log.instance
end

def init_rs_plus_v2v_install
  init_remote_services_install
  init_v2v_install
end

log = AETK::Log.instance
log.info "Running abiquo-initenv..."
case AETK::System.detect_install_type 
when :monolithic
  log.info "Monolithic install detected. Setting up the environment."
  init_server_install
  init_remote_services_install
  init_v2v_install
when :server
  log.info "Server install detected. Setting up the environment."
  init_server_install
when :remote_services
  log.info "Remote Services install detected. Setting up the environment."
  init_remote_services_install
when :v2v
  log.info "V2V install detected. Setting up the environment."
  init_v2v_install
when :rs_plus_v2v
  log.info "Remote Services + V2V install detected. Setting up the environment."
  init_rs_plus_v2v_install
else
  AETK::Log.instance.error "Couldn't detect install type. Skipping first setup."
end

#
# Try to apply netapp ontap connector settings
#
if File.exist?('/opt/abiquo/ontap/tomcat/webapps/ROOT/WEB-INF/classes/config.xml')
  AETK::Log.info "Abiquo ONTAP connector found."
  if File.exist?('/etc/sysconfig/abiquo-rs')
    buf = File.read '/etc/sysconfig/abiquo-rs'
    buf.each_line do |l|
      if l =~ /ontap_user/
        user = l.split('=').last.strip.chomp rescue ''
        AETK::Log.info "ONTAP: Configure ontap-user"
        `abicli set ontap-user #{user}`
      end
    end
    buf.each_line do |l|
      if l =~ /ontap_password/
        password = l.split('=').last.strip.chomp rescue ''
        AETK::Log.info "ONTAP: Configure ontap-password"
        `abicli set ontap-password #{password}`
      end
    end
    buf.each_line do |l|
      if l =~ /ontap_server_ip/
        server = l.split('=').last.strip.chomp rescue ''
        AETK::Log.info "ONTAP: Configure ontap-server-ip"
        `abicli set ontap-server-ip #{server}`
      end
    end
  else
    AETK::Log.warn "/etc/sysconfig/abiquo-rs config file not found. Skipping ONTAP configuration."
  end
else
  AETK::Log.info "Abiquo ONTAP connector NOT found."
end
