#!/bin/bash
#
# Downloads upstream releases and uses reprepro to update the
# repository in debian/. This is idempotent and doesn't clobber files
# if the latest releases is already included.
#

set -e

release_repo=tonarino/innernet

has_changes() {
    git status --porcelain "$@" | grep -q .
}

main() {
    local tmpd
    tmpd="$(mktemp -d tmp.addlatest.XXXXXXXXXX)"

    (
        cd "$tmpd"

        wget -Olatest.json \
             -H'Accept: application/json' \
             "https://api.github.com/repos/$release_repo/releases/latest"

        cat latest.json \
            | jq -r '.assets[] | select(.name | endswith(".deb")) | (.name + " " + .url)' \
            | while read name url; do
            wget --header='Accept: application/octet-stream' -O"$name" "$url"
            reprepro --export=silent-never -b ../debian includedeb unstable "$name"
        done
    )

    if has_changes debian/pool; then
        reprepro -b debian export unstable
    fi

    if has_changes debian/{db,dists,pool}; then
        git add debian/{db,dists,pool}
        git \
            -c 'user.email=41898282+github-actions[bot]@users.noreply.github.com' \
            -c 'user.name=github-actions[bot]' \
            commit -m "Included release $release_repo@$(cat "$tmpd/latest.json" | jq -r '.name | ltrimstr("v")')."
    else
        echo 'No updates to commit.'
    fi
}

main