#!/bin/sh
# MyAiMiner.com Desktop Boost — solo Monero mining for Mac, Linux, Chromebook Linux and Android (Termux).
# Press Ctrl+C at any time to stop.
set -e
VER=6.26.0
WALLET=47EptGJK463hHpN5u8xfEyTujLipUGEk5ASQPP8FjnypQpFhkFQfCVvX83PxsTkKibFb7xos8R7kAhGhFcPAsPJUEDNxsdq

echo "MyAiMiner.com Desktop Boost: uses your CPU to solo-mine Monero for MyAiMiner.com."
printf "Do you agree to start mining? [y/N] "; read a
case "$a" in y|Y|yes|YES) ;; *) exit 0;; esac

OS="$(uname -s)"; ARCH="$(uname -m)"
BUILD=0; F=""
case "$OS-$ARCH" in
  Darwin-arm64) F=xmrig-$VER-macos-arm64.tar.gz ;;
  Darwin-*) F=xmrig-$VER-macos-x64.tar.gz ;;
  Linux-x86_64) F=xmrig-$VER-linux-static-x64.tar.gz ;;
  Linux-aarch64|Linux-arm64) BUILD=1 ;;
  *) echo "Sorry, this computer type is not supported."; exit 1 ;;
esac

# Install helper packages when they are missing (needs sudo on Mac/Linux, none in Termux).
SUDO=""
if [ "$(id -u)" != "0" ] && command -v sudo >/dev/null 2>&1; then SUDO=sudo; fi

install_pkgs() {
  if [ -n "${TERMUX_VERSION:-}" ]; then pkg install -y "$@"; return; fi
  if command -v apt-get >/dev/null 2>&1; then $SUDO apt-get update -y; $SUDO apt-get install -y "$@"; return; fi
  if command -v dnf >/dev/null 2>&1; then $SUDO dnf install -y "$@"; return; fi
  if command -v pacman >/dev/null 2>&1; then $SUDO pacman -Sy --noconfirm "$@"; return; fi
  if command -v apk >/dev/null 2>&1; then $SUDO apk add "$@"; return; fi
  echo "Please install these yourself, then run this file again: $*"; exit 1
}

DIR="$HOME/.myaiminer-boost"; mkdir -p "$DIR"; cd "$DIR"

if [ "$BUILD" = 1 ]; then
  # ARM/Android: XMRig publishes no ready-made download for this chip, so the
  # official source is compiled once. Everything after this is instant.
  if [ ! -x "src/xmrig-$VER/build/xmrig" ]; then
    echo "First run only: building the official XMRig $VER from source."
    echo "This can take 30-60 minutes on a phone or tablet. Later runs start instantly."
    if [ -n "${TERMUX_VERSION:-}" ]; then
      pkg install -y git cmake clang make libuv openssl zlib
    else
      install_pkgs git cmake build-essential libuv1-dev libssl-dev
    fi
    rm -rf "src/xmrig-$VER"
    git clone --depth 1 --branch "v$VER" https://github.com/xmrig/xmrig "src/xmrig-$VER"
    cmake -S "src/xmrig-$VER" -B "src/xmrig-$VER/build" -DCMAKE_BUILD_TYPE=Release -DWITH_HWLOC=OFF
    JOBS="$(nproc 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null || echo 2)"
    cmake --build "src/xmrig-$VER/build" -j "$JOBS"
  fi
  XMRIG="src/xmrig-$VER/build/xmrig"
else
  if [ ! -x "xmrig-$VER/xmrig" ]; then
    command -v curl >/dev/null 2>&1 || install_pkgs curl
    echo "Downloading the official XMRig $VER from github.com/xmrig ..."
    curl -fsSL -o x.tgz "https://github.com/xmrig/xmrig/releases/download/v$VER/$F"
    tar xzf x.tgz && rm x.tgz
  fi
  XMRIG="xmrig-$VER/xmrig"
fi

cd "$DIR"
set +e
while true; do
  for N in xmr-node.cakewallet.com xmr.support nodes.hashvault.pro node1.xmr-tw.org xmr.litepay.ch; do
    echo "Connecting to $N ..."
    "./$XMRIG" --daemon -o "$N:18081" -u "$WALLET" --coin monero --cpu-priority 1 --donate-level 1
    sleep 10
  done
done
