module Puppet
  newtype(:cobbler_profile) do
  @doc = "Set up a cobbler profile"

  ensurable

  newparam(:name, :namevar => true) do
    desc "The name of the profile"
  end
  newparam(:dhcp) do
    desc "DHCP Tags"
  end
  newparam(:distro) do
    desc "The distro this brofile is based on"
  end
  newparam(:comment) do
    desc "A comment for the profile"
  end
  newparam(:pxe) do
    desc "Should the profile show up in the pxe menu?"
  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 profile"
  end
  newparam(:nssearch) do
    desc "The search domain"
  end
  newparam(:mgmtclasses) do
    desc "A list of classes to pass to a configuration management system"
  end
  newparam(:repos) do
    desc "List of repos to apply to the profile"
  end
  newparam(:serveroverride) do
    desc "Server Overide"
  end
  newparam(:virtautoboot) do
    desc "Flag to set the profile 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_distro) do
    @parameters[:distro].value
  end
  autorequire(:cobbler_repo) do
    @parameters[:repos].value.split(' ')
  end
end
end

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

  def create
    opts = {
      :name => '--name',
      :dhcp => '--dhcp-tag',
      :distro => '--distro',
      :comments => '--comment',
      :pxe => '--enable-menu',
      :kickstart => '--kickstart',
      :kopts => '--kopts',
      :koptspost => '--kopts-post',
      :ksmeta => '--ksmeta',
      :nameservers => '--name-servers',
      :nssearch => '--name-servers-search',
      :mgmtclasses => '--mgmt-classes',
      :repos => '--repos',
      :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 profile edit '
    if `cobbler profile report --name=#{@resource[:name]}`.match(/^No/)
      cmd = 'cobbler profile add '
    end
    @resource.to_hash.each do |key, value|
      opt = opts[key]
      if opt
        cmd << " #{opt}='#{value}'"
      end
    end
    `#{cmd}`
  end

  def destroy
    `cobbler profile remove --name=#{@resource[:name]}`
  end
    
  def exists?
    rep = `cobbler profile report --name=#{@resource[:name]}`
    if rep.match(/^No/)
      return false
    end
    avail = { 
      "Name" => :name,
      "DHCP Tag" => :dhcp,
      "Distribution" => :distro,
      "Comment" => :comment,
      "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
