# Install Sendmail packages.
#
class sendmail::common {

    if !$mail_domain and $domain {
        $mail_domain = $domain
    }

    if $operatingsystem != "OpenBSD" {
        package { "sendmail":
            name   => [ "sendmail", "sendmail-cf", ],
            ensure => installed,
        }
    }

}


# Configure Sendmail submission.
#
# === Global variables
#
#   $mail_server:
#       Hostname of mail server.
#
class sendmail::client inherits sendmail::common {

    if !$mail_server {
        $mail_server = "127.0.0.1"
    }

    case $operatingsystem {
        "openbsd": {
            file { "/usr/share/sendmail/cf/submit.mc":
                ensure  => present,
                content => template("sendmail/submit.mc.erb"),
                owner   => "root",
                group   => "bin",
                mode    => 0444,
                notify  => Exec["make submit.cf"],
            }
        }
        default: {
            file { "/etc/mail/submit.mc":
                ensure  => present,
                content => template("sendmail/submit.mc.erb"),
                owner   => "root",
                group   => "root",
                mode    => 0644,
                notify  => Exec["make submit.cf"],
                require => Package["sendmail"],
            }
        }
    }

    exec { "make submit.cf":
        path    => "/bin:/usr/bin:/sbin:/usr/sbin",
        cwd     => $operatingsystem ? {
            "openbsd" => "/usr/share/sendmail/cf",
            default   => "/etc/mail",
        },
        refreshonly => true,
    }

    file { "/etc/mail/submit.cf":
        ensure  => present,
        owner   => "root",
        group   => $operatingsystem ? {
            "openbsd" => "wheel",
            "ubuntu"  => "smmsp",
            default   => "root",
        },
        mode    => 0644,
        source  => $operatingsystem ? {
            "openbsd" => "/usr/share/sendmail/cf/submit.cf",
            default   => undef,
        },
        require => Exec["make submit.cf"],
    }

}

# Configure Sendmail server.
#
#   $sendmail_domain:
#       SMTP domain name.
class sendmail::server inherits sendmail::common {

    include procmail

    case $operatingsystem {
        centos,redhat: {
            $sendmail_ostype = "linux"
            $group = "root"
            file { "/etc/sysconfig/sendmail":
                ensure  => present,
                content => "DAEMON=yes\nQUEUE=1h\n",
                owner   => "root",
                group   => $group,
                mode    => 0644,
                notify => Service["sendmail"],
            }
        } ubuntu: {
            fail("Operating system not supported.")
        } openbsd: {
            $sendmail_ostype = "openbsd"
            $group = "wheel"
        }
    }

    file { "sendmail.mc": name => $operatingsystem ? { 
        "openbsd" => "/usr/share/sendmail/cf/sendmail.mc", 
        default => "/etc/mail/sendmail.mc", },
        content => template("sendmail/sendmail.mc.erb"),
        owner   => "root",
        group   => $group,
        mode    => 0644,
        notify  => Exec["make sendmail.cf"],
    }

    file { "/etc/aliases": 
        ensure  => present,
        notify => Exec["newaliases"],
        owner   => "root",
        group   => $group,
        mode    => 0644,
    } 

    exec { "make sendmail.cf":
        path    => "/bin:/usr/bin:/sbin:/usr/sbin",
        cwd     => $operatingsystem ? {
            openbsd => "/usr/share/sendmail/cf",
            default => "/etc/mail",
        },
        require => $operatingsystem ? {
            openbsd => undef,
            default => Package["sendmail"],
        },
        refreshonly => true,
    }

    file { "/etc/mail/sendmail.cf":
        ensure  => present,
        owner   => "root",
        group   => $group,
        mode    => 0644,
        source  => $operatingsystem ? {
            openbsd => "/usr/share/sendmail/cf/sendmail.cf",
            default => undef,
        },
        require => Exec["make sendmail.cf"],
        notify => Service["sendmail"],
    }

    service { "sendmail":
        ensure  => running,
        enable  => true,
        require => File["/etc/mail/sendmail.cf"],
    }

    exec { "newaliases":
        path    => "/bin:/usr/bin:/sbin:/usr/sbin",
        refreshonly => true,
    }
}
