class websites::setup {
  # Third-party hosted nodes generally aren't behind an https proxy
  $hosting_type = hiera('nodo::vserver::hosting_type', 'direct')

  # Include apache
  class { 'apache':
    https_proxy => $hosting_type ? {
      'direct' => 'yes',
      default  => false,
    },
  }

  # The needed apache modules
  apache::module { "alias":
    ensure  => present,
  }

  # Images folder
  file { "${apache::www_folder}/images":
    ensure  => directory,
    recurse => true,
    purge   => true,
    force   => true,
    owner   => "root",
    group   => "root",
    # This mode will also apply to files from the source directory
    mode    => 0644,
    # Puppet will automatically set +x for directories
    source  => [ "puppet:///modules/site_apache/htdocs/${::domain}/images",
                 "puppet:///modules/websites/htdocs/images", ]
  }

  # Certificates folder
  file { "${apache::www_folder}/certs":
    ensure  => directory,
    recurse => true,
    purge   => true,
    force   => true,
    owner   => "root",
    group   => "root",
    # This mode will also apply to files from the source directory
    mode    => 0644,
    # Puppet will automatically set +x for directories
    source  => [ "puppet:///modules/site_apache/htdocs/${::domain}/certs",
                 "puppet:///modules/websites/htdocs/certs", ]
  }

  # Web index
  file { "${apache::www_folder}/index.html":
    ensure  => present,
    owner   => "root",
    group   => "root",
    mode    => 0644,
    source  => [ "puppet:///modules/site_apache/htdocs/${::domain}/index.html",
                 "puppet:///modules/websites/htdocs/index.html", ]
  }

  # Missing page
  file { "${apache::www_folder}/missing.html":
    ensure  => present,
    owner   => "root",
    group   => "root",
    mode    => 0644,
    source  => [ "puppet:///modules/site_apache/htdocs/${::domain}/missing.html",
                 "puppet:///modules/websites/htdocs/missing.html", ]
  }

  # Make sure that a top level index exists
  file { "/var/www/index.html":
    ensure => present,
  }

  # TODO: remove in the future as the default vhost already does the job
  # Default vhost: can just be applied on the defining host
  apache::site { "${apache::server_name}":
    server_alias => "${::domain}",
    docroot      => "${apache::www_folder}",
    mpm          => false,
    tag          => 'all',
    ensure       => absent,
  }

  # We have to use 'zzz-error' so it will be the last matched vhost
  apache::site { "error":
    template       => 'apache/error.erb',
    docroot        => "${apache::error_folder}",
    filename       => 'zzz-error',
    mpm            => false,
    tag            => 'all',
  }

  # Index page for error
  file { "${apache::error_folder}/index.html":
    ensure  => "${apache::www_folder}/index.html",
    owner   => "root",
    group   => "root",
    force   => true,
    require => File["${apache::error_folder}"],
  }

  # Images folder for error
  file { "${apache::error_folder}/images":
    ensure  => "${apache::www_folder}/images",
    owner   => "root",
    group   => "root",
    force   => true,
    require => File["${apache::error_folder}", "${apache::www_folder}/images"],
  }
}