#!/usr/bin/env bash
set -eux

VERSIONS_FILE_URL="https://raw.githubusercontent.com/harvester/harvester/master/misc/support-versions.txt"
HARVESTER_IMAGES="$(pwd)/$(dirname $0)/images-sources-harvester-standalone.txt"

# Variable that holds all versions of Harvester that will be scanned.
# master branch is always scanned and other versions are added in
# retrieve_versions().
SCAN_VERSIONS=("master")

retrieve_versions() {
    local version_regex="^v[0-9]+\.[0-9]+\.[0-9]+(-rc[0-9]+)?$"

    for version in $(curl -s "${VERSIONS_FILE_URL}" | grep -v "^#"); do
        if (echo "$version" | grep -qE "$version_regex"); then
            SCAN_VERSIONS+=(${version})

            # This regex is to match the respective dev branch in Harvester.
            # Example: the version retrieved is 'v1.3.0', so the dev branch is
            # 'v1.3'.
            local dev_branch=$(echo "$version" | sed "s/\.[0-9]\+\(-rc[0-9]\+\)\?$//")
            SCAN_VERSIONS+=(${dev_branch})
        else
            echo "=> Skipping Harvester $version - it does not match our version regex"
        fi
    done
}

download_image_lists() {
    local version="$1"
    local images_file_before_130="https://releases.rancher.com/harvester/$version/image-lists.tar.gz"
    local images_file_after_130="https://releases.rancher.com/harvester/$version/image-lists-amd64.tar.gz"
    local image_file=""
    local dir=$(pwd)
    local tmpdir=$(mktemp -d)

    cd "$tmpdir"

    echo "=> Downloading Harvester's image file for version $version"

    if (curl --fail -sLO "$images_file_after_130"); then
        image_file=$(basename "$images_file_after_130")
    elif (curl --fail -sLO "$images_file_before_130"); then
        image_file=$(basename "$images_file_before_130")
    else
        echo "=> Unable to find an image file for Harvester's $version version"
        exit 1
    fi

    extract_images "$image_file" "$version"

    cd "$dir" && rm -rf "$tmpdir"
}

extract_images() {
    local image_file="$1"
    local version="$2"
    local dev_version_regex="^v[0-9]+\.[0-9]+$"

    tar xzf "$image_file"

    # Remove the extra file that is only present in the master and dev branches.
    # This file contains 2 images that use a commit ID as their tag and, for
    # some reason, they are invalid. The right tag for that image is present in
    # another file and is always the corresponding version tag.
    rm -f image-lists/harvester-extra-"$version".txt

    # For dev branches we add the suffix '-head', similar to Rancher's head
    # branches. This is needed, so image-scanning can differentiate head
    # branches and only create issues for images of such branches.
    if (echo "$version" | grep -qE "$dev_version_regex"); then
        version="${version}-head"
    fi

    # Concatenate all individual images files from the Harvester's big version
    # file. Then we remove the 'docker.io' domain, but keep 'registry.suse.com'
    # and other domains, to be inline with the Rancher's image file. Finally, we
    # add the 'harvester' and 'release/harvester/<version>' versions labels that
    # will be used by image-scanning when creating the GH issues. Note that
    # there is a space before the labels and they are separated by a comma.
    # Everything is concatenated into one big file that will be parsed by
    # image-scanning.
    cat image-lists/*.txt | \
        sed "s/^docker.io\///" | \
        sed "s/$/ harvester,release\/harvester\/$version/" >> "$HARVESTER_IMAGES"
}

main() {
    rm -rf "$HARVESTER_IMAGES"

    retrieve_versions

    for version in "${SCAN_VERSIONS[@]}"; do
        download_image_lists "${version}"
    done

    echo "=> Generated list of Harvester's standalone images"
    cat "$HARVESTER_IMAGES"
}

main

