module Puppet
  newtype(:cobbler_system) do
  @doc = "Set up a cobbler system"

  ensurable

  newparam(:name, :namevar => true) do
    desc "The name of the system"
  end
  newparam(:profile) do
    desc "The profile this system is based on"
  end
  newparam(:comment) do
    desc "A comment for the system"
  end
  newparam(:kickstart) do
    desc "Path to the kickstart file"
  end
  newparam(:kopts) do
    desc "Kernel options used durring an install"
  end
  newparam(:ksmeta) do
    desc "The meta paramaters to pass to the kickstart"
  end
  newparam(:koptspost) do
    desc "Kernel options to append to the installed system"
  end
  newparam(:nameservers) do
    desc "List of nameservers to assign to the system"
  end
  newparam(:nssearch) do
    desc "The search domain"
  end
  newparam(:gateway) do
    desc "The search domain"
  end
  newparam(:hostname) do
    desc "The search domain"
  end
  newparam(:image) do
    desc "The search domain"
  end
  newparam(:mgmtclasses) do
    desc "A list of classes to pass to a configuration management system"
  end
  newparam(:serveroverride) do
    desc "Server Overide"
  end
  newparam(:virtautoboot) do
    desc "Flag to set the system to auto start"
  end
  newparam(:virtbridge) do
    desc "The network bridge to bond to"
  end
  newparam(:virtcpus) do
    desc "The number of virtual cpus"
  end
  newparam(:virtfilesize) do
    desc "The virtual file size to use, in GB"
  end
  newparam(:virtpath) do
    desc "The path to the virtual hard drive(s)"
  end
  newparam(:virtram) do
    desc "Amount of ram for the vm, in MB"
  end
  newparam(:virttype) do
    desc "The type of virtual machine, xen, qemu, xenfv, vmware"
  end
  autorequire(:cobbler_profile) do
    [@parameters[:profile].value]
  end
end
end

Puppet::Type.type(:cobbler_system).provide(:rhcobbler_system) do
  desc "Red Hat support for cobbler, should work on any cobbler install"

  def create
    opts = {
      :name => '--name',
      :profile => '--profile',
      :image => '--image',
      :comments => '--comment',
      :kickstart => '--kickstart',
      :kopts => '--kopts',
      :koptspost => '--kopts-post',
      :ksmeta => '--ksmeta',
      :nameservers => '--name-servers',
      :nssearch => '--name-servers-search',
      :gateway => '--gateway',
      :hostname => '--hostname',
      :mgmtclasses => '--mgmt-classes',
      :serveroverride => '--server',
      :virtautoboot => '--virt-auto-boot',
      :virtbridge => '--virt-bridge',
      :virtcpus => '--virt-cpus',
      :virtfilesize => '--virt-file-size',
      :virtpath => '--virt-path',
      :virtram => '--virt-ram',
      :virttype => '--virt-type',
      }
    cmd = 'cobbler system edit '
    if `cobbler system report --name=#{@resource[:name]}`.match(/^No/)
      cmd = 'cobbler system add '
    end
    @resource.to_hash.each do |key, value|
      opt = opts[key]
      if opt
        cmd << " #{opt}='#{value}'"
      end
    end
    `#{cmd}`
  end

  def destroy
    `cobbler system remove --name=#{@resource[:name]}`
  end
    
  def exists?
    rep = `cobbler system report --name=#{@resource[:name]}`
    if rep.match(/^No/)
      return false
    end
    avail = { 
      "Name" => :name,
      "Profile" => :profile,
      "Image" => :image,
      "Comment" => :comment,
      "Gateway" => :gateway,
      "Hostname" => :hostname,
      "Kernel Options" => :kopts,
      "Kernel Options (Post Install)" => :koptspost,
      "Kickstart Metadata" => :ksmeta,
      "Management Classes" => :mgmtclasses,
      "Name Servers" => :nameservers,
      "Name Servers Search Path" => :nssearch,
      "Server Override" => :serveroverride,
      "Virt Auto Boot" => :virtautoboot,
      "Virt Bridge" => :virtbridge,
      "Virt CPUs" => :virtcpus,
      "Virt File Size(GB)" => :virtfilesize,
      "Virt Path" => :virtpath,
      "Virt RAM (MB)" => :virtram,
      "Virt Type" => :virttype,
      }
    rep.each do |line|
      val = avail[line.split(":")[0].strip]
      unless val
        next
      end
      unless @resource[val]
        next
      end
      if val
        stat = line.split(":")[1].strip
        if stat.match(/^\{/)
          stat.gsub!('{', '')
          stat.gsub!('}', '')
          stat.gsub!('\'', '')
          stat.gsub!(': ', '=')
          stat.gsub!(',', ' ')
        elsif stat.match(/^\[/)
          stat.gsub!('[', '')
          stat.gsub!(']', '')
          stat.gsub!('\'', '')
          stat.gsub!(' ', '')
          stat.gsub!(',', ' ')
        end
        unless @resource[val] == stat
          return false
        end
      end
    end
    return true
  end
end
