#!/usr/bin/env bash

set -euo pipefail

REPO_SLUG=${GITHUB_REPOSITORY}
GIT_COMMIT=${GITHUB_SHA}
GIT_BRANCH=main

function main() {
    URL="https://ci.appveyor.com/api/projects/$REPO_SLUG/history?recordsNumber=200&branch=$GIT_BRANCH"

    echo "Will poll $URL looking for commit $GIT_COMMIT."

    while true; do
        build=$(curl --fail --show-error --silent "$URL" | jq "[.builds | .[] | select(.commitId == \"$GIT_COMMIT\")][0]") || { sleep 10; continue; }
        id=$(echo "$build" | jq -r ".buildId")
        status=$(echo "$build" | jq -r ".status")

        if [ "$status" = "null" ]; then
            echo "Build has not started."
        else
            echo -n "https://ci.appveyor.com/project/batect/batect/builds/$id: "

            case "$status" in
            success)
                echo "build succeeded."
                exit 0
                ;;
            failed)
                echo "build failed."
                exit 1
                ;;
            cancelled)
                echo "build was cancelled."
                exit 1
                ;;
            *)
                echo "build has not completed: status is $status"
                ;;
            esac
        fi

        sleep 10
    done
}

main
