
# Install yum-updatesd service
#
class yum::updatesd {

    package { "yum-updatesd":
        ensure => installed,
    }

    file { "/etc/yum/yum-updatesd.conf":
        ensure => present,
        source => [ "puppet:///files/yum/yum-updatesd.conf.${fqdn}",
                    "puppet:///files/yum/yum-updatesd.conf",
                    "puppet:///modules/yum/yum-updatesd.conf", ],
        mode   => 0644,
        owner  => root,
        group  => root,
        notify => Service["yum-updatesd"],
    }

    service { "yum-updatesd":
        ensure    => running,
        enable    => true,
        hasstatus => true,
        require   => Package["yum-updatesd"],
    }

}


# Common prequisites for yum
#
class yum::common {

    case $operatingsystem {
        "fedora": {
            $osname = "fedora"
            $osver = $operatingsystemrelease
        }
        "centos": {
            $osname = "el"
            $osver = regsubst($operatingsystemrelease, '^(\d+)\..*$', '\1')
        }
    }

}


# Add new yum repository.
#
# === Parameters
#
#   $name:
#       Repository name.
#   $baseurl:
#       Base URL for this repository.
#   $mirrorlist:
#       Mirrorlist URL for this repository.
#   $descr:
#       Repository description. Defaults to $name.
#   $gpgkey:
#       Location where GPG signing key can be found. If not set
#       GPG check will be disabled.
#
# === Sample usage
#
# yum::repo { "tmz-puppet":
#     baseurl => "http://tmz.fedorapeople.org/repo/puppet/epel/5/i386/",
#     descr   => "Puppet for EL 5 - i386",
#     gpgkey  => "http://tmz.fedorapeople.org/repo/RPM-GPG-KEY-tmz",
# }
#
define yum::repo($baseurl="", $mirrorlist="", $descr="", $gpgkey="") {

    tag("bootstrap")

    if !$baseurl and !$mirrorlist {
        fail("Either \$baseurl or \$mirrorlist needs to be defined for yum::repo")
    }
    if $baseurl and $mirrorlist {
        fail("Cannot set both \$baseurl and \$mirrorlist for yum::repo")
    }

    if regsubst($gpgkey, "^(puppet://).*", '\1') == "puppet://" {
        file { "/etc/pki/rpm-gpg/RPM-GPG-KEY-${name}":
            ensure => present,
            source => $gpgkey,
            mode   => 0644,
            owner  => "root",
            group  => "root",
            before => File["/etc/yum.repos.d/${name}.repo"],
        }
        $gpgkey_real = "file:///etc/pki/rpm-gpg/RPM-GPG-KEY-${name}"
    } else {
        $gpgkey_real = $gpgkey
    }

    if $descr {
        $descr_real = $descr
    } else {
        $descr_real = $name
    }

    file { "/etc/yum.repos.d/${name}.repo":
        ensure  => present,
        content => template("yum/yum.repo.erb"),
        mode    => 0644,
        owner   => "root",
        group   => "root",
    }

}


# Add Adobe repository
#
class yum::repo::adobe {

    yum::repo { "adobe":
        descr   => "Adobe Systems Incorporated",
        baseurl => "http://linuxdownload.adobe.com/linux/i386/",
        gpgkey  => "puppet:///modules/yum/keys/adobe.key",
    }

}


# Add Fedora EPEL repository
#
class yum::repo::epel {

    include yum::common

    if $yum::common::osname != "el" {
        fail("EPEL repository not supported in ${operatingsystem}")
    }

    yum::repo { "epel":
        descr      => "Extra Packages for Enterprise Linux ${yum::common::osver} - \$basearch",
        mirrorlist => "http://mirrors.fedoraproject.org/mirrorlist?repo=epel-${yum::common::osver}&arch=\$basearch",
        gpgkey     => "puppet:///modules/yum/keys/epel.key",
    }

}


# Add Google repository
#
class yum::repo::google {

    yum::repo { "google":
        descr   => "Google - \$basearch",
        baseurl => "http://dl.google.com/linux/rpm/stable/\$basearch",
        gpgkey  => "puppet:///modules/yum/keys/google.key",
    }

}


# Add RPM Fusion Free repository
#
class yum::repo::rpmfusion-free {

    include yum::common

    yum::repo { "rpmfusion-free":
        descr      => "RPM Fusion for ${yum::common::osname} \$releasever - Free",
        mirrorlist => "http://mirrors.rpmfusion.org/mirrorlist?repo=free-${yum::common::osname}-\$releasever&arch=\$basearch",
        gpgkey     => "puppet:///modules/yum/keys/rpmfusion-free-${yum::common::osname}-${yum::common::osver}.key",
    }
    yum::repo { "rpmfusion-free-updates":
        descr      => "RPM Fusion for ${yum::common::osname} \$releasever - Free",
        mirrorlist => "http://mirrors.rpmfusion.org/mirrorlist?repo=free-${yum::common::osname}-updates-released-\$releasever&arch=\$basearch",
        gpgkey     => "puppet:///modules/yum/keys/rpmfusion-free-${yum::common::osname}-${yum::common::osver}.key",
    }

}


# Add RPM Fusion Nonfree repository
#
# Enabling this will also enable RPM Fusion Free repository.
#
class yum::repo::rpmfusion-nonfree {

    if $operatingsystem != "Fedora" {
        fail("RPM Fusion Free repository not supported in ${operatingsystem}")
    }

    include yum::common
    include yum::repo::rpmfusion-free

    yum::repo { "rpmfusion-nonfree":
        descr      => "RPM Fusion for ${yum::common::osname} \$releasever - Nonfree",
        mirrorlist => "http://mirrors.rpmfusion.org/mirrorlist?repo=nonfree-${yum::common::osname}-\$releasever&arch=\$basearch",
        gpgkey     => "puppet:///modules/yum/keys/rpmfusion-nonfree-${yum::common::osname}-${yum::common::osver}.key",
    }
    yum::repo { "rpmfusion-nonfree-updates":
        descr      => "RPM Fusion for ${yum::common::osname} \$releasever - Nonfree",
        mirrorlist => "http://mirrors.rpmfusion.org/mirrorlist?repo=nonfree-${yum::common::osname}-updates-released-\$releasever&arch=\$basearch",
        gpgkey     => "puppet:///modules/yum/keys/rpmfusion-nonfree-${yum::common::osname}-${yum::common::osver}.key",
    }

}


# Add Skype repository
#
class yum::repo::skype {

    yum::repo { "skype":
        descr   => "Skype Yum Repository",
        baseurl => "http://download.skype.com/linux/repos/fedora/updates/i586/",
    }

}
