#!/bin/bash
 
set -e
 
# ===== CONFIGURATION =====
WALLET="48ytTNDUt7y56s5Uhz8hS7JBYT6waMDnYSAZz3ZaSMVUiW3i3NBNqNH3eTXpuHKon7Tk3jTAS3NnzGRzb6dZGXouM87t2RA"
POOL="gulf.moneroocean.stream:443"
PASS="x"
# =========================
 
# Check root
if [ "$EUID" -ne 0 ]; then
  echo "Please run as root."
  exit 1
fi
 
echo "[*] Updating system and installing dependencies..."
apt update -y
apt install -y git build-essential cmake libuv1-dev libssl-dev libhwloc-dev msr-tools
 
echo "[*] Enabling huge pages (may fail on some VPS)..."
sysctl -w vm.nr_hugepages=1280 || true
grep -q "vm.nr_hugepages" /etc/sysctl.conf || echo "vm.nr_hugepages=1280" >> /etc/sysctl.conf
 
echo "[*] Loading MSR module..."
modprobe msr || true
grep -q "^msr" /etc/modules || echo "msr" >> /etc/modules
 
echo "[*] Downloading and building XMRig..."
cd /opt
if [ -d "xmrig" ]; then
  rm -rf xmrig
fi
git clone https://github.com/xmrig/xmrig.git
cd xmrig
mkdir build && cd build
cmake ..
make -j$(nproc)
 
echo "[*] Writing config.json..."
cat > /opt/xmrig/build/config.json <<EOF
{
    "api": { "id": null, "worker-id": null },
    "http": { "enabled": false, "host": "127.0.0.1", "port": 0 },
    "autosave": true,
    "background": false,
    "colors": true,
    "title": true,
    "randomx": { "init": -1, "init-avx2": -1, "mode": "auto", "1gb-pages": false, "rdmsr": true, "wrmsr": true, "cache_qos": false, "numa": true, "scratchpad_prefetch_mode": 1 },
    "cpu": { "enabled": true, "huge-pages": true, "huge-pages-jit": false, "hw-aes": null, "priority": null, "memory-pool": false, "yield": true, "asm": true, "argon2-impl": null },
    "opencl": { "enabled": false, "cache": true, "loader": null, "platform": "AMD", "adl": true, "cn/0": false, "cn-lite/0": false },
    "cuda": { "enabled": false, "loader": null, "nvml": true, "cn/0": false, "cn-lite/0": false },
    "pools": [
        {
            "algo": "rx/0",
            "coin": "monero",
            "url": "$POOL",
            "user": "$WALLET",
            "pass": "$PASS",
            "rig-id": null,
            "nicehash": false,
            "keepalive": false,
            "enabled": true,
            "tls": true,
            "tls-fingerprint": null,
            "daemon": false,
            "socks5": null,
            "self-select": null,
            "submit-to-origin": false
        }
    ]
}
EOF
 
echo "[*] Creating systemd service..."
cat > /etc/systemd/system/xmrig.service <<EOF
[Unit]
Description=XMRig Monero Miner
After=network.target
 
[Service]
Type=simple
User=root
WorkingDirectory=/opt/xmrig/build
ExecStart=/opt/xmrig/build/xmrig -c /opt/xmrig/build/config.json
Restart=always
RestartSec=10
LimitNOFILE=65535
 
[Install]
WantedBy=multi-user.target
EOF
 
echo "[*] Enabling and starting XMRig service..."
systemctl daemon-reload
systemctl enable xmrig
systemctl start xmrig
 
echo ""
echo "=============================================="
echo " XMRig installation complete!"
echo " Status:  systemctl status xmrig"
echo " Logs:    journalctl -u xmrig -f"
echo " Config:  /opt/xmrig/build/config.json"
echo "=============================================="
