#!/bin/sh
# mimics: source ~/.cargo/env
export PATH="$HOME/.cargo/bin:$PATH"

# On Debian, cargo and rustc are sometimes different
VERSION=$(rustc --version)
if [ $? -eq 0 ]; then
  echo "Using `which cargo`"
  echo "Rustc version: $VERSION"

  # Check for old version of rustc (edition:2021 requires rust 1.56)
  VERNUM=$(echo $VERSION | cut -d' ' -f2) || true
  MAJOR=$(echo $VERNUM | cut -d'.' -f1) || true
  MINOR=$(echo $VERNUM | cut -d'.' -f2) || true
  if [ "$MAJOR" -eq "1" ] && [ "$MINOR" -lt "56" ]; then
    echo "Found old rust. Using legacy gifski 1.4.3 build."
    rm -f src/myrustlib/vendor.tar.xz
    cp -f src/old-1.4.3/* src/myrustlib/
  elif [ "$MAJOR" -eq "1" ] && [ "$MINOR" -lt "74" ]; then
    echo "Found old rust. Using legacy gifski 1.6.6 build."
    rm -f src/myrustlib/vendor.tar.xz
    cp -f src/old-1.6.6/* src/myrustlib/
  fi
  exit 0
fi

# Try local version on MacOS, otherwise error
echo "------------------ RUST COMPILER NOT FOUND --------------------"
echo ""
echo "Cargo was not found on the PATH. Please install cargo / rustc:"
echo ""
echo " - yum install cargo         (Fedora/CentOS)"
echo " - apt-get install cargo     (Debian/Ubuntu)"
echo " - brew install rust         (MacOS)"
echo ""
echo "Alternatively install Rust from: <https://www.rust-lang.org>"
echo ""
echo "---------------------------------------------------------------"
echo ""
exit 1
