# Install MoinMoin.
#
class wiki::moin {

    file { "/usr/local/src/moin.tar.gz":
        ensure => present,
        mode   => 0644,
        owner  => root,
        group  => root,
        source => "puppet:///files/packages/moin.tar.gz",
        links  => follow,
    }
    file { "/usr/local/src/moin.patch":
        ensure => present,
        mode   => 0644,
        owner  => root,
        group  => root,
        source => "puppet:///files/packages/moin.patch",
        links  => follow,
    }
    util::extract::tar { "/usr/local/src/moin":
        strip   => 1,
        source  => "/usr/local/src/moin.tar.gz",
        require => File["/usr/local/src/moin.tar.gz"],
    }
    util::patch { "/usr/local/src/moin":
        source  => "/usr/local/src/moin.patch",
        require => Util::Extract::Tar["/usr/local/src/moin"],
    }
    python::setup::install { "/usr/local/src/moin":
        require => Util::Patch["/usr/local/src/moin"],
    }

}


# Fetch graphingwiki from svn.
#
class wiki::graphingwiki::common {

    file { "/usr/local/src/graphingwiki.tar.gz":
        ensure => directory,
        mode   => 0644,
        owner  => root,
        group  => root,
        source => "puppet:///files/packages/graphingwiki.tar.gz",
        links  => follow,
    }
    util::extract::tar { "/usr/local/src/graphingwiki":
        strip   => 1,
        source  => "/usr/local/src/graphingwiki.tar.gz",
        require => File["/usr/local/src/graphingwiki.tar.gz"],
    }

}


# Install Graphingwiki.
#
class wiki::graphingwiki inherits wiki::graphingwiki::common {

    python::setup::install { "/usr/local/src/graphingwiki/graphingwiki":
        require => Util::Extract::Tar["/usr/local/src/graphingwiki"],
    }

}


# Install Opencollab.
#
class wiki::opencollab inherits wiki::graphingwiki::common {

    python::setup::install { "/usr/local/src/graphingwiki/opencollab":
        require => Util::Extract::Tar["/usr/local/src/graphingwiki"],
    }

}


# Install collab.
#
class wiki::collab inherits wiki::graphingwiki::common {

    python::setup::install { "/usr/local/src/graphingwiki/collab":
        require => Util::Extract::Tar["/usr/local/src/graphingwiki"],
    }

}


# Configure collab server.
#
# === Global variables
#
#   $wiki_datadir:
#       Directory for wiki data. Defaults to /srv/wikis.
#
#   $collab_webhosts:
#       List of collab virtual hosts. Defaults to [ "$homename" ].
#
#   $collab_jabberdomain:
#       Domain for jabber extauth.
#
#   $collab_conferencedomain:
#       Conference domain for jabber extauth.
#       Defaults to conference.$wiki_collab_jabberdomain.
#
class wiki::collab::base {

    include cairo::python
    include igraph::python
    include graphviz::python
    include ldap::client::python

    include apache::mod::authnz_ldap
    include apache::mod::ldap
    include apache::mod::rewrite
    include apache::mod::wsgi

    include wiki::graphingwiki
    include wiki::opencollab
    include wiki::collab

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

    exec { "usermod-www-data":
        path    => "/bin:/usr/bin:/sbin:/usr/sbin",
        command => "usermod -a -G collab ${apache::sslserver::user}",
        unless  => "id -n -G ${apache::sslserver::user} | grep '\\bcollab\\b'",
        require => Group["collab"],
    }

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

        file { "/srv/wikis":
            ensure  => link,
            target  => "${wiki_datadir}",
            require => File["${wiki_datadir}"],
        }
    } else {
        file { "/srv/wikis":
            ensure  => directory,
            mode    => 0755,
            owner   => root,
            group   => root,
        }
    }

    file { [ "/srv/wikis/collab",
             "/srv/wikis/collab/archive",
             "/srv/wikis/collab/cache",
             "/srv/wikis/collab/config",
             "/srv/wikis/collab/docbook",
             "/srv/wikis/collab/htdocs",
             "/srv/wikis/collab/log",
             "/srv/wikis/collab/underlay",
             "/srv/wikis/collab/user",
             "/srv/wikis/collab/wikis",
             "/srv/wikis/collab/run", ]:
        ensure  => directory,
        mode    => 2660,
        owner   => collab,
        group   => collab,
        require => [ File["/srv/wikis"], User["collab"], Group["collab"],
                     Python::Setup::Install["/usr/local/src/graphingwiki/collab"],
                     Python::Setup::Install["/usr/local/src/moin"], ],
    }

    exec { "collab-copy-underlay":
        user    => collab,
        group   => collab,
        path    => "/bin:/usr/bin:/sbin:/usr/sbin",
        cwd     => $operatingsystem ? {
            ubuntu  => "/usr/local/share/moin/underlay",
            default => "/usr/share/moin/underlay",
        },
        command => "cp -R * /srv/wikis/collab/underlay && chmod -R g=u,o-rwx /srv/wikis/collab/underlay/*",
        creates => "/srv/wikis/collab/underlay/pages",
        require => File["/srv/wikis/collab/underlay"],
    }

    file { "/srv/wikis/collab/config/collabfarm.py":
        ensure  => present,
        mode    => 0660,
        owner   => collab,
        group   => collab,
        source  => [ "puppet:///private/wiki/collabfarm.py",
                     "puppet:///files/wiki/collabfarm.py",
                     "/usr/local/src/graphingwiki/collab/config/collabfarm.py", ],
        replace => false,
        require => File["/srv/wikis/collab/config"],
    }

    file { "/srv/wikis/collab/config/intermap.txt":
        ensure  => present,
        mode    => 0660,
        owner   => collab,
        group   => collab,
        source  => "/usr/local/src/graphingwiki/collab/config/intermap.txt",
        replace => false,
        require => File["/srv/wikis/collab/config"],
    }

    file { "/srv/wikis/collab/config/logging.conf":
        ensure  => present,
        mode    => 0660,
        owner   => collab,
        group   => collab,
        source  => "/usr/local/src/graphingwiki/collab/config/logging.conf",
        replace => false,
        require => File["/srv/wikis/collab/config"],
    }

    file { "/srv/wikis/collab/htdocs/.htaccess":
        ensure  => present,
        mode    => 0660,
        owner   => collab,
        group   => collab,
        source  => "puppet:///modules/wiki/htaccess",
        replace => false,
        require => File["/srv/wikis/collab/htdocs"],
    }

    file { [ "/etc/local", "/etc/local/collab" ]:
        ensure => directory,
        mode   => 0755,
        owner  => root,
        group  => root,
    }

    if $collab_jabberdomain and !$collab_conferencedomain {
        $collab_conferencedomain = "conference.${collab_jabberdomain}"
    }

    file { "/etc/local/collab/collab.ini":
        ensure  => present,
        mode    => 0644,
        owner   => root,
        group   => root,
        content => template("wiki/collab.ini.erb"),
        require => File["/etc/local/collab"],
    }

    if !$wiki_collab_fqdn {
        $wiki_collab_fqdn = $homename
    }

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

    cron { "collab-htaccess":
        ensure  => present,
        command => $operatingsystem ? {
            ubuntu  => "/usr/local/bin/collab-htaccess",
            default => "/usr/bin/collab-htaccess",
        },
        user    => "collab",
        require => [ Class["wiki::collab"], File["/srv/wikis/collab/htdocs/.htaccess"], ]
    }

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

        file { "/srv/www/https/${name}/moin_static":
            ensure  => link,
            target  => $operatingsystem ? {
                ubuntu  => "/usr/local/share/moin/htdocs",
                default => "/usr/share/moin/htdocs",
            },
            require => File["/srv/www/https/${name}"],
        }
    }

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

    configwebhost { $collab_webhosts: }

}
