#!/bin/bash

VERSION=$1

rm -rf starship-*.tar.?z cargo-starship*

echo "Downloading starship-$VERSION..."

wget --content-disposition "https://github.com/starship/starship/archive/refs/tags/v$VERSION.tar.gz"

tar xf starship-$VERSION.tar.gz

tar cf starship-$VERSION.tar starship-$VERSION

cd starship-$VERSION

  # Configure cargo-vendor-filterer
  cat << EOF >> Cargo.toml
[package.metadata.vendor-filter]
platforms = ["x86_64-unknown-linux-gnu", "i686-unknown-linux-gnu"]
all-features = true
exclude-crate-paths = [
  { name = "libz-sys", exclude = "src/zlib" },
  { name = "libz-sys", exclude = "src/zlib-ng" },
]
EOF


  if ! [ -f /usr/bin/cargo-vendor-filterer ]; then
    echo "WARNING: Creating unfiltered vendor libs tarball!"
    cargo vendor
  else
    cargo vendor-filterer
  fi

  mv vendor ../cargo-starship-$VERSION
cd ..

tar cf cargo-starship-$VERSION.tar cargo-starship-$VERSION

plzip -9 cargo-starship-$VERSION.tar
plzip -9 starship-$VERSION.tar

rm -rf starship-$VERSION
rm -rf cargo-starship-$VERSION
rm -f starship-$VERSION.tar.gz
