# == class: archvsync
#
# base glance config.
#
# === parameters:
#
#  [*manage_apache*]
#    (Optional) Install and configure Apache.
#    Defaults to true
#
#  [*manage_pureftpd*]
#    (Optional) Install and configure pureftpd.
#    Defaults to true
#
#  [*package_ensure*]
#    (Optional) Ensure state for package.
#    Defaults to 'present'
#
#  [*mirrorname*]
#    (Optional) Name of the mirror.
#    Defaults to the hostname's FQDN.
#
#  [*to*]
#    (Optional) Mirror destination.
#    Defaults to '/home/ftp/debian/'
#
#  [*mailto*]
#    (Optional) Email of the ftpmaster.
#    Defaults to 'toto@example.com'
#
#  [*homedir*]
#    (Optional) Mirror FTP home.
#    Defaults to '/home/ftp'
#
#  [*hub*]
#    (Optional) HUB param.
#    Defaults to 'false'
#
#  [*rsync_host*]
#    (Optional) Source to rsync from.
#    Defaults to ftp.fr.debian.org.
#
#  [*rsync_path*]
#    (Optional) Rsync path in the rsync_host.
#    Defaults to 'debian'
#
#  [*info_maintainer*]
#    (Optional) Maintainer name and email.
#    Defaults to 'Toor Op <root@localhost>'
#
#  [*info_sponsor*]
#    (Optional) Company sponsoring the mirror.
#    Defaults to 'World Company SA <https://www.example.com>'
#
#  [*info_country*]
#    (Optional) Country where the mirror will be hosted in.
#    Defaults to 'US'
#
#  [*info_location*]
#    (Optional) Location where the mirror will be hosted in.
#    Defaults to 'Nowhere city'
#
#  [*info_throughput*]
#    (Optional) Bandwidth speed of the mirror.
#    Defaults to '10Gb'
#
#  [*arch_include*]
#    (Optional) Architectures to mirror.
#    Defaults to 'amd64 source'
#
#  [*arch_exclude*]
#    (Optional) Architectures to mirror.
#    Defaults to ''
#
#  [*logdir*]
#    (Optional) Directory where logs will be written.
#    Defaults to '/home/ftp/log'
#
#  [*setup_daily_cron*]
#    (Optional) Install a cron job to do ftpsync every day.
#    Default to true
#
class archvsync(
  $manage_apache    = true,
  $manage_pureftpd  = true,
  $package_ensure   = 'present',
  $mirrorname       = $::fqdn,
  $to               = '/home/ftp/debian/',
  $mailto           = 'toto@example.com',
  $homedir          = '/home/ftp',
  $hub              = 'false',
  $rsync_host       = 'ftp.fr.debian.org',
  $rsync_path       = 'debian',
  $info_maintainer  = 'Toor Op <root@localhost>',
  $info_sponsor     = 'World Company SA <https://www.example.com>',
  $info_country     = 'US',
  $info_location    = 'Nowhere city',
  $info_throughput  = '10Gb',
  $arch_include     = 'amd64 source',
  $arch_exclude     = '',
  $logdir           = '/home/ftp/log',
  $setup_daily_cron = true,
) {
  include ::archvsync::deps

  group { 'ftp':
    ensure => 'present',
  }->
  user { 'ftp':
    ensure           => 'present',
    home             => $homedir,
    password         => '!!',
    password_max_age => '99999',
    password_min_age => '0',
    shell            => '/bin/bash',
    groups           => ['ftp'],
  }->
  file { $homedir:
    ensure                  => directory,
    owner                   => ftp,
    mode                    => '0755',
    selinux_ignore_defaults => true,
  }->
  file { $to:
    ensure                  => directory,
    owner                   => ftp,
    mode                    => '0755',
    selinux_ignore_defaults => true,
  }->
  file { $logdir:
    ensure                  => directory,
    owner                   => ftp,
    mode                    => '0755',
    selinux_ignore_defaults => true,
  }->
  file { "${homedir}/.config":
    ensure                  => directory,
    owner                   => ftp,
    mode                    => '0755',
    selinux_ignore_defaults => true,
  }->
  file { "${homedir}/.config/ftpsync":
    ensure                  => directory,
    owner                   => ftp,
    mode                    => '0755',
    selinux_ignore_defaults => true,
  }->
  file { "${homedir}/.config/ftpsync/ftpsync.conf":
    ensure                  => file,
    owner                   => ftp,
    mode                    => '0644',
    selinux_ignore_defaults => true,
    content                 => template("${module_name}/ftpsync.conf.erb"),
  }->
  package { 'ftpsync':
    ensure => $package_ensure,
    tag    => ['archvsync-package'],
  }

  if $setup_daily_cron {
    file { '/etc/cron.daily/ftpsync':
      ensure                  => file,
      owner                   => 'root',
      mode                    => '0755',
      selinux_ignore_defaults => true,
      content                 => template("${module_name}/ftpsync.erb"),
      require                 => Package['ftpsync']
    }
  }

  if $manage_apache {
    class { '::archvsync::apache': }
  }

  if $manage_pureftpd {
    class { '::archvsync::pureftpd':
      package_ensure => $package_ensure,
    }
  }
}
