{{ lookup('file', 'roles/install-script/files/function-common') }}


export PATH=$PATH:/usr/local/bin:/usr/local/sbin

# Include Additional Functions
function download_playbook {
    if [ ! -f /etc/opt/slapcache.cfg ]; then
      slapcache-conf
    fi
    DFILE="/tmp/tmpplaybook$(basename $0).$$/"
    TFILE="archive.tar.gz"
    mkdir -p $DFILE
    cd $DFILE
    slapcache-download --destination=$TFILE
    tar -xzvf $TFILE
    rm $TFILE
}

# Determine what system we are running on.  This provides ``os_VENDOR``,
# ``os_RELEASE``, ``os_UPDATE``, ``os_PACKAGE``, ``os_CODENAME``
# and ``DISTRO``
GetDistro

if [[ ${DISTRO} =~ (-.) ]] && [[ -f /etc/debian_version ]]; then
  apt_get install lsb-release
  GetDistro
fi


if [[ $EUID -gt 0 ]]; then
    echo "####################################################"
    echo "#                                                  #"
    echo "#  ERROR: You must be root to run this script!!!!  #"
    echo "#                                                  #"
    echo "####################################################"
    exit 1
fi

# Warn users who aren't on an explicitly supported distro, but allow them to
# override check and attempt installation with ``export FORCE=yes``
if [[ ! ${DISTRO} =~ (wheezy|jessie|trusty|rhel7|rhel6) ]]; then
    echo "WARNING: this script has not been tested on $DISTRO"
    if [[ "$FORCE" != "yes" ]]; then
        die $LINENO "If you wish to run this script anyway run with FORCE=yes"
    fi
fi

# Make sure wheezy backports are available.
if [[ $DISTRO == "wheezy" ]]; then
    echo "deb http://ftp.debian.org/debian wheezy-backports main contrib " > /etc/apt/sources.list.d/wheezy-backports.list
fi

if is_fedora && [[ $DISTRO == "rhel7" ]]; then
    # RHEL requires EPEL for many Ansible dependencies

    # NOTE: We always remove and install latest -- some environments
    # use snapshot images, and if EPEL version updates they break
    # unless we update them to latest version.
    if sudo yum repolist enabled epel | grep -q 'epel'; then
        uninstall_package epel-release || true
    fi

    # This trick installs the latest epel-release from a bootstrap
    # repo, then removes itself (as epel-release installed the
    # "real" repo).
    #
    # You would think that rather than this, you could use
    # $releasever directly in .repo file we create below.  However
    # RHEL gives a $releasever of "6Server" which breaks the path;
    # see https://bugzilla.redhat.com/show_bug.cgi?id=1150759
    cat <<EOF | sudo tee /etc/yum.repos.d/epel-bootstrap.repo
[epel-bootstrap]
name=Bootstrap EPEL
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-7&arch=\$basearch
failovermethod=priority
enabled=0
gpgcheck=0
EOF
    # Enable a bootstrap repo.  It is removed after finishing
    # the epel-release installation.
    yum-config-manager --enable epel-bootstrap
    yum_install epel-release || \
        die $LINENO "Error installing EPEL repo, cannot continue"
    # EPEL rpm has installed it's version
    rm -f /etc/yum.repos.d/epel-bootstrap.repo

    # ... and also optional to be enabled
    is_package_installed yum-utils || install_package yum-utils
    yum-config-manager --enable rhel-7-server-optional-rpms

fi

if is_fedora && [[ $DISTRO == "rhel6" ]]; then
    if sudo yum repolist enabled epel | grep -q 'epel'; then
        uninstall_package epel-release || true
    fi

    cat <<EOF | sudo tee /etc/yum.repos.d/epel-bootstrap.repo
[epel-bootstrap]
name=Bootstrap EPEL
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-6&arch=\$basearch
failovermethod=priority
enabled=0
gpgcheck=0
EOF
    yum-config-manager --enable epel-bootstrap
    yum_install epel-release || \
        die $LINENO "Error installing EPEL repo, cannot continue"
    # EPEL rpm has installed it's version
    rm -f /etc/yum.repos.d/epel-bootstrap.repo

    is_package_installed yum-utils || install_package yum-utils
    yum-config-manager --enable rhel-6-server-optional-rpms

    is_package_installed libselinux-python || install_package libselinux-python
fi

is_package_installed ansible || install_package ansible
is_package_installed python-setuptools || install_package python-setuptools

if is_ubuntu && [[ $DISTRO == "trusty" ]]; then
  is_package_installed python-apt || install_package python-apt
  is_package_installed python-pycurl || install_package python-pycurl
fi

ansible localhost -m easy_install -a name=slapcache --connection=local
