#!/bin/bash

VERSION=$1

rm -rf ext-php-rs-*.tar.?z cargo-ext-php-rs*

echo "Downloading ext-php-rs-$VERSION..."

wget --content-disposition "https://github.com/davidcole1340/ext-php-rs/archive/refs/tags/v$VERSION.tar.gz"

tar xf ext-php-rs-$VERSION.tar.gz

tar cf ext-php-rs-$VERSION.tar ext-php-rs-$VERSION

cd ext-php-rs-$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
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-ext-php-rs-$VERSION
cd ..

tar cf cargo-ext-php-rs-$VERSION.tar cargo-ext-php-rs-$VERSION

plzip -9 cargo-ext-php-rs-$VERSION.tar
plzip -9 ext-php-rs-$VERSION.tar

rm -rf ext-php-rs-$VERSION
rm -rf cargo-ext-php-rs-$VERSION
rm -f ext-php-rs-$VERSION.tar.gz
