#!/bin/sh
# spell-checker:ignore uuhelp
ARG=""
if test "$1" != "--do-it"; then
    ARG="--dry-run --allow-dirty"
fi

desired_version=$(grep '^version' Cargo.toml | awk -F' = ' '{print $2}' | tr -d '"' | tr -d ' ')

# Figure out any dependencies between the util via Cargo.toml
# We store this as edges in a graph with each line:
# [dependent] [dependency]
# We use ROOT as a the node that should come before all other nodes.
PROGS=$(ls -1d src/uu/*/)
PARTIAL_ORDER=""
for p in $PROGS; do
    DEPENDENCIES=$(grep -oE "^uu_[a-z0-9]+" ${p}Cargo.toml)

    # Turn "src/uu/util/" into "util"
    p=${p#src/uu/}
    p=${p%/}

    PARTIAL_ORDER+="$p ROOT\n"
    while read d; do
        if [ $d ]; then
            # Remove "uu_" prefix
            d=${d#uu_}

            PARTIAL_ORDER+="$p $d\n"
        fi
    done <<<"$DEPENDENCIES"
done

# Apply tsort to get the order in which to publish the crates
TOTAL_ORDER=$(echo -e $PARTIAL_ORDER | tsort | tac)

# Remove the ROOT node from the start
TOTAL_ORDER=${TOTAL_ORDER#ROOT}

set -e
for dir in src/uuhelp_parser/ src/uucore_procs/ src/uucore/ src/uu/stdbuf/src/libstdbuf/; do
    (
        cd "$dir"
        #shellcheck disable=SC2086
#        cargo publish $ARG
    )
    sleep 2s
done

for p in $TOTAL_ORDER; do
    (
        cd "src/uu/$p"
        pkg_name=$(grep '^name' Cargo.toml | head -1 | awk -F' = ' '{print $2}' | tr -d '"' | tr -d ' ')
        crate_info=$(curl -s "https://crates.io/api/v1/crates/$pkg_name/versions")
        if echo "$crate_info" | grep -q "\"num\":\"$desired_version\""; then
            echo "$p version $desired_version already uploaded"
        else
	        #shellcheck disable=SC2086
	        cargo publish $ARG --allow-dirty
	fi
    )
done

#shellcheck disable=SC2086
cargo publish $ARG --allow-dirty
