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

# Write 'cargo' to Makevars
VERSION=$(cargo --version)
if [ $? -eq 0 ]; then
  echo "Using `which cargo`"
  echo "$VERSION"

  # Check for old version of rustc (edition:2021 requires rust 1.56)
  if [ "$(uname)" = "Linux" ]; then
    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."
      cp -f src/legacy/* src/myrustlib/
    fi
  fi
  exit 0
fi

# Try local version on MacOS, otherwise error
[ `uname` = "Darwin" ] && curl "https://autobrew.github.io/scripts/rust" -sSf | sh && exit 0
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
