#!/bin/bash

VERSION="1.2.1"
TMP="${RUNNER_TEMP:-/tmp}"
JAR="${TMP}/dependency-tree-diff.jar"
MD5="f3b743fde958bc67c76e75c52b54fca5"

wget --timestamping --quiet --show-progress "https://github.com/JakeWharton/dependency-tree-diff/releases/download/$VERSION/dependency-tree-diff.jar" -P "$TMP"

if [ "$(md5sum "$JAR" | cut -d' ' -f1)" != "$MD5" ]; then
    echo "Error: MD5 checksum verification failed"
    exit 1
fi

update_deps() {
    local output_file=$1
    local gradle_config=$2
    local gradle_project=$3

    echo "Updating $gradle_project:$gradle_config"

    java -jar "$JAR" "$output_file" <( ./gradlew $gradle_project:dependencies \
        --no-configuration-cache \
        --configuration "$gradle_config" \
        < /dev/null ) > "$output_file"
}

update_deps deps_googleplay.txt googleplayReleaseRuntimeClasspath :app
update_deps deps_fdroid.txt genericReleaseRuntimeClasspath :app
update_deps deps_wear.txt releaseRuntimeClasspath :wear
