# Install ejabberd.
#
# === Global variables
#
#   $ejabberd_hosts:
#       Array of domains serverd by ejabberd. Defaults to [ "$homename" ].
#
#   $ejabberd_admin:
#       Array of users with admin privileges.
#
class ejabberd {

    if !$ejabberd_hosts {
        $ejabberd_hosts = [ "${homename}" ]
    }
    if !$ejabberd_admin {
        $ejabberd_admin = []
    }

    package { "ejabberd":
        ensure => installed,
    }

    service { "ejabberd":
        ensure => running,
        enable => true,
    }

    file { "/etc/ejabberd/ejabberd.pem":
        ensure  => present,
        mode    => 0600,
        owner   => ejabberd,
        group   => ejabberd,
        require => Package["ejabberd"],
    }

    file { "/etc/ejabberd/ejabberd.cfg":
        ensure  => present,
        mode    => 0600,
        owner   => ejabberd,
        group   => ejabberd,
        content => template("ejabberd/ejabberd.cfg.erb"),
        require => Package["ejabberd"],
        notify  => Service["ejabberd"],
    }

    if $ejabberd_webhosts {
        file { "/var/www/jabber":
            ensure => directory,
            mode   => 0755,
            owner  => "root",
            group  => "root",
        }

        file { "/var/www/jabber/.htaccess":
            ensure  => present,
            mode    => 0644,
            owner   => "root",
            group   => "root",
            source  => "puppet:///modules/ejabberd/htaccess",
            require => File["/var/www/jabber"],
        }

        apache::configfile { "ejabberd.conf":
            http   => false,
            source => "puppet:///modules/ejabberd/ejabberd-httpd.conf",
        }

        define configwebhost() {
            file { "/srv/www/https/${name}/jabber":
                ensure  => link,
                target  => "/var/www/jabber",
                require => File["/srv/www/https/${name}"],
            }
        }

        configwebhost { $ejabberd_webhosts: }
    }
}
