#!/usr/bin/env bash

# bootstrap-ansible: download and build Ansible on a Debian/Ubuntu host

# Copyright (C) 2014-2018 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2014-2018 DebOps https://debops.org/


# This program is free software; you can redistribute
# it and/or modify it under the terms of the
# GNU General Public License as published by the Free
# Software Foundation; either version 3 of the License,
# or (at your option) any later version.
#
# This program is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General
# Public License along with this program; if not,
# write to the Free Software Foundation, Inc., 59
# Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# An on-line copy of the GNU General Public License can
# be downloaded from the FSF web page at:
# https://www.gnu.org/copyleft/gpl.html


# Usage: ./bootstrap-ansible [branch] [build_directory]

set -o nounset -o pipefail -o errexit


readonly DEBIAN_VERSION="$(sed 's/\..*//' /etc/debian_version)"

ansible_branch="${1:-devel}"
build_dir="${2:-}"

if [ -z "${build_dir}" ] ; then
    build_dir="$(mktemp -d)"
    # shellcheck disable=SC2064
    trap "rm -rf ${build_dir}" EXIT
fi

install_ansible_requirements () {

    local -a apt_packages

    apt_packages=( git devscripts cdbs debhelper dpkg-dev fakeroot sshpass \
                   asciidoc xmlto build-essential lsb-release dh-python )

    apt_packages+=(
        python-crypto \
        python-cryptography \
        python-dnspython \
        python-httplib2 \
        python-jinja2 \
        python-ldap \
        python-nose \
        python-paramiko \
        python-passlib \
        python-setuptools \
        python-sphinx \
        python-yaml
    )

    if [ "${DEBIAN_VERSION}" -gt 8 ] ; then
        apt_packages+=( python-packaging )
    fi

    sudo apt-get --no-install-recommends -y install  "${apt_packages[@]}"
}

build_ansible_deb () {

    # Build Debian package
    if [ -n "$(grep 'local_deb' Makefile || true)" ] ; then
        LANG=C make local_deb > /tmp/ansible-deb-build.log \
            || tail -n 50 /tmp/ansible-deb-build.log
    else
        LANG=C make deb > /tmp/ansible-deb-build.log \
            || tail -n 50 /tmp/ansible-deb-build.log
    fi

    # Check if .deb package with new method is present
    if [ -n "$(find deb-build/unstable/ -name "ansible_*_all.deb" 2>/dev/null)" ]; then

        sudo dpkg -i deb-build/unstable/ansible_*_all.deb

    # Otherwise, look for package generated with old method
    elif [ -n "$(find .. -name "ansible_*_all.deb" 2>/dev/null)" ]; then

        sudo dpkg -i ../ansible_*_all.deb

    fi

}

bootstrap_ansible_deb () {

    local ansible_branch
    local build_dir
    local ansible_git_repo
    local ansible_source_dir

    ansible_branch="${1:-devel}"
    build_dir="${2:-$(mktemp -d)}"
    ansible_git_repo="${3:-https://github.com/ansible/ansible}"
    ansible_source_dir="ansible"

    if [ ! -d "${build_dir}" ] ; then
        mkdir -p "${build_dir}"
    fi

    cd "${build_dir}" || exit 1

    if [ -d "${ansible_source_dir}" ] ; then

        cd "${ansible_source_dir}" || exit 1

        local old_git_checkout
        local current_branch_name
        local current_branch_name
        local current_branch_name

        old_git_checkout="$(git rev-parse HEAD)"
        current_branch_name="$(git symbolic-ref HEAD 2>/dev/null)" ||
        current_branch_name="(unnamed branch)"     # detached HEAD
        current_branch_name=${current_branch_name##refs/heads/}

        if [ "${current_branch_name}" != "${ansible_branch}" ] ; then
            git checkout "${ansible_branch}"
        fi

        git pull --quiet
        git submodule update

        local current_git_checkout

        current_git_checkout="$(git rev-parse HEAD)"

        if [ "${old_git_checkout}" != "${current_git_checkout}" ] ; then
            build_ansible_deb
        fi

    else

        install_ansible_requirements
        git clone --branch "${ansible_branch}" --recursive "${ansible_git_repo}" "${ansible_source_dir}"
        cd "${ansible_source_dir}" || exit 1
        build_ansible_deb

    fi
}

bootstrap_ansible_deb "${ansible_branch}" "${build_dir}"
