#!/bin/bash

VERSION=${VERSION:-0.5.31}

if [ ! -r wasm-component-ld-$VERSION.tar.lz ]; then
  echo "ERROR: wasm-component-ld-$VERSION.tar.lz not found"
  exit 1
fi

# Let's get the timestamp correct as long as we're here:
touch -d "$(tar tvf wasm-component-ld-$VERSION.tar.lz | head -n 1 | cut -d 0 -f 2- | cut -d ' ' -f 2-3)" wasm-component-ld-$VERSION.tar.lz

# Clear any existing stuff out:
rm -rf wasm-component-ld-$VERSION

# Extract the original tarball:
tar xf wasm-component-ld-$VERSION.tar.lz

# Vendor it:
cd wasm-component-ld-$VERSION
  if ! [ -f /usr/bin/cargo-vendor-filterer ]; then
    echo "WARNING: Creating unfiltered vendor libs tarball!"
    cargo vendor
  else
    cargo vendor-filterer --platform="x86_64-unknown-linux-gnu" --platform="i686-unknown-linux-gnu"
  fi
cd ..
mv wasm-component-ld-$VERSION wasm-component-ld-vendored-$VERSION

# Tar up the vendored version:
tar cf wasm-component-ld-vendored-$VERSION.tar wasm-component-ld-vendored-$VERSION
plzip -9 wasm-component-ld-vendored-$VERSION.tar

# Clean up:
rm -rf wasm-component-ld-vendored-$VERSION
