#!/bin/sh -eu

tmpdir=
restore_vendor=
restore_cargo=
cleanup_tmpdir() {
    [ -z "$tmpdir" ] || rm -rf -- "$tmpdir"
    exit "$@"
}


at_exit() {
    if [ -n "$restore_vendor" ]; then
        rm -rf vendor
        mv "$tmpdir/vendor" -T vendor
    fi
    if [ -n "$restore_cargo" ]; then
        mv "$tmpdir/.cargo" -T .cargo
    fi
    cleanup_tmpdir "$@"
}

tmpdir=$(mktemp -dt "${0##*/}.XXXXXXXX")
trap 'at_exit $?' EXIT
trap 'exit 143' HUP INT QUIT PIPE TERM

if [ -d .cargo ]; then
    mv .cargo -t "$tmpdir"
    restore_cargo=1
fi
if [ -d vendor ]; then
    mv vendor -t "$tmpdir"
    restore_vendor=1
fi
cargo vendor-filterer  --all-features \
    --platform=aarch64-unknown-linux-gnu \
    --platform=armv7-unknown-linux-gnueabihf \
    --platform=loongarch64-unknown-linux-gnu \
    --platform=i686-unknown-linux-gnu \
    --platform=powerpc64le-unknown-linux-gnu \
    --platform=riscv64gc-unknown-linux-gnu \
    --platform=x86_64-unknown-linux-gnu \
    "$@"
#
restore_vendor=
