#!/bin/bash -xe
# The script fails at the first error

PLUGIN=$1


function setup_build_env() {
    source /etc/os-release || source /usr/lib/os-release
    if [ "${ID}" = "ubuntu" ]; then
        # The Ubuntu kernel, for mysterious reasons, can be read only by root. Fix it.
        # See https://bugs.launchpad.net/ubuntu/+source/linux/+bug/759725
        sudo dpkg-statoverride --add --update root root 0644 /boot/vmlinuz-$(uname -r)
    fi
}


function get_cloud_image() {
    # Download the cloud image for the specified distro and version
    local required_name="$1"
    local distro_name="$2"
    local img_url=""
    case "${distro_name}" in
        "centos7")
            cache_name="CentOS-7-x86_64-GenericCloud.qcow2"
            img_url="http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2"
            ;;
        "ubuntu-trusty")
            # assume trusty for now
            cache_name="trusty-server-cloudimg-amd64-disk1.img"
            img_url="https://cloud-images.ubuntu.com/trusty/current/trusty-server-cloudimg-amd64-disk1.img"
            ;;
        *)
            ;;
    esac

    # download the image to the cache
    if [ ! -f "${cache_name}" ]; then
        curl -o "${cache_name}" "${img_url}"
    fi
    cp -f ${cache_name} ${required_name}
}


function build_images() {
    # build all the images for the specified plugin_name
    # - plugin_name: name of the plugin as required by sahara-image-pack
    # - plugin_version: version of the plugin
    # - distributions: list of distributions for the version of the plugin
    local plugin_name="$1"
    local plugin_version="$2"
    local distributions="$3"
    local image_name=""
    for distro in ${distributions}; do
        image_name="${distro}_${plugin_name}_${plugin_version}.qcow2"
        get_cloud_image "${image_name}" "${distro}"
        tox -e images -- sahara-image-pack --image "${image_name}" "${plugin_name}" "${plugin_version}"
    done
}


setup_build_env

# This define the matrix: for each plugin version, add a line like:
# build_images "<plugin_name>" "<plugin_version>" "<distribution> <distribution>"

case "$PLUGIN" in
    "cloudera")
        build_images "cdh" "5.9.0" "centos7"
        build_images "cdh" "5.11.0" "centos7"
        build_images "cdh" "5.13.0" "centos7"
        ;;
    "ambari")
        build_images "ambari" "2.4" "centos7"
        ;;
    "mapr")
        build_images "mapr" "5.2.0.mrv2" "centos7"
        ;;
    *)
        echo "Invalid version"
        ;;
esac
