
class dhcp::server::common {

    package { "dhcp":
	name   => $operatingsystem ? {
            Debian  => "dhcp3-server",
	    OpenBSD => "isc-dhcp-server",
            Ubuntu  => "dhcp3-server",
	    default => "dhcp",
	},
	ensure => installed,
    }

    file { "dhcpd.leases":
	name    => $operatingsystem ? {
            Debian  => "/var/lib/dhcp3/dhcpd.leases",
	    OpenBSD => "/var/db/dhcpd.leases",
            Ubuntu  => "/var/lib/dhcp3/dhcpd.leases",
	    default => "/var/lib/dhcpd/dhcpd.leases",
	},
	ensure  => present,
	owner   => $operatingsystem ? {
            debian  => dhcpd,
            ubuntu  => dhcpd,
            default => root,
        },
	group   => $operatingsystem ? {
            Debian  => dhcpd,
	    OpenBSD => wheel,
            Ubuntu  => dhcpd,
	    default => root,
	},
	require => Package["dhcp"],
	before  => Service["dhcpd"],
    }

    service { "dhcpd":
        name    => $operatingsystem ? {
            Debian  => "dhcp3-server",
            OpenBSD => "isc-dhcpd",
            Ubuntu  => "dhcp3-server",
            default => "dhcpd",
        },
	ensure  => running,
	enable  => true,
        binary  => $operatingsystem ? {
            OpenBSD => "/usr/local/sbin/dhcpd",
            default => undef,
        },
        start   => $operatingsystem ? {
            OpenBSD => "/usr/local/sbin/dhcpd -q",
            default => undef,
        },
	require => Package["dhcp"],
    }

}


class dhcp::server inherits dhcp::server::common {

    file { "/etc/dhcpd.conf":
        name   => $operatingsystem ? {
            debian  => "/etc/dhcp3/dhcpd.conf",
            ubuntu  => "/etc/dhcp3/dhcpd.conf",
            default => "/etc/dhcpd.conf",
        },
        ensure => present,
        source => [ "puppet:///files/dhcp/dhcpd.conf.${fqdn}",
                    "puppet:///files/dhcp/dhcpd.conf", ],
        mode   => 0644,
        owner  => root,
        group  => $operatingsystem ? {
            openbsd => wheel,
            default => root,
        },
        notify => Service["dhcpd"],
    }
  
}


class dhcp::server::ldap inherits dhcp::server::common {

    include ldap::client::python

    file { "/usr/local/sbin/dhcpdump.py":
	ensure  => present,
	source  => "puppet:///modules/dhcp/dhcpdump.py",
	mode    => 0755,
	owner   => root,
	group   => $operatingsystem ? {
	    OpenBSD => wheel,
	    default => root,
	},
    }

    file { "/etc/dhcpd.conf.in":
	ensure  => present,
	source  => [ "puppet:///files/dhcp/dhcpd.conf.in.${hostname}",
		     "puppet:///files/dhcp/dhcpd.conf.in", ],
	mode    => 0644,
	owner   => root,
	group   => $operatingsystem ? {
	    OpenBSD => wheel,
	    default => root,
	},
	require => Package["dhcp"],
    }

    exec { "generate-dhcp-conf":
	path    => "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin",
	command => "dhcpdump.py /etc/dhcpd.conf.in* > /etc/dhcpd.conf",
	onlyif  => "! dhcpdump.py /etc/dhcpd.conf.in* | diff /etc/dhcpd.conf -",
	require => [ File["/etc/dhcpd.conf.in"],
		     File["/usr/local/sbin/dhcpdump.py"], ],
	notify  => Service["dhcpd"],
    }

}
