#!/usr/bin/env sh

ARCH="$(uname -m)"
SYS_NAME="$(uname -s)"
JIRAIC_INSTALL_DIR="/usr/local/bin"
JIRAIC_EXEC_PATH="/usr/local/bin/jiraic"

if [ -f $JIRAIC_EXEC_PATH ]; then
  echo "The Jirai Compiler is already installed."
else
  echo "Downloading the binary for $SYS_NAME $ARCH."
  if [[ "$SYS_NAME" == "Darwin" ]]; then
    if [[ "$ARCH" == "arm64" ]]; then
      curl -O https://binaries.alyxshang.boo/jirai/0.3.0/jiraic-0.3.0-apple-silicon.bin
      mv jiraic-* jiraic
    else
      curl -O https://binaries.alyxshang.boo/jirai/0.3.0/jiraic-0.3.0-intel.bin
      mv jiraic-* jiraic
    fi
  else
    if [[ "$SYS_NAME" == "Linux" ]]; then
      if [[ "$ARCH" == "x86_64" ]]; then
        curl -O https://binaries.alyxshang.boo/jirai/0.3.0/jiraic-v.0.3.0-gnu.bin
        mv jiraic-* jiraic
      else
        echo "Unsupported architecture."
      fi
    else
      echo "Unsupported platform."
    fi
  fi
  echo "Installing the binary to $JIRAIC_INSTALL_DIR".
  chmod a+x jiraic
  sudo mv jiraic $JIRAIC_INSTALL_DIR
  echo "Installed to $(which jiraic)."
fi
