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

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

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

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

    # For dev branches we remove the '.x' patch version and add the suffix
    # '-head' instead, 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.
    version=$(echo $version | sed "s/\.x$/-head/")

    # Concatenate the individual images files from the Longhorn's version file
    # into the bigger 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 'longhorn' and
    # 'release/longhorn/<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_file" | \
        sed "s/^docker.io\///" | \
        sed "s/$/ longhorn,release\/longhorn\/$version/" >> "$LONGHORN_IMAGES"
}

download_image_lists() {
    local version="$1"
    local images_file="https://raw.githubusercontent.com/longhorn/longhorn/$version/deploy/longhorn-images.txt"
    local image_filename=
    local dir=$(pwd)
    local tmpdir=$(mktemp -d)

    cd "$tmpdir"

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

    if !(curl --fail -sLO "$images_file"); then
        echo "=> Unable to find an image file for Longhorn's $version version"
        exit 1
    fi

    concat_images $(basename $images_file) "$version"

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

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 Longhorn.
            # Example: the version retrieved is 'v1.3.0', so the dev branch is
            # 'v1.3.x'.
            local dev_branch=$(echo "$version" | sed "s/\.[0-9]\+\(-rc[0-9]\+\)\?$/\.x/")
            SCAN_VERSIONS+=(${dev_branch})
        else
            echo "=> Longhorn $version does not match our version regex"
            exit 1
        fi
    done

    # Dedup versions.
    SCAN_VERSIONS=($(echo ${SCAN_VERSIONS[@]} | tr ' ' '\n' | sort -u))
}

main() {
    rm -rf "$LONGHORN_IMAGES"

    retrieve_versions

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

    echo "=> Generated list of Longhorn's standalone images"
    cat "$LONGHORN_IMAGES"
}

main

