#! /bin/sh

source_rpm_url=https://pagure.io/copr/copr-test-sources/raw/master/f/buildtag-0-0.src.rpm

log()
{
    stop_time=$(date +%s)
    took=$(( stop_time - start_time ))
    echo "build_id=$build_id start=$start_time stop=$stop_time took=$took exit_status=$exit_status" \
        >> "{{ ping_log }}"
}

start_time=$(date +%s)

#{% if devel %}

build_id=1
exit_status=0
log
exit 0

#{% endif %}

output=$(copr build --nowait @copr/copr-ping "$source_rpm_url")
build_id=$(echo "$output" | grep 'Created builds:' | cut -d' ' -f3)
exit_status=$?
if ! expr "$build_id" : '\([0-9]*\)$'; then
    # we don't even have the build_id
    build_id=failed_to_submit
fi

if test $exit_status -ne 0; then
    log
fi

copr watch-build "$build_id"
exit_status=$?
log
