# Install vsroom.
#
class vsroom {

    require abusehelper

    file { "/usr/local/src/vsroom.tar.gz":
        ensure => present,
        mode   => 0644,
        owner  => root,
        group  => root,
        source => "puppet:///files/packages/vsroom.tar.gz",
        links  => follow,
    }
    util::extract::tar { "/usr/local/src/vsroom":
        strip   => 1,
        source  => "/usr/local/src/vsroom.tar.gz",
        require => File["/usr/local/src/vsroom.tar.gz"],
        before  => Python::Setup::Install["/usr/local/src/vsroom"],
    }
    python::setup::install { "/usr/local/src/vsroom":
        python  => $operatingsystem ? {
            centos  => "python2.6",
            default => "python",
        },
        require => $operatingsystem ? {
            centos  => Package["python26"],
            default => undef,
        },
    }
    file { "/usr/local/bin/vsroomctl":
        ensure  => present,
        mode    => 0755,
        owner   => root,
        group   => root,
        source  => "/usr/local/src/vsroom/contrib/vsroomctl/vsroomctl",
        require => Util::Extract::Tar["/usr/local/src/vsroom"],
    }
    file { "/usr/local/bin/vsroompasswd":
        ensure  => present,
        mode    => 0755,
        owner   => root,
        group   => root,
        source  => "/usr/local/src/vsroom/contrib/vsroompasswd/vsroompasswd",
        require => Util::Extract::Tar["/usr/local/src/vsroom"],
    }

    include user::system
    realize(User["vsroom"], Group["vsroom"])

    if $vsroom_datadir {
        file { "${vsroom_datadir}":
            ensure  => directory,
            mode    => 0755,
            owner   => root,
            group   => root,
        }

        file { "/var/lib/vsroom":
            ensure  => link,
            target  => "${vsroom_datadir}",
            require => File["${vsroom_datadir}"],
        }
    } else {
        file { "/var/lib/vsroom":
            ensure  => directory,
            mode    => 0755,
            owner   => root,
            group   => root,
        }
    }

    file { "/var/lib/vsroom/.htpasswd":
        ensure  => present,
        mode    => 0640,
        owner   => root,
        group   => $apache::sslserver::group,
        require => File["/var/lib/vsroom"],
    }

    file { "/var/www/vsroom":
        ensure => directory,
        mode   => 0755,
        owner  => "root",
        group  => "root",
    }

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

    file { "/var/www/vsroom/config.json":
        ensure  => present,
        mode    => 0644,
        owner   => "root",
        group   => "root",
        content => "{ \"boshUrl\": \"/jabber/http-bind/\" }\n",
        replace => false,
        require => File["/var/www/vsroom"],
    }

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

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

    if !$vsroom_webhosts {
        $vsroom_webhosts = [ "${homename}" ]
    }

    configwebhost { $vsroom_webhosts: }

}
